class OpenStack::Connection

Attributes

auth_host[R]
auth_method[R]
auth_path[R]
auth_port[R]
auth_scheme[R]
authkey[R]
authok[RW]
authtenant[R]
authtoken[RW]
authuser[R]
http[R]
is_debug[R]
proxy_host[R]
proxy_port[R]
region[R]
regions_list[R]
service_host[RW]
service_name[R]
service_path[RW]
service_port[RW]
service_scheme[RW]
service_type[R]

Public Class Methods

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

Creates and returns a new Connection object, depending on the #service_type passed in the options:

e.g: os = ::create({:username => “herp@derp.com”, :api_key=>“password”,

:auth_url => "https://region-a.geo-1.identity.cloudsvc.com:35357/v2.0/",
:authtenant=>"herp@derp.com-default-tenant", :service_type=>"object-store")

Will return an OpenStack::Swift::Connection object.

options hash:

:auth_method - Type of authentication - 'password', 'key', 'rax-kskey' - defaults to 'password'
:username - Your OpenStack username or public key, depending on auth_method. *required*
:authtenant_name OR :authtenant_id - Your OpenStack tenant name or id *required*. Defaults to username.
  passing :authtenant will default to using that parameter as tenant name.
:api_key - Your OpenStack API key *required* (either private key or password, depending on auth_method)
:auth_url - Configurable auth_url endpoint.
:service_name - (Optional for v2.0 auth only). The optional name of the compute service to use.
:service_type - (Optional for v2.0 auth only). Defaults to "compute"
:region - (Optional for v2.0 auth only). The specific service region to use. Defaults to first returned region.
:retry_auth - Whether to retry if your auth token expires (defaults to true)
:proxy_host - If you need to connect through a proxy, supply the hostname here
:proxy_port - If you need to connect through a proxy, supply the port here

The options hash is used to create a new OpenStack::Connection object (private constructor) and this is passed to the constructor of OpenStack::Compute::Connection or OpenStack::Swift::Connection (depending on :service_type) where authentication is done using OpenStack::Authentication.

# File lib/openstack/connection.rb, line 59
def self.create(options = {:retry_auth => true})
  #call private constructor and grab instance vars
  connection = new(options)
  case connection.service_type
    when "compute"
      OpenStack::Compute::Connection.new(connection)
    when "object-store"
      OpenStack::Swift::Connection.new(connection)
    when "volume"
      OpenStack::Volume::Connection.new(connection)
    when "image"
      OpenStack::Image::Connection.new(connection)
   else
      raise Exception::InvalidArgument, "Invalid :service_type parameter: #{@service_type}"
  end
end
new(options = {:retry_auth => true}) click to toggle source
# File lib/openstack/connection.rb, line 78
def initialize(options = {:retry_auth => true})
  @authuser = options[:username] || (raise Exception::MissingArgument, "Must supply a :username")
  @authkey = options[:api_key] || (raise Exception::MissingArgument, "Must supply an :api_key")
  @auth_url = options[:auth_url] || (raise Exception::MissingArgument, "Must supply an :auth_url")
  @authtenant = (options[:authtenant_id])? {:type => "tenantId", :value=>options[:authtenant_id]} : {:type=>"tenantName", :value=>(options[:authtenant_name] || options[:authtenant] || @authuser)}
  @auth_method = options[:auth_method] || "password"
  @service_name = options[:service_name] || nil
  @service_type = options[:service_type] || "compute"
  @region = options[:region] || @region = nil
  @regions_list = {} # this is populated during authentication - from the returned service catalogue
  @is_debug = options[:is_debug]
  auth_uri=nil
  begin
    auth_uri=URI.parse(@auth_url)
  rescue Exception => e
    raise Exception::InvalidArgument, "Invalid :auth_url parameter: #{e.message}"
  end
  raise Exception::InvalidArgument, "Invalid :auth_url parameter." if auth_uri.nil? or auth_uri.host.nil?
  @auth_host = auth_uri.host
  @auth_port = auth_uri.port
  @auth_scheme = auth_uri.scheme
  @auth_path = auth_uri.path
  @retry_auth = options[:retry_auth]
  @proxy_host = options[:proxy_host]
  @proxy_port = options[:proxy_port]
  @authok = false
  @http = {}
end

Public Instance Methods

req(method, path, options = {}) click to toggle source

This is a much more sane way to make a http request to the api. Example: res = conn.req('GET', “/servers/#{id}”)

# File lib/openstack/connection.rb, line 183
def req(method, path, options = {})
  server   = options[:server]   || @service_host
  port     = options[:port]     || @service_port
  scheme   = options[:scheme]   || @service_scheme
  headers  = options[:headers]  || {'content-type' => 'application/json'}
  data     = options[:data]
  attempts = options[:attempts] || 0
  path = @service_path + path
  res = csreq(method,server,path,port,scheme,headers,data,attempts)
  if not res.code.match(/^20.$/)
    OpenStack::Exception.raise_exception(res)
  end
  return res
end