Class: HP::Cloud::ContainerResource

Inherits:
RemoteResource show all
Defined in:
lib/hpcloud/container_resource.rb

Constant Summary

Constants inherited from RemoteResource

RemoteResource::CONTAINER_META, RemoteResource::DEFAULT_STORAGE_MAX_SIZE, RemoteResource::DEFAULT_STORAGE_PAGE_LENGTH, RemoteResource::DEFAULT_STORAGE_SEGMENT_SIZE, RemoteResource::OBJECT_META

Instance Attribute Summary collapse

Attributes inherited from RemoteResource

#etag, #headers, #modified, #size, #synckey, #syncto, #type

Attributes inherited from Resource

#container, #cstatus, #destination, #fname, #ftype, #path, #public, #public_url, #readers, #restart, #writers

Instance Method Summary collapse

Methods inherited from RemoteResource

VALID_CONTAINER_META, VALID_OBJECT_META, #close, #container_head, #copy_file, #foreach, #get_destination, #get_size, #has_same_account, #object_head, #open, #parse_container_headers, #parse_object_headers, #printable_container_headers, #printable_object_headers, #read, #set_destination, #valid_destination, #valid_source, #write

Methods inherited from Resource

#close, #container_head, #copy, #copy_all, #copy_file, #foreach, #get_destination, #get_mime_type, #initialize, #isDirectory, #isFile, #isLocal, #isMulti, #isObject, #isRemote, #is_container?, #is_object_store?, #is_shared?, #is_valid?, #not_implemented, #open, #read, #set_destination, #set_error, #set_mime_type, #set_restart, #to_hash, #valid_destination, #valid_source, #write

Constructor Details

This class inherits a constructor from HP::Cloud::Resource

Instance Attribute Details

#countObject

Returns the value of attribute count.



27
28
29
# File 'lib/hpcloud/container_resource.rb', line 27

def count
  @count
end

Instance Method Details

#cdn_public_ssl_urlObject



75
76
77
# File 'lib/hpcloud/container_resource.rb', line 75

def cdn_public_ssl_url
  @storage.directories.head(@container).cdn_public_ssl_url
end

#cdn_public_urlObject



71
72
73
# File 'lib/hpcloud/container_resource.rb', line 71

def cdn_public_url
  @storage.directories.head(@container).cdn_public_url
end

#grant(acl) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/hpcloud/container_resource.rb', line 115

def grant(acl)
  begin
    return false unless container_head()

    @readacl.grant(acl.readers)
    @writeacl.grant(acl.writers)
    return save()
  rescue Exception => e
    @cstatus = CliStatus.new("Exception granting permissions for '#{@fname}': " + e.to_s, :general_error)
    return false
  end
end

#headObject



51
52
53
# File 'lib/hpcloud/container_resource.rb', line 51

def head
  return container_head()
end

#parseObject



29
30
31
32
33
34
35
36
37
# File 'lib/hpcloud/container_resource.rb', line 29

def parse
  unless @fname.index('/').nil?
    raise Exception.new("Valid container names do not contain the '/' character: #{@fname}")
  end
  @fname = ':' + @fname  if @fname[0,1] != ':'
  super
  @lname = @fname
  @sname = @container
end

#printable_headersObject



55
56
57
# File 'lib/hpcloud/container_resource.rb', line 55

def printable_headers
  printable_container_headers
end

#remove(force, at = nil, after = nil) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/hpcloud/container_resource.rb', line 79

def remove(force, at=nil, after=nil)
  begin
    return false unless container_head()
    unless at.nil?
      @cstatus = CliStatus.new("The at option is only supported for objects.", :incorrect_usage)
      return false
    end
    unless after.nil?
      @cstatus = CliStatus.new("The after option is only supported for objects.", :incorrect_usage)
      return false
    end

    if force == true
      foreach { |x| x.remove(force) }
    end
    begin
      @storage.delete_container(@container)
    rescue Excon::Errors::Conflict
      @cstatus = CliStatus.new("The container '#{@fname}' is not empty. Please use -f option to force deleting a container with objects in it.", :conflicted)
      return false
    end
  rescue Excon::Errors::Forbidden => error
    @cstatus = CliStatus.new("Permission denied for '#{@fname}.", :permission_denied)
    return false
  rescue Exception => e
    @cstatus = CliStatus.new("Exception removing '#{@fname}': " + e.to_s, :general_error)
    return false
  end
  return true
end

#revoke(acl) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/hpcloud/container_resource.rb', line 128

def revoke(acl)
  begin
    return false unless container_head()

    @readacl.revoke(acl.readers)
    @writeacl.revoke(acl.writers)
    return save()
  rescue Exception => e
    @cstatus = CliStatus.new("Exception revoking permissions for '#{@fname}': " + e.to_s, :general_error)
    return false
  end
end

#saveObject



159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/hpcloud/container_resource.rb', line 159

def save
  options = {}
  options['X-Container-Sync-Key'] = @synckey unless @synckey.nil?
  options['X-Container-Sync-To'] = @syncto unless @syncto.nil?
  options.merge!(@readacl.to_hash)
  options.merge!(@writeacl.to_hash)
  begin
    @storage.put_container(@container, options)
  rescue Excon::Errors::BadRequest => error
    @@error = ErrorResponse.new(error).to_s
    error_status = :incorrect_usage
  end
  return true
end

#set_metadata(key, value) ⇒ Object



65
66
67
68
69
# File 'lib/hpcloud/container_resource.rb', line 65

def (key, value)
  hsh = printable_headers()
  hsh[key] = value
  @storage.post_container(@container, hsh)
end

#sync(synckey, syncto) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/hpcloud/container_resource.rb', line 141

def sync(synckey, syncto)
  return false unless container_head()
  @synckey = synckey
  @syncto = syncto
  unless syncto.nil?
    unless syncto.start_with?("https://") || syncto.start_with?("http://")
      resource = ResourceFactory.create(Connection.instance.storage, syncto)
      if resource.head
        @syncto = resource.public_url
      else
        @cstatus = resource.cstatus
        return false
      end
    end
  end
  return save
end

#tempurl(period) ⇒ Object



110
111
112
113
# File 'lib/hpcloud/container_resource.rb', line 110

def tempurl(period)
  @cstatus = CliStatus.new("Temporary URLs not supported on containers ':#{@container}'.", :incorrect_usage)
  return nil
end

#valid_metadata_key?(key) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
62
63
# File 'lib/hpcloud/container_resource.rb', line 59

def (key)
  return true if key.start_with?("X-Container-Meta-")
  return true unless CONTAINER_META.index(key).nil?
  return false
end

#valid_virtualhost?Boolean

Returns:

  • (Boolean)


39
40
41
42
43
44
45
46
47
48
49
# File 'lib/hpcloud/container_resource.rb', line 39

def valid_virtualhost?
  return false if @container.nil?
  if (1..63).include?(@container.length)
    if @container =~ /^[a-z0-9-]*$/
      if @container[0,1] != '-' and @container[-1,1] != '-'
        return true 
      end
    end
  end
  false
end