class Ropenstack::Keystone

Public Class Methods

new(address, port, token = nil) click to toggle source

Overide of the initialize function to allow users to provide keystone address and port outside of a URI which then gets passed on to the super function. Handles taking a token differently so that it lines up with the data format that keystone using. This is required for get_token() function.

# File lib/ropenstack/keystone.rb, line 18
def initialize(address, port, token = nil)
  super(URI.parse("http://" + address + ":" + port.to_s + "/v2.0"), token)
  unless token.nil?
    @data = { "access" => { "token" => { "id" => token } } } 
  end
end

Public Instance Methods

addEndpoint(region, service_id, publicurl, adminurl, internalurl) click to toggle source

Add an endpoint list

# File lib/ropenstack/keystone.rb, line 151
def addEndpoint(region, service_id, publicurl, adminurl, internalurl)
  data = {
    'endpoint' => {
      'region' => region,
      'service_id' => service_id,
      'publicurl' => publicurl,
      'adminurl' => adminurl,
      'internalurl' => internalurl
    }
  }
  return post_request(address("/endpoints"), data, token())
end
addToServices(name, type, description) click to toggle source

Add a service to the keystone services directory

# File lib/ropenstack/keystone.rb, line 130
def addToServices(name, type, description)
  data = {
    'OS-KSADM:service' => {
       'name' => name,
       'type' => type,
       'description' => description
    }
  }
  return post_request(address("/OS-KSADM/services"), data, token())
end
admin() click to toggle source

Returns true if a user is admin.

# File lib/ropenstack/keystone.rb, line 94
def admin()
  @data["access"]["user"]["roles"].each do |role|
    if role["name"].eql?("admin")
      return true
    end
  end
  return false
end
authenticate(username, password, token = nil, tenant = nil) click to toggle source

Authenticate via keystone, unless a token and tenant are defined then a unscoped token is returned with all associated data and stored in the @data variable. Does not return anything, but all data is accessible through accessor methods.

# File lib/ropenstack/keystone.rb, line 30
def authenticate(username, password, token = nil, tenant = nil)
  data = {
    "auth" => { 
      "passwordCredentials" => { 
        "username" => username, 
        "password" => password 
      } 
    } 
  }
  unless tenant.nil?
    data["auth"]["tenantName"] = tenant
  end
  @data = post_request(address("/tokens"), data, token)
end
getEndpoints() click to toggle source

Get the endpoint list

# File lib/ropenstack/keystone.rb, line 167
def getEndpoints()
  return get_request(address("/endpoints"), token())
end
getServices() click to toggle source

Get list of services

# File lib/ropenstack/keystone.rb, line 144
def getServices()
  return get_request(address("/OS-KSADM/services"), token())
end
raw() click to toggle source

Return the raw @data hash with all the data from authentication.

# File lib/ropenstack/keystone.rb, line 65
def raw()
  return @data              
end
scope_token(para1, para2 = nil, para3 = nil) click to toggle source

Scope token provides two ways to call it:

scope_token(tenantName) => Just using the current token and a tenantName it
                           scopes in the token. Token stays the same.
scope_token(username, password, tenantName) => This uses the username and 
                           password to reauthenticate with a tenant. The 
                           token changes.
# File lib/ropenstack/keystone.rb, line 53
def scope_token(para1, para2 = nil, para3 = nil)
  if ( para2.nil? )
    data = { "auth" => { "tenantName" => para1, "token" => { "id" => token() } } }
    @data = post_request(address("/tokens"), data, token())
  else 
    authenticate(para1, para2, token(), para3)                      
  end
end
services() click to toggle source

Get the service catalog returned by Keystone on authentication.

# File lib/ropenstack/keystone.rb, line 106
def services()
  return @data["access"]["serviceCatalog"]
end
tenant_id() click to toggle source

Get the tenant id from the @data

# File lib/ropenstack/keystone.rb, line 113
def tenant_id()
  return @data["access"]["token"]["tenant"]["id"]
end
tenant_list() click to toggle source

Separate Keystone Calls

# File lib/ropenstack/keystone.rb, line 123
def tenant_list()
  return get_request(address('/tenants'), token()) 
end
tenant_name() click to toggle source
# File lib/ropenstack/keystone.rb, line 117
def tenant_name()
  return @data["access"]["token"]["tenant"]["name"] 
end
token() click to toggle source

Gets the authentication token from the hash and returns it as a string.

# File lib/ropenstack/keystone.rb, line 72
def token() 
  return @data["access"]["token"]["id"] 
end
token_metadata() click to toggle source

This returns the token and all metadata associated with the token, including the tenant information.

# File lib/ropenstack/keystone.rb, line 80
def token_metadata()
  return @data["access"]["token"]
end
user() click to toggle source

Return the user hash from the authentication data

# File lib/ropenstack/keystone.rb, line 87
def user()
  return @data["access"]["user"]
end