Class: OpenStack::Swift::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/openstack/swift/connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection) ⇒ Connection

Returns a new instance of Connection.



11
12
13
14
# File 'lib/openstack/swift/connection.rb', line 11

def initialize(connection)
  @connection = connection
  OpenStack::Authentication.init(@connection)
end

Instance Attribute Details

#connectionObject

Returns the value of attribute connection.



9
10
11
# File 'lib/openstack/swift/connection.rb', line 9

def connection
  @connection
end

Instance Method Details

#authok?Boolean

Returns true if the authentication was successful and returns false otherwise.

cf.authok?
=> true

Returns:

  • (Boolean)


20
21
22
# File 'lib/openstack/swift/connection.rb', line 20

def authok?
  @connection.authok
end

#bytesObject

The total size in bytes under this connection



55
56
57
# File 'lib/openstack/swift/connection.rb', line 55

def bytes
  get_info[:bytes]
end

#container(name) ⇒ Object Also known as: get_container

Returns an OpenStack::Swift::Container object that can be manipulated easily. Throws a OpenStack::Exception::ItemNotFound if the container doesn’t exist.

container = cf.container('test')
container.count
=> 2
container = cf.container("no_such_container")
=> OpenStack::Exception::ItemNotFound: The resource could not be found


33
34
35
# File 'lib/openstack/swift/connection.rb', line 33

def container(name)
  OpenStack::Swift::Container.new(self, name)
end

#container_exists?(containername) ⇒ Boolean

Returns true if the requested container exists and returns false otherwise.

cf.container_exists?('good_container')
=> true

cf.container_exists?('bad_container')
=> false

Returns:

  • (Boolean)


105
106
107
108
109
110
111
112
113
# File 'lib/openstack/swift/connection.rb', line 105

def container_exists?(containername)
  path = "/#{URI.encode(containername.to_s)}"
  begin
    response = @connection.req("HEAD", path)
  rescue OpenStack::Exception::ItemNotFound
    return false
  end
  true
end

#containers(limit = nil, marker = nil) ⇒ Object Also known as: list_containers

Gathers a list of the containers that exist for the account and returns the list of container names as an array. If no containers exist, an empty array is returned.

If you supply the optional limit and marker parameters, the call will return the number of containers specified in limit, starting after the object named in marker.

cf.containers
=> ["backup", "Books", "cftest", "test", "video", "webpics"]

cf.containers(2,'cftest')
=> ["test", "video"]


75
76
77
78
79
# File 'lib/openstack/swift/connection.rb', line 75

def containers(limit = nil, marker = nil)
  path = OpenStack.get_query_params({:limit=>limit, :marker=>marker, :format=>'json'}, [:limit, :marker, :format], "")
  response = @connection.req("GET", URI.encode(path))
  OpenStack.symbolize_keys(JSON.parse(response.body)).inject([]){|res,cur| res << cur[:name]; res }
end

#containers_detail(limit = nil, marker = nil) ⇒ Object Also known as: list_containers_info

Retrieves a list of containers on the account along with their sizes (in bytes) and counts of the objects held within them. If no containers exist, an empty hash is returned.

If you supply the optional limit and marker parameters, the call will return the number of containers specified in limit, starting after the object named in marker.

cf.containers_detail
=> { "container1" => { :bytes => "36543", :count => "146" },
     "container2" => { :bytes => "105943", :count => "25" } }


91
92
93
94
95
# File 'lib/openstack/swift/connection.rb', line 91

def containers_detail(limit = nil, marker = nil)
  path = OpenStack.get_query_params({:limit=>limit, :marker=>marker, :format=>'json'}, [:limit, :marker, :format], "")
  response = @connection.req("GET", URI.encode(path))
  OpenStack.symbolize_keys(JSON.parse(response.body)).inject({}){|res,current| res.merge!({current[:name]=>{:bytes=>current[:bytes].to_s,:count=>current[:count].to_s}}) ; res }
end

#countObject

The total number of containers under this connection



60
61
62
# File 'lib/openstack/swift/connection.rb', line 60

def count
  get_info[:count]
end

#create_container(containername) ⇒ Object

Creates a new container and returns the OpenStack::Swift::Container object.

“/” is not valid in a container name. The container name is limited to 256 characters or less.

container = cf.create_container('new_container')
container.name
=> "new_container"

container = cf.create_container('bad/name')
=> OpenStack::Exception::InvalidArgument: Container name cannot contain '/'


126
127
128
129
130
131
132
# File 'lib/openstack/swift/connection.rb', line 126

def create_container(containername)
  raise OpenStack::Exception::InvalidArgument.new("Container name cannot contain '/'") if containername.match("/")
  raise OpenStack::Exception::InvalidArgument.new("Container name is limited to 256 characters") if containername.length > 256
  path = "/#{URI.encode(containername.to_s)}"
  @connection.req("PUT", path, {:headers=>{"Content-Length"=>"0"}})
  OpenStack::Swift::Container.new(self, containername)
end

#delete_container(containername) ⇒ Object

Deletes a container from the account. Throws a OpenStack::Exception::ResourceStateConflict if the container still contains objects. Throws a OpenStack::Exception::ItemNotFound if the container doesn’t exist.

cf.delete_container('new_container')
=> true

cf.delete_container('video')
=> OpenStack::Exception::ResourceStateConflict: The container: "video" is not empty. There was a conflict with the state of the resource

cf.delete_container('nonexistent')
=> OpenStack::Exception::ItemNotFound: The container: "nonexistant" does not exist. The resource could not be found


147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/openstack/swift/connection.rb', line 147

def delete_container(containername)
  path = "/#{URI.encode(containername.to_s)}"
  begin
    @connection.req("DELETE", path)
  rescue OpenStack::Exception::ResourceStateConflict => conflict
    msg = "The container: \"#{containername}\" is not empty. #{conflict.message}"
    raise OpenStack::Exception::ResourceStateConflict.new(msg, conflict.response_code, conflict.response_body)
  rescue OpenStack::Exception::ItemNotFound => not_found
    msg = "The container: \"#{containername}\" does not exist. #{not_found.message}"
    raise OpenStack::Exception::ItemNotFound.new(msg, not_found.response_code, not_found.response_body)
  end
  true
end

#get_infoObject

Sets instance variables for the bytes of storage used for this account/connection, as well as the number of containers stored under the account. Returns a hash with :bytes and :count keys, and also sets the instance variables.

cf.get_info
=> {:count=>8, :bytes=>42438527}
cf.bytes
=> 42438527

Hostname of the storage server



46
47
48
49
50
51
52
# File 'lib/openstack/swift/connection.rb', line 46

def get_info
    raise OpenStack::Exception::Authentication, "Not authenticated" unless authok?
    response = @connection.req("HEAD", "")
    @bytes = response["x-account-bytes-used"].to_i
    @count = response["x-account-container-count"].to_i
    {:bytes => @bytes, :count => @count}
end