Class: Xnode::Keystone::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::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.



37
38
39
40
41
42
# File 'lib/xnode/keystone.rb', line 37

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



33
34
35
# File 'lib/xnode/keystone.rb', line 33

def baseurl
  @baseurl
end

#passwordObject

Set accessors for credentials



33
34
35
# File 'lib/xnode/keystone.rb', line 33

def password
  @password
end

#tenantObject

Set accessors for credentials



33
34
35
# File 'lib/xnode/keystone.rb', line 33

def tenant
  @tenant
end

#urlObject

Set accessors for credentials



33
34
35
# File 'lib/xnode/keystone.rb', line 33

def url
  @url
end

#usernameObject

Set accessors for credentials



33
34
35
# File 'lib/xnode/keystone.rb', line 33

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.



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/xnode/keystone.rb', line 60

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.



54
55
56
# File 'lib/xnode/keystone.rb', line 54

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.



47
48
49
50
51
# File 'lib/xnode/keystone.rb', line 47

def request
  url = @baseurl + '/tokens'
  data = Xnode::Keystone.auth_data(@tenant, @username, @password).to_json
  response  = Xnode::Keystone.post(url, data)
end

#tenants(baseurl, token) ⇒ Object

Fetches a list of tenants from the platform and returns the data as a Hash



73
74
75
76
# File 'lib/xnode/keystone.rb', line 73

def tenants(baseurl, token)
  url = baseurl + '/tenants'
  response = Xnode::Keystone.get(url, token)
end