class Keystone::V2_0::Client

Attributes

auth_url[RW]
endpoint_manager[RW]
password[RW]
role_manager[RW]
service_manager[RW]
tenant_manager[RW]
tenant_name[RW]
user_manager[RW]

query providers

username[RW]

client specific attributes

Public Class Methods

new(username, password, tenant_name, auth_url) click to toggle source

create a new Keystone client instance with the given credentials

# File lib/keystone/v2_0/client.rb, line 27
def initialize(username, password, tenant_name, auth_url)
  # initialize the object
  self.username    = username
  self.password    = password
  self.tenant_name = tenant_name
  self.auth_url    = auth_url

  # create the initial query managers
  self.user_manager     = Keystone::V2_0::Manager::User.new     auth_url
  self.role_manager     = Keystone::V2_0::Manager::Role.new     auth_url
  self.tenant_manager   = Keystone::V2_0::Manager::Tenant.new   auth_url
  self.service_manager  = Keystone::V2_0::Manager::Service.new  auth_url
  self.endpoint_manager = Keystone::V2_0::Manager::Endpoint.new auth_url

  # create the manager methods through which queries will be performed
  # using meta-programming to ensure DRY principle is followed
  [ "users", "roles", "tenants", "services", "endpoints" ].each do |query|
    singular_method = query.sub(/s$/, '')
    self.class.send(:define_method, "#{singular_method}_interface") do
      unless (token = get_token).nil?
        self.send("#{singular_method}_manager").token = token
        return self.send("#{singular_method}_manager")
      else
        raise "An exception has occurred attempting to invoke '#{query}'"
      end
    end
  end
end

Private Instance Methods

get_token() click to toggle source

obtain a token for the action being performed

# File lib/keystone/v2_0/client.rb, line 59
def get_token
  options                           = {}
  options[:url]                     = self.auth_url + "/tokens"
  options[:method]                  = :post
  options[:headers]                 = {}
  options[:headers]["User-Agent"]   = "keystone-client"
  options[:headers]["Accept"]       = "application/json"
  options[:headers]["Content-Type"] = "application/json"
  options[:payload]                 = { "auth" =>
                                        { "tenantName" => self.tenant_name,
                                          "passwordCredentials" =>
                                          {
                                            "username" => self.username,
                                            "password" => self.password
                                          }
                                        }
                                      }.to_json

  # provide a block to ensure the response is parseable rather than
  # having RestClient throw an exception
  RestClient::Request.execute(options) do |response, request, result|
    if response and response.code == 200
      return JSON.parse(response.body)["access"]["token"]["id"]
    else
      return nil
    end
  end
end