2011-09-26 02:41:54 -04:00
|
|
|
require(File.expand_path(File.join(File.dirname(__FILE__), 'core')))
|
|
|
|
|
|
|
|
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')
|
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]
|
|
|
|
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-02-21 11:09:26 -05:00
|
|
|
:server_management_url => response.headers['X-Server-Management-Url']
|
|
|
|
}
|
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-02-26 03:09:22 -05:00
|
|
|
@openstack_tenant = options[:openstack_tenant] || 'admin'
|
2011-09-26 02:41:54 -04:00
|
|
|
@compute_service_name = options[:openstack_compute_service_name]
|
2012-02-22 09:40:32 -05:00
|
|
|
@endpoint_type = options[:openstack_endpoint_type] || 'publicURL'
|
2011-09-26 02:41:54 -04:00
|
|
|
|
|
|
|
req_body= {
|
2012-02-10 12:19:08 -05:00
|
|
|
'auth' => {
|
|
|
|
'passwordCredentials' => {
|
|
|
|
'username' => @openstack_username,
|
|
|
|
'password' => @openstack_api_key
|
|
|
|
}
|
2011-09-26 02:41:54 -04:00
|
|
|
}
|
|
|
|
}
|
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-02-22 01:46:56 -05:00
|
|
|
svc = body['access']['serviceCatalog'].
|
|
|
|
detect{|x| @compute_service_name.include?(x['type']) }
|
2012-02-21 11:09:26 -05:00
|
|
|
|
|
|
|
unless svc
|
|
|
|
unless @openstack_tenant
|
|
|
|
response = connection.request({
|
|
|
|
:expects => [200, 204],
|
|
|
|
:headers => {'Content-Type' => 'application/json',
|
|
|
|
'X-Auth-Token' => body['access']['token']['id']},
|
|
|
|
:host => uri.host,
|
|
|
|
:method => 'GET',
|
|
|
|
:path => '/v2.0/tenants'
|
|
|
|
})
|
|
|
|
|
|
|
|
body = MultiJson.decode(response.body)
|
|
|
|
req_body['auth']['tenantName'] = body['tenants'].first['name']
|
|
|
|
end
|
|
|
|
|
|
|
|
body = retrieve_tokens_v2(connection, req_body, uri)
|
|
|
|
svc = body['access']['serviceCatalog'].
|
2012-02-22 01:46:56 -05:00
|
|
|
detect{|x| @compute_service_name.include?(x['type']) }
|
2012-02-21 11:09:26 -05:00
|
|
|
end
|
|
|
|
|
2012-02-22 09:40:32 -05:00
|
|
|
mgmt_url = svc['endpoints'].detect{|x| x[@endpoint_type]}[@endpoint_type]
|
2012-02-21 11:09:26 -05:00
|
|
|
token = body['access']['token']['id']
|
|
|
|
|
|
|
|
{ :token => token,
|
|
|
|
:server_management_url => mgmt_url }
|
|
|
|
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 10:31:28 -04:00
|
|
|
:body => Fog::JSON.encode(req_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
|