2 # Original work by Stephen Paul Suarez
3 # https://github.com/musashi-dev/aviator/blob/develop/lib/aviator/openstack/identity/v3/public/create_token.rb
5 define_request :create_token, :inherit => [:openstack, :common, :v3, :public, :base] do
8 meta :service, :identity
12 'http://api.openstack.org/api-ref-identity-v3.html#Token_Calls'
14 param :domainId, :required => false, :alias => :domain_id
15 param :domainName, :required => false, :alias => :domain_name
16 param :password, :required => false
17 param :tenantId, :required => false, :alias => :tenant_id
18 param :tenantName, :required => false, :alias => :tenant_name
19 param :tokenId, :required => false, :alias => :token_id
20 param :userId, :required => false, :alias => :user_id
21 param :username, :required => false
25 params[:token_id] ? token_auth_body : password_auth_body
35 url = session_data[:auth_service][:host_uri]
36 url += '/v3' if (URI(url).path =~ /^\/?\w+/).nil?
43 # Removes nil elements from hash
44 # Adapted from http://stackoverflow.com/a/14773555/402145
45 def compact_hash(hash, opts = {})
46 opts[:recurse] ||= true
47 hash.inject({}) do |new_hash, (k,v)|
49 new_hash[k] = opts[:recurse] && v.kind_of?(Hash) ? compact_hash(v, opts) : v
58 :id => params[:domain_id],
59 :name => params[:domain_name]
64 def password_auth_body
68 :methods => ['password'],
70 :user => compact_hash({
71 :id => params[:user_id],
72 :name => params[:username],
73 :password => params[:password]
80 if params[:domain_name] || params[:domain_id]
81 p[:auth][:identity][:password][:user][:domain] = domain_hash
84 if params[:tenant_name] || params[:tenant_id] || params[:domain_name] || params[:domain_id]
85 p[:auth][:scope] = scope_hash
94 if params[:tenant_name] || params[:tenant_id]
95 p[:project] = compact_hash({
96 :id => params[:tenant_id],
97 :name => params[:tenant_name]
99 p[:project][:domain] = domain_hash if params[:domain_name] || params[:domain_id]
101 elsif params[:domain_name] || params[:domain_id]
102 p[:domain] = domain_hash
113 :methods => ['token'],
114 :token => { :id => params[:token_id] }
118 p[:auth][:scope] = scope_hash if params[:tenant_name] || params[:tenant_id]