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)

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.



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

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



30
31
32
# File 'lib/xnode/keystone.rb', line 30

def baseurl
  @baseurl
end

#passwordObject

Set accessors for credentials



30
31
32
# File 'lib/xnode/keystone.rb', line 30

def password
  @password
end

#tenantObject

Set accessors for credentials



30
31
32
# File 'lib/xnode/keystone.rb', line 30

def tenant
  @tenant
end

#urlObject

Set accessors for credentials



30
31
32
# File 'lib/xnode/keystone.rb', line 30

def url
  @url
end

#usernameObject

Set accessors for credentials



30
31
32
# File 'lib/xnode/keystone.rb', line 30

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.



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/xnode/keystone.rb', line 57

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.



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

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.



44
45
46
47
48
# File 'lib/xnode/keystone.rb', line 44

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