2012-05-14 14:10:36 -04:00
|
|
|
require 'fog/core'
|
2011-09-26 02:41:54 -04:00
|
|
|
|
|
|
|
module Fog
|
|
|
|
module OpenStack
|
|
|
|
extend Fog::Provider
|
|
|
|
|
|
|
|
module Errors
|
|
|
|
class ServiceError < Fog::Errors::Error
|
|
|
|
attr_reader :response_data
|
|
|
|
|
|
|
|
def self.slurp(error)
|
|
|
|
if error.response.body.empty?
|
|
|
|
data = nil
|
|
|
|
message = nil
|
|
|
|
else
|
2012-04-25 10:31:28 -04:00
|
|
|
data = Fog::JSON.decode(error.response.body)
|
2011-09-26 02:41:54 -04:00
|
|
|
message = data['message']
|
|
|
|
end
|
|
|
|
|
|
|
|
new_error = super(error, message)
|
|
|
|
new_error.instance_variable_set(:@response_data, data)
|
|
|
|
new_error
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class InternalServerError < ServiceError; end
|
|
|
|
class Conflict < ServiceError; end
|
|
|
|
class NotFound < ServiceError; end
|
|
|
|
class ServiceUnavailable < ServiceError; end
|
|
|
|
|
|
|
|
class BadRequest < ServiceError
|
|
|
|
attr_reader :validation_errors
|
|
|
|
|
|
|
|
def self.slurp(error)
|
|
|
|
new_error = super(error)
|
|
|
|
unless new_error.response_data.nil?
|
|
|
|
new_error.instance_variable_set(:@validation_errors, new_error.response_data['validationErrors'])
|
|
|
|
end
|
|
|
|
new_error
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-02-22 01:46:56 -05:00
|
|
|
service(:compute , 'openstack/compute' , 'Compute' )
|
|
|
|
service(:identity, 'openstack/identity', 'Identity')
|
2012-09-20 18:24:19 -04:00
|
|
|
service(:network, 'openstack/network', 'Network')
|
2011-09-26 02:41:54 -04:00
|
|
|
|
|
|
|
# legacy v1.0 style auth
|
|
|
|
def self.authenticate_v1(options, connection_options = {})
|
2012-02-21 11:09:26 -05:00
|
|
|
uri = options[:openstack_auth_uri]
|
|
|
|
connection = Fog::Connection.new(uri.to_s, false, connection_options)
|
2011-09-26 02:41:54 -04:00
|
|
|
@openstack_api_key = options[:openstack_api_key]
|
|
|
|
@openstack_username = options[:openstack_username]
|
2012-07-08 12:54:41 -04:00
|
|
|
|
2011-09-26 02:41:54 -04:00
|
|
|
response = connection.request({
|
|
|
|
:expects => [200, 204],
|
|
|
|
:headers => {
|
|
|
|
'X-Auth-Key' => @openstack_api_key,
|
|
|
|
'X-Auth-User' => @openstack_username
|
|
|
|
},
|
|
|
|
:host => uri.host,
|
|
|
|
:method => 'GET',
|
|
|
|
:path => (uri.path and not uri.path.empty?) ? uri.path : 'v1.0'
|
|
|
|
})
|
|
|
|
|
|
|
|
return {
|
|
|
|
:token => response.headers['X-Auth-Token'],
|
2012-07-08 12:54:41 -04:00
|
|
|
:server_management_url => response.headers['X-Server-Management-Url'],
|
|
|
|
:identity_public_endpoint => response.headers['X-Keystone']
|
2012-02-21 11:09:26 -05:00
|
|
|
}
|
2011-09-26 02:41:54 -04:00
|
|
|
end
|
|
|
|
|
2012-02-21 11:09:26 -05:00
|
|
|
# Keystone Style Auth
|
|
|
|
def self.authenticate_v2(options, connection_options = {})
|
|
|
|
uri = options[:openstack_auth_uri]
|
|
|
|
connection = Fog::Connection.new(uri.to_s, false, connection_options)
|
2011-09-26 02:41:54 -04:00
|
|
|
@openstack_api_key = options[:openstack_api_key]
|
|
|
|
@openstack_username = options[:openstack_username]
|
2012-03-03 08:32:05 -05:00
|
|
|
@openstack_tenant = options[:openstack_tenant]
|
2012-03-04 09:01:55 -05:00
|
|
|
@openstack_auth_token = options[:openstack_auth_token]
|
|
|
|
@service_name = options[:openstack_service_name]
|
2012-03-03 08:32:05 -05:00
|
|
|
@identity_service_name = options[:openstack_identity_service_name]
|
2012-03-04 09:01:55 -05:00
|
|
|
@endpoint_type = options[:openstack_endpoint_type] || 'publicURL'
|
2012-07-16 11:28:28 -04:00
|
|
|
@openstack_region = options[:openstack_region]
|
2012-03-04 09:01:55 -05:00
|
|
|
|
|
|
|
if @openstack_auth_token
|
|
|
|
req_body = {
|
|
|
|
'auth' => {
|
|
|
|
'token' => {
|
|
|
|
'id' => @openstack_auth_token
|
|
|
|
}
|
2012-02-10 12:19:08 -05:00
|
|
|
}
|
2011-09-26 02:41:54 -04:00
|
|
|
}
|
2012-03-04 09:01:55 -05:00
|
|
|
else
|
|
|
|
req_body = {
|
|
|
|
'auth' => {
|
|
|
|
'passwordCredentials' => {
|
2012-10-18 01:33:32 -04:00
|
|
|
'username' => @openstack_username.to_s,
|
2012-05-16 04:11:09 -04:00
|
|
|
'password' => @openstack_api_key.to_s
|
2012-03-04 09:01:55 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
2012-02-10 12:19:08 -05:00
|
|
|
req_body['auth']['tenantName'] = @openstack_tenant if @openstack_tenant
|
2011-09-26 02:41:54 -04:00
|
|
|
|
2012-02-21 11:09:26 -05:00
|
|
|
body = retrieve_tokens_v2(connection, req_body, uri)
|
2012-03-20 04:52:14 -04:00
|
|
|
|
2012-02-22 01:46:56 -05:00
|
|
|
svc = body['access']['serviceCatalog'].
|
2012-03-03 08:32:05 -05:00
|
|
|
detect{|x| @service_name.include?(x['type']) }
|
2012-02-21 11:09:26 -05:00
|
|
|
|
|
|
|
unless svc
|
|
|
|
unless @openstack_tenant
|
2012-04-19 10:06:36 -04:00
|
|
|
response = Fog::Connection.new(
|
2012-05-03 12:30:18 -04:00
|
|
|
"#{uri.scheme}://#{uri.host}:#{uri.port}/v2.0/tenants", false, connection_options).request({
|
2012-02-21 11:09:26 -05:00
|
|
|
:expects => [200, 204],
|
|
|
|
:headers => {'Content-Type' => 'application/json',
|
2012-09-11 18:45:56 -04:00
|
|
|
'Accept' => 'application/json',
|
2012-02-21 11:09:26 -05:00
|
|
|
'X-Auth-Token' => body['access']['token']['id']},
|
|
|
|
:host => uri.host,
|
2012-04-19 10:06:36 -04:00
|
|
|
:method => 'GET'
|
2012-02-21 11:09:26 -05:00
|
|
|
})
|
|
|
|
|
2012-04-25 12:42:42 -04:00
|
|
|
body = Fog::JSON.decode(response.body)
|
2012-05-16 02:16:19 -04:00
|
|
|
if body['tenants'].empty?
|
|
|
|
raise Errors::NotFound.new('No Tenant Found')
|
|
|
|
else
|
|
|
|
req_body['auth']['tenantName'] = body['tenants'].first['name']
|
|
|
|
end
|
2012-02-21 11:09:26 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
body = retrieve_tokens_v2(connection, req_body, uri)
|
2012-05-26 00:16:17 -04:00
|
|
|
if body['access']['token']['tenant'].nil?
|
|
|
|
raise Errors::NotFound.new("Invalid Tenant '#{@openstack_tenant}'")
|
|
|
|
end
|
2012-02-21 11:09:26 -05:00
|
|
|
svc = body['access']['serviceCatalog'].
|
2012-04-19 10:06:36 -04:00
|
|
|
detect{|x| @service_name.include?(x['type']) }
|
2012-02-21 11:09:26 -05:00
|
|
|
end
|
|
|
|
|
2012-07-16 11:28:28 -04:00
|
|
|
svc['endpoints'] = svc['endpoints'].select{ |x| x['region'] == @openstack_region } if @openstack_region
|
|
|
|
if svc['endpoints'].count > 1
|
|
|
|
regions = svc["endpoints"].map { |x| x['region'] }.uniq.join(',')
|
|
|
|
raise Errors::NotFound.new("Multiple regions available choose one of these '#{regions}'")
|
|
|
|
end
|
|
|
|
|
2012-04-20 10:25:00 -04:00
|
|
|
identity_svc = body['access']['serviceCatalog'].
|
|
|
|
detect{|x| @identity_service_name.include?(x['type']) } if @identity_service_name
|
|
|
|
tenant = body['access']['token']['tenant']
|
|
|
|
user = body['access']['user']
|
|
|
|
|
2012-02-22 09:40:32 -05:00
|
|
|
mgmt_url = svc['endpoints'].detect{|x| x[@endpoint_type]}[@endpoint_type]
|
2012-03-04 03:07:07 -05:00
|
|
|
identity_url = identity_svc['endpoints'].detect{|x| x['publicURL']}['publicURL'] if identity_svc
|
2012-02-21 11:09:26 -05:00
|
|
|
token = body['access']['token']['id']
|
2012-07-18 01:26:33 -04:00
|
|
|
expires = body['access']['token']['expires']
|
2012-02-21 11:09:26 -05:00
|
|
|
|
2012-03-04 07:29:27 -05:00
|
|
|
{
|
2012-04-20 10:25:00 -04:00
|
|
|
:user => user,
|
|
|
|
:tenant => tenant,
|
2012-03-04 07:29:27 -05:00
|
|
|
:token => token,
|
2012-07-18 01:26:33 -04:00
|
|
|
:expires => expires,
|
2012-03-04 07:29:27 -05:00
|
|
|
:server_management_url => mgmt_url,
|
|
|
|
:identity_public_endpoint => identity_url,
|
|
|
|
:current_user_id => body['access']['user']['id']
|
|
|
|
}
|
2012-02-21 11:09:26 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.retrieve_tokens_v2(connection, request_body, uri)
|
2011-09-26 02:41:54 -04:00
|
|
|
response = connection.request({
|
|
|
|
:expects => [200, 204],
|
2012-02-21 11:09:26 -05:00
|
|
|
:headers => {'Content-Type' => 'application/json'},
|
2012-04-25 12:42:42 -04:00
|
|
|
:body => Fog::JSON.encode(request_body),
|
2011-09-26 02:41:54 -04:00
|
|
|
:host => uri.host,
|
2011-10-11 16:59:12 -04:00
|
|
|
:method => 'POST',
|
2011-09-26 02:41:54 -04:00
|
|
|
:path => (uri.path and not uri.path.empty?) ? uri.path : 'v2.0'
|
|
|
|
})
|
2012-02-21 11:09:26 -05:00
|
|
|
|
|
|
|
Fog::JSON.decode(response.body)
|
2011-09-26 02:41:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|