class CloudDB::Connection

Attributes

auth_url[R]
authkey[R]
authok[RW]
authtoken[RW]
authuser[R]
dbmgmthost[RW]
dbmgmtpath[RW]
dbmgmtport[RW]
dbmgmtscheme[RW]
region[R]

Public Class Methods

new(options = {:retry_auth => true}) click to toggle source

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 options 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)
# File lib/clouddb/connection.rb, line 32
def initialize(options = {:retry_auth => true})
  @authuser = options[:username] || (raise CloudDB::Exception::Authentication, "Must supply a :username")
  @authkey = options[:api_key] || (raise CloudDB::Exception::Authentication, "Must supply an :api_key")
  @region = options[:region] || (raise CloudDB::Exception::Authentication, "Must supply a :region")
  @retry_auth = options[:retry_auth]
  @auth_url = options[:auth_url] || CloudDB::AUTH_USA
  @snet = ENV['RACKSPACE_SERVICENET'] || options[:snet]
  @authok = false
  @http = {}
  CloudDB::Authentication.new(self)
end

Public Instance Methods

authok?() click to toggle source

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

Example:

dbaas.authok?
=> true
# File lib/clouddb/connection.rb, line 49
def authok?
  @authok
end
create_instance(options = {}) click to toggle source

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"}]}
                                    ]
                         )
# File lib/clouddb/connection.rb, line 114
def create_instance(options = {})
  body = Hash.new
  body[:instance] = Hash.new

  body[:instance][:flavorRef]  = options[:flavor_ref] or raise CloudDB::Exception::MissingArgument, "Must provide a flavor to create an instance"
  body[:instance][:name]       = options[:name] or raise CloudDB::Exception::MissingArgument, "Must provide a name to create an instance"
  body[:instance][:volume]     = options[:volume] or raise CloudDB::Exception::MissingArgument, "Must provide a size to create an instance"
  body[:instance][:databases]  = options[:databases] if options[:databases]
  body[:instance][:users]      = options[:users] if options[:users]
  (raise CloudDB::Exception::Syntax, "Instance name must be 128 characters or less") if options[: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
flavor(id)
Alias for: get_flavor
flavors()
Alias for: list_flavors
flavors_detail()
Alias for: list_flavors_detail
get_flavor(id) click to toggle source

Returns a CloudDB::Flavor object for the given flavor id number.

Example:

dbaas.get_flavor(3)
# File lib/clouddb/connection.rb, line 165
def get_flavor(id)
  CloudDB::Flavor.new(self,id)
end
Also aliased as: flavor
get_instance(id) click to toggle source

Returns a CloudDB::Instance object for the given instance ID number.

Example:

dbaas.get_instance(692d8418-7a8f-47f1-8060-59846c6e024f)
# File lib/clouddb/connection.rb, line 90
def get_instance(id)
  CloudDB::Instance.new(self,id)
end
Also aliased as: instance
instance(id)
Alias for: get_instance
instances()
Alias for: list_instances
instances_detail()
list_flavors() click to toggle source

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
# 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
Also aliased as: flavors
list_flavors_detail() click to toggle source

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
# 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
Also aliased as: flavors_detail
list_instances() click to toggle source

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).
# 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
Also aliased as: instances
list_instances_detail() click to toggle source

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.
# 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
Also aliased as: instances_detail