Module: Ropenstack::Identity::Version2

Defined in:
lib/ropenstack/identity/v2.rb

Overview

  • Name: IdentityVersion2

  • Description: An implementation of the V2.0 Identity API Client in Ruby

  • Author: Sam ‘Tehsmash’ Betts, John Davidge

  • Date: 30/06/2014

Instance Method Summary collapse

Instance Method Details

#add_endpoint(region, service_id, publicurl, adminurl, internalurl) ⇒ Object

Add an endpoint list



146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/ropenstack/identity/v2.rb', line 146

def add_endpoint(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

#add_to_services(name, type, description) ⇒ Object

Add a service to the keystone services directory



125
126
127
128
129
130
131
132
133
134
# File 'lib/ropenstack/identity/v2.rb', line 125

def add_to_services(name, type, description)
        data = {
                'OS-KSADM:service' => {
                         'name' => name,
                         'type' => type,
                         'description' => description
                }
        }
        return post_request(address("/OS-KSADM/services"), data, token())
end

#address(endpoint) ⇒ Object



170
171
172
# File 'lib/ropenstack/identity/v2.rb', line 170

def address(endpoint)
  super("/v2.0/" + endpoint)
end

#adminObject

Returns true if a user is admin.



89
90
91
92
93
94
95
96
# File 'lib/ropenstack/identity/v2.rb', line 89

def admin()
        @data["access"]["user"]["roles"].each do |role|
                if role["name"].eql?("admin")
                        return true
                end
        end
        return false
end

#authenticate(username, password, tenant = nil) ⇒ Object

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.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ropenstack/identity/v2.rb', line 14

def authenticate(username, password, 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

#get_endpoints(token = nil) ⇒ Object

Get the endpoint list



162
163
164
165
166
167
168
# File 'lib/ropenstack/identity/v2.rb', line 162

def get_endpoints(token = nil)
        if token.nil?
                return get_request(address("/endpoints"), token())
        else
                return get_request(address("/tokens/#{token}/endpoints"), token())
        end
end

#get_servicesObject

Get list of services



139
140
141
# File 'lib/ropenstack/identity/v2.rb', line 139

def get_services()
        return get_request(address("/OS-KSADM/services"), token())
end

#rawObject

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



49
50
51
# File 'lib/ropenstack/identity/v2.rb', line 49

def raw()
        return @data		
end

#scope_token(para1, para2 = nil, para3 = nil) ⇒ Object

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.



37
38
39
40
41
42
43
44
# File 'lib/ropenstack/identity/v2.rb', line 37

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

#servicesObject

Get the service catalog returned by Identity on authentication.



101
102
103
# File 'lib/ropenstack/identity/v2.rb', line 101

def services()
        return @data["access"]["serviceCatalog"]
end

#tenant_idObject

Get the tenant id from the @data



108
109
110
# File 'lib/ropenstack/identity/v2.rb', line 108

def tenant_id()
        return @data["access"]["token"]["tenant"]["id"]
end

#tenant_listObject

Separate Identity Calls



118
119
120
# File 'lib/ropenstack/identity/v2.rb', line 118

def tenant_list()
        return get_request(address('/tenants'), token()) 
end

#tenant_nameObject



112
113
114
# File 'lib/ropenstack/identity/v2.rb', line 112

def tenant_name()
        return @data["access"]["token"]["tenant"]["name"] 
end

#token(token = nil) ⇒ Object

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



56
57
58
59
60
61
62
# File 'lib/ropenstack/identity/v2.rb', line 56

def token(token = nil)
        if token.nil?
                return @data["access"]["token"]["id"] 
        else
                get_request(address("/tokens/#{token}"), token())
        end
end

#token?(token) ⇒ Boolean

TODO Add head version of tokens call

Returns:

  • (Boolean)


67
68
69
# File 'lib/ropenstack/identity/v2.rb', line 67

def token?(token)
        return false
end

#token_metadataObject

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



75
76
77
# File 'lib/ropenstack/identity/v2.rb', line 75

def ()
        return @data["access"]["token"]
end

#userObject

Return the user hash from the authentication data



82
83
84
# File 'lib/ropenstack/identity/v2.rb', line 82

def user()
        return @data["access"]["user"]
end

#versionObject



174
175
176
# File 'lib/ropenstack/identity/v2.rb', line 174

def version
  "V2"
end