Class: Xnode::Keystone::V2::Authenticate

Inherits:
Object
  • Object
show all
Defined in:
lib/xnode/keystone.rb

Overview

This class sets up an authenticated session with the OpenStack API’s via Keystone, and exposes methods for interacting the Identity service.

Example

> auth = Xnode::Keystone::V2::Authenticate.new

> request = auth.request

> token = auth.get_token(request)

> catalog = auth.catalog(request)

> keystone_endpoint = catalog[‘public’]

> tenants = auth.tenants(keystone_endpoint, token)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAuthenticate

Authenticates against the OpenStack Identity API endpoint on 5000/tcp or 35357/tcp via HTTP/S. Uses environment variables to set your authentication credentials.



40
41
42
43
44
45
# File 'lib/xnode/keystone.rb', line 40

def initialize
  @username  = ENV['OS_USERNAME']
  @password  = ENV['OS_PASSWORD']
  @tenant    = ENV['OS_TENANT_NAME']
  @baseurl   = ENV['OS_AUTH_URL']
end

Instance Attribute Details

#baseurlObject

Set accessors for credentials



36
37
38
# File 'lib/xnode/keystone.rb', line 36

def baseurl
  @baseurl
end

#passwordObject

Set accessors for credentials



36
37
38
# File 'lib/xnode/keystone.rb', line 36

def password
  @password
end

#tenantObject

Set accessors for credentials



36
37
38
# File 'lib/xnode/keystone.rb', line 36

def tenant
  @tenant
end

#urlObject

Set accessors for credentials



36
37
38
# File 'lib/xnode/keystone.rb', line 36

def url
  @url
end

#usernameObject

Set accessors for credentials



36
37
38
# File 'lib/xnode/keystone.rb', line 36

def username
  @username
end

Instance Method Details

#catalog(data) ⇒ Object

Parses the serviceCatalog returned within the response, and writes it into a modified Hash that makes it easy to extract public, internal or admin URI’s by service type.



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/xnode/keystone.rb', line 64

def catalog(data)
  mapendpoints = Hash.new
  data['access']['serviceCatalog'].each do |key, value|
    mapendpoints[key['type']] = {
      'public' => key['endpoints'][0]['publicURL'],
      'internal' => key['endpoints'][0]['internalURL'],
      'admin' => key['endpoints'][0]['adminURL']
    }
  end
  return mapendpoints
end

#get_token(data) ⇒ Object

Extracts the authentication token from the response, returning it as a String.



58
59
60
# File 'lib/xnode/keystone.rb', line 58

def get_token(data)
  data['access']['token']['id']
end

#requestObject

Uses a private post method (keystone/http.rb) to send an HTTP POST request to OpenStack. The JSON response is automatically decoded into a Ruby Hash for later manipulation. The response body remains untouched otherwise, so everything you’d expect to find in the response is within the Hash.



50
51
52
53
54
55
# File 'lib/xnode/keystone.rb', line 50

def request
  url = "#{@baseurl}/tokens"
  data = Xnode::Keystone.auth_data(@tenant, @username, @password).to_json
  request   = RestClient.post "#{url}", data, :content_type => :json, :accept => :json
  response  = JSON.parse(request)
end