Class: CloudDB::Connection
- Inherits:
-
Object
- Object
- CloudDB::Connection
- Defined in:
- lib/clouddb/connection.rb
Instance Attribute Summary collapse
-
#auth_url ⇒ Object
readonly
Returns the value of attribute auth_url.
-
#authkey ⇒ Object
readonly
Returns the value of attribute authkey.
-
#authok ⇒ Object
Returns the value of attribute authok.
-
#authtoken ⇒ Object
Returns the value of attribute authtoken.
-
#authuser ⇒ Object
readonly
Returns the value of attribute authuser.
-
#dbmgmthost ⇒ Object
Returns the value of attribute dbmgmthost.
-
#dbmgmtpath ⇒ Object
Returns the value of attribute dbmgmtpath.
-
#dbmgmtport ⇒ Object
Returns the value of attribute dbmgmtport.
-
#dbmgmtscheme ⇒ Object
Returns the value of attribute dbmgmtscheme.
-
#region ⇒ Object
readonly
Returns the value of attribute region.
Instance Method Summary collapse
-
#authok? ⇒ Boolean
Returns true if the authentication was successful and returns false otherwise.
-
#create_instance(options = {}) ⇒ Object
Creates a brand new database instance under your account.
-
#dbreq(method, server, path, port, scheme, headers = {}, data = nil, attempts = 0) ⇒ Object
This method actually makes the HTTP REST calls out to the server.
-
#get_flavor(id) ⇒ Object
(also: #flavor)
Returns a CloudDB::Flavor object for the given flavor id number.
-
#get_instance(id) ⇒ Object
(also: #instance)
Returns a CloudDB::Instance object for the given instance ID number.
-
#initialize(options = {:retry_auth => true}) ⇒ Connection
constructor
Creates a new CloudDB::Connection object.
-
#list_flavors ⇒ Object
(also: #flavors)
Returns the list of available database flavors.
-
#list_flavors_detail ⇒ Object
(also: #flavors_detail)
Returns the list of available database flavors in detail.
-
#list_instances ⇒ Object
(also: #instances)
Returns the list of available database instances.
-
#list_instances_detail ⇒ Object
(also: #instances_detail)
Returns the list of available database instances with detail.
Constructor Details
#initialize(options = {:retry_auth => true}) ⇒ Connection
Creates a new CloudDB::Connection object. Uses CloudDB::Authentication to perform the login for the connection.
Setting the retry_auth option to false will cause an exception to be thrown if your authorization token expires. Otherwise, it will attempt to re-authenticate.
This will likely be the base class for most operations.
The constructor takes a hash of options, including:
:username - Your Rackspace Cloud username *required*
:api_key - Your Rackspace Cloud API key *required*
:region - The region in which to manage database instances. Current are :dfw (Rackspace Dallas/Ft. Worth
Datacenter), :ord (Rackspace Chicago Datacenter) and :lon (Rackspace London Datacenter). *required*
:auth_url - The URL to use for authentication. (defaults to Rackspace USA).
:retry_auth - Whether to retry if your auth token expires (defaults to true)
Example:
dbaas = CloudDB::Connection.new(:username => 'YOUR_USERNAME', :api_key => 'YOUR_API_KEY', :region => :dfw)
32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/clouddb/connection.rb', line 32 def initialize( = {:retry_auth => true}) @authuser = [:username] || (raise CloudDB::Exception::Authentication, "Must supply a :username") @authkey = [:api_key] || (raise CloudDB::Exception::Authentication, "Must supply an :api_key") @region = [:region] || (raise CloudDB::Exception::Authentication, "Must supply a :region") @retry_auth = [:retry_auth] @auth_url = [:auth_url] || CloudDB::AUTH_USA @snet = ENV['RACKSPACE_SERVICENET'] || [:snet] @authok = false @http = {} CloudDB::Authentication.new(self) end |
Instance Attribute Details
#auth_url ⇒ Object (readonly)
Returns the value of attribute auth_url.
12 13 14 |
# File 'lib/clouddb/connection.rb', line 12 def auth_url @auth_url end |
#authkey ⇒ Object (readonly)
Returns the value of attribute authkey.
5 6 7 |
# File 'lib/clouddb/connection.rb', line 5 def authkey @authkey end |
#authok ⇒ Object
Returns the value of attribute authok.
7 8 9 |
# File 'lib/clouddb/connection.rb', line 7 def authok @authok end |
#authtoken ⇒ Object
Returns the value of attribute authtoken.
6 7 8 |
# File 'lib/clouddb/connection.rb', line 6 def authtoken @authtoken end |
#authuser ⇒ Object (readonly)
Returns the value of attribute authuser.
4 5 6 |
# File 'lib/clouddb/connection.rb', line 4 def authuser @authuser end |
#dbmgmthost ⇒ Object
Returns the value of attribute dbmgmthost.
8 9 10 |
# File 'lib/clouddb/connection.rb', line 8 def dbmgmthost @dbmgmthost end |
#dbmgmtpath ⇒ Object
Returns the value of attribute dbmgmtpath.
9 10 11 |
# File 'lib/clouddb/connection.rb', line 9 def dbmgmtpath @dbmgmtpath end |
#dbmgmtport ⇒ Object
Returns the value of attribute dbmgmtport.
10 11 12 |
# File 'lib/clouddb/connection.rb', line 10 def dbmgmtport @dbmgmtport end |
#dbmgmtscheme ⇒ Object
Returns the value of attribute dbmgmtscheme.
11 12 13 |
# File 'lib/clouddb/connection.rb', line 11 def dbmgmtscheme @dbmgmtscheme end |
#region ⇒ Object (readonly)
Returns the value of attribute region.
13 14 15 |
# File 'lib/clouddb/connection.rb', line 13 def region @region end |
Instance Method Details
#authok? ⇒ Boolean
Returns true if the authentication was successful and returns false otherwise.
Example:
dbaas.authok?
=> true
49 50 51 |
# File 'lib/clouddb/connection.rb', line 49 def authok? @authok end |
#create_instance(options = {}) ⇒ Object
Creates a brand new database instance under your account.
Options:
:flavor_ref - reference to a flavor as specified in the response from the List Flavors API call. *required*
:name - the name of the database instance. Limited to 128 characters or less. *required*
:size - specifies the volume size in gigabytes (GB). The value specified must be between 1 and 10. *required*
:databases - the databases to be created for the instance.
:users - the users to be created for the instance.
Example:
i = dbaas.create_instance(:flavor_ref => "https://ord.databases.api.rackspacecloud.com/v1.0/1234/flavors/1",
:name => "test_instance",
:volume => {:size => "1"},
:databases => [{:name => "testdb"}],
:users => [{:name => "test",
:password => "test",
:databases => [{:name => "testdb"}]}
]
)
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/clouddb/connection.rb', line 114 def create_instance( = {}) body = Hash.new body[:instance] = Hash.new body[:instance][:flavorRef] = [:flavor_ref] or raise CloudDB::Exception::MissingArgument, "Must provide a flavor to create an instance" body[:instance][:name] = [:name] or raise CloudDB::Exception::MissingArgument, "Must provide a name to create an instance" body[:instance][:volume] = [:volume] or raise CloudDB::Exception::MissingArgument, "Must provide a size to create an instance" body[:instance][:databases] = [:databases] if [:databases] body[:instance][:users] = [:users] if [:users] (raise CloudDB::Exception::Syntax, "Instance name must be 128 characters or less") if [:name].size > 128 response = dbreq("POST", dbmgmthost, "#{dbmgmtpath}/instances", dbmgmtport, dbmgmtscheme, {}, body.to_json) CloudDB::Exception.raise_exception(response) unless response.code.to_s.match(/^20.$/) body = JSON.parse(response.body)['instance'] return get_instance(body["id"]) end |
#dbreq(method, server, path, port, scheme, headers = {}, data = nil, attempts = 0) ⇒ Object
This method actually makes the HTTP REST calls out to the server. Relies on the thread-safe typhoeus gem to do the heavy lifting. Never called directly.
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/clouddb/connection.rb', line 172 def dbreq(method, server, path, port, scheme, headers = {}, data = nil, attempts = 0) # :nodoc: if data unless data.is_a?(IO) headers['Content-Length'] = data.respond_to?(:lstat) ? data.stat.size : data.size end else headers['Content-Length'] = 0 end hdrhash = headerprep(headers) url = "#{scheme}://#{server}#{path}" print "DEBUG: Data is #{data}\n" if (data && ENV['DATABASES_VERBOSE']) request = Typhoeus::Request.new(url, :body => data, :method => method.downcase.to_sym, :headers => hdrhash, :verbose => ENV['DATABASES_VERBOSE'] ? true : false) CloudDB.hydra.queue(request) CloudDB.hydra.run response = request.response print "DEBUG: Body is #{response.body}\n" if ENV['DATABASES_VERBOSE'] raise CloudDB::Exception::ExpiredAuthToken if response.code.to_s == "401" response rescue Errno::EPIPE, Errno::EINVAL, EOFError # Server closed the connection, retry raise CloudDB::Exception::Connection, "Unable to reconnect to #{server} after #{attempts} attempts" if attempts >= 5 attempts += 1 @http[server].finish if @http[server].started? start_http(server,path,port,scheme,headers) retry rescue CloudDB::Exception::ExpiredAuthToken raise CloudDB::Exception::Connection, "Authentication token expired and you have requested not to retry" if @retry_auth == false CloudDB::Authentication.new(self) retry end |
#get_flavor(id) ⇒ Object Also known as: flavor
Returns a CloudDB::Flavor object for the given flavor id number.
Example:
dbaas.get_flavor(3)
165 166 167 |
# File 'lib/clouddb/connection.rb', line 165 def get_flavor(id) CloudDB::Flavor.new(self,id) end |
#get_instance(id) ⇒ Object Also known as: instance
Returns a CloudDB::Instance object for the given instance ID number.
Example:
dbaas.get_instance(692d8418-7a8f-47f1-8060-59846c6e024f)
90 91 92 |
# File 'lib/clouddb/connection.rb', line 90 def get_instance(id) CloudDB::Instance.new(self,id) end |
#list_flavors ⇒ Object Also known as: flavors
Returns the list of available database flavors.
Information returned includes:
:id - The numeric id of this flavor
:name - The name of the flavor
:links - Useful information regarding the flavor
137 138 139 140 141 142 |
# File 'lib/clouddb/connection.rb', line 137 def list_flavors() response = dbreq("GET", dbmgmthost, "#{dbmgmtpath}/flavors", dbmgmtport, dbmgmtscheme) CloudDB::Exception.raise_exception(response) unless response.code.to_s.match(/^20.$/) flavors = CloudDB.symbolize_keys(JSON.parse(response.body)["flavors"]) return flavors end |
#list_flavors_detail ⇒ Object Also known as: flavors_detail
Returns the list of available database flavors in detail.
Information returned includes:
:id - The numeric id of this flavor
:name - The name of the flavor
:vcpus - The amount of virtual cpu power
:ram - The available memory in MB
:links - Useful information regarding the flavor
153 154 155 156 157 158 |
# File 'lib/clouddb/connection.rb', line 153 def list_flavors_detail() response = dbreq("GET", dbmgmthost, "#{dbmgmtpath}/flavors/detail", dbmgmtport, dbmgmtscheme) CloudDB::Exception.raise_exception(response) unless response.code.to_s.match(/^20.$/) flavors = CloudDB.symbolize_keys(JSON.parse(response.body)["flavors"]) return flavors end |
#list_instances ⇒ Object Also known as: instances
Returns the list of available database instances.
Information returned includes:
:id - The numeric id of the instance.
:name - The name of the instance.
:status - The current state of the instance (BUILD, ACTIVE, BLOCKED, RESIZE, SHUTDOWN, FAILED).
59 60 61 62 63 64 |
# File 'lib/clouddb/connection.rb', line 59 def list_instances() response = dbreq("GET", dbmgmthost, "#{dbmgmtpath}/instances", dbmgmtport, dbmgmtscheme) CloudDB::Exception.raise_exception(response) unless response.code.to_s.match(/^20.$/) instances = CloudDB.symbolize_keys(JSON.parse(response.body)["instances"]) return instances end |
#list_instances_detail ⇒ Object Also known as: instances_detail
Returns the list of available database instances with detail.
Information returned includes:
:id - The numeric ID of the instance.
:name - The name of the instance.
:status - The current state of the instance (BUILD, ACTIVE, BLOCKED, RESIZE, SHUTDOWN, FAILED).
:hostname - A DNS-resolvable hostname associated with the database instance.
:flavor - The flavor of the instance.
:volume - The volume size of the instance.
:created - The time when the instance was created.
:updated - The time when the instance was last updated.
78 79 80 81 82 83 |
# File 'lib/clouddb/connection.rb', line 78 def list_instances_detail() response = dbreq("GET", dbmgmthost, "#{dbmgmtpath}/instances/detail", dbmgmtport, dbmgmtscheme) CloudDB::Exception.raise_exception(response) unless response.code.to_s.match(/^20.$/) instances = CloudDB.symbolize_keys(JSON.parse(response.body)["instances"]) return instances end |