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



99
100
101
102
103
104
# File 'lib/xnode/keystone.rb', line 99

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

Instance Attribute Details

#baseurlObject

Set accessors for credentials



95
96
97
# File 'lib/xnode/keystone.rb', line 95

def baseurl
  @baseurl
end

#passwordObject

Set accessors for credentials



95
96
97
# File 'lib/xnode/keystone.rb', line 95

def password
  @password
end

#tenantObject

Set accessors for credentials



95
96
97
# File 'lib/xnode/keystone.rb', line 95

def tenant
  @tenant
end

#tokenObject

Set accessors for credentials



95
96
97
# File 'lib/xnode/keystone.rb', line 95

def token
  @token
end

#usernameObject

Set accessors for credentials



95
96
97
# File 'lib/xnode/keystone.rb', line 95

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.



126
127
128
129
130
131
132
133
134
135
# File 'lib/xnode/keystone.rb', line 126

def catalog(data)
  mapendpoints = Hash.new
  data['token']['catalog'].each do |key, value|
    mapendpoints[key['type']] = Hash.new
    key['endpoints'].each do |k, v|
      mapendpoints[key['type']][k['interface']] = "#{k['url']}"
    end
  end
  return mapendpoints
end

#get_tokenObject

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



120
121
122
# File 'lib/xnode/keystone.rb', line 120

def get_token
  @token
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.



109
110
111
112
113
114
115
116
117
# File 'lib/xnode/keystone.rb', line 109

def request
  url = "#{@baseurl}/auth/tokens"
  data = Xnode::Keystone.auth_data_v3(@username, @password).to_json
  # TODO: fix this so that the generic :post method can be used.
  response = RestClient.post "#{url}", data, :content_type => :json, :accept => :json
  @token = response.headers[:x_subject_token]

  return JSON.parse(response.body)
end