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']
|
2012-12-05 13:53:11 -05:00
|
|
|
if message.nil? and !data.values.first.nil?
|
|
|
|
message = data.values.first['message']
|
|
|
|
end
|
2011-09-26 02:41:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
new_error = super(error, message)
|
|
|
|
new_error.instance_variable_set(:@response_data, data)
|
|
|
|
new_error
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class ServiceUnavailable < ServiceError; end
|
|
|
|
|
|
|
|
class BadRequest < ServiceError
|
|
|
|
attr_reader :validation_errors
|
|
|
|
|
|
|
|
def self.slurp(error)
|
|
|
|
new_error = super(error)
|
2012-12-05 13:53:11 -05:00
|
|
|
unless new_error.response_data.nil? or new_error.response_data['badRequest'].nil?
|
|
|
|
new_error.instance_variable_set(:@validation_errors, new_error.response_data['badRequest']['validationErrors'])
|
2011-09-26 02:41:54 -04:00
|
|
|
end
|
|
|
|
new_error
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-02-22 01:46:56 -05:00
|
|
|
service(:compute , 'openstack/compute' , 'Compute' )
|
2013-02-16 06:25:43 -05:00
|
|
|
service(:image, 'openstack/image', 'Image')
|
2012-02-22 01:46:56 -05:00
|
|
|
service(:identity, 'openstack/identity', 'Identity')
|
2012-09-20 18:24:19 -04:00
|
|
|
service(:network, 'openstack/network', 'Network')
|
2013-01-23 14:26:17 -05:00
|
|
|
service(:storage, 'openstack/storage', 'Storage')
|
2013-02-27 10:58:03 -05:00
|
|
|
service(:volume, 'openstack/volume', 'Volume')
|
2013-04-04 23:39:45 -04:00
|
|
|
service(:metering, 'openstack/metering', 'Metering')
|
2013-06-04 07:25:51 -04:00
|
|
|
service(:orchestration, 'openstack/orchestration', 'Orchestration')
|
2011-09-26 02:41:54 -04:00
|
|
|
|
2013-08-15 02:48:13 -04:00
|
|
|
def self.authenticate(options, connection_options = {})
|
|
|
|
case options[:openstack_auth_uri].path
|
|
|
|
when /v1(\.\d+)?/
|
|
|
|
authenticate_v1(options, connection_options)
|
|
|
|
else
|
|
|
|
authenticate_v2(options, connection_options)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
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
|
|
|
|
},
|
|
|
|
:method => 'GET',
|
|
|
|
:path => (uri.path and not uri.path.empty?) ? uri.path : 'v1.0'
|
|
|
|
})
|
|
|
|
|
|
|
|
return {
|
|
|
|
:token => response.headers['X-Auth-Token'],
|
2013-08-15 02:48:13 -04:00
|
|
|
:server_management_url => response.headers['X-Server-Management-Url'] || response.headers['X-Storage-Url'],
|
2012-07-08 12:54:41 -04:00
|
|
|
: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 = {})
|
2012-10-30 02:03:31 -04:00
|
|
|
uri = options[:openstack_auth_uri]
|
|
|
|
tenant_name = options[:openstack_tenant]
|
2012-12-10 07:55:54 -05:00
|
|
|
service_type = options[:openstack_service_type]
|
2012-10-30 02:03:31 -04:00
|
|
|
service_name = options[:openstack_service_name]
|
2012-12-10 07:55:54 -05:00
|
|
|
identity_service_type = options[:openstack_identity_service_type]
|
2012-10-30 02:03:31 -04:00
|
|
|
endpoint_type = (options[:openstack_endpoint_type] || 'publicURL').to_s
|
|
|
|
openstack_region = options[:openstack_region]
|
2011-09-26 02:41:54 -04:00
|
|
|
|
2012-03-20 04:52:14 -04:00
|
|
|
|
2012-10-30 02:03:31 -04:00
|
|
|
body = retrieve_tokens_v2(options, connection_options)
|
2012-12-10 07:55:54 -05:00
|
|
|
service = get_service(body, service_type, service_name)
|
2012-02-21 11:09:26 -05:00
|
|
|
|
2012-11-14 21:04:53 -05:00
|
|
|
options[:unscoped_token] = body['access']['token']['id']
|
|
|
|
|
2012-10-30 02:03:31 -04:00
|
|
|
unless service
|
|
|
|
unless tenant_name
|
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']},
|
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?
|
2012-12-09 16:51:07 -05:00
|
|
|
raise Fog::Errors::NotFound.new('No Tenant Found')
|
2012-05-16 02:16:19 -04:00
|
|
|
else
|
2012-10-30 02:03:31 -04:00
|
|
|
options[:openstack_tenant] = body['tenants'].first['name']
|
2012-05-16 02:16:19 -04:00
|
|
|
end
|
2012-02-21 11:09:26 -05:00
|
|
|
end
|
|
|
|
|
2012-10-30 02:03:31 -04:00
|
|
|
body = retrieve_tokens_v2(options, connection_options)
|
2012-12-10 07:55:54 -05:00
|
|
|
service = get_service(body, service_type, service_name)
|
|
|
|
|
2012-02-21 11:09:26 -05:00
|
|
|
end
|
|
|
|
|
2012-10-30 02:03:31 -04:00
|
|
|
service['endpoints'] = service['endpoints'].select do |endpoint|
|
|
|
|
endpoint['region'] == openstack_region
|
|
|
|
end if openstack_region
|
|
|
|
|
2012-12-04 12:46:21 -05:00
|
|
|
if service['endpoints'].empty?
|
2013-08-02 21:03:38 -04:00
|
|
|
raise Fog::Errors::NotFound.new("No endpoints available for region '#{openstack_region}'")
|
2012-12-04 12:46:21 -05:00
|
|
|
end if openstack_region
|
|
|
|
|
2012-12-04 23:22:31 -05:00
|
|
|
unless service
|
|
|
|
available = body['access']['serviceCatalog'].map { |endpoint|
|
|
|
|
endpoint['type']
|
|
|
|
}.sort.join ', '
|
|
|
|
|
2012-12-10 07:55:54 -05:00
|
|
|
missing = service_type.join ', '
|
2012-12-04 23:22:31 -05:00
|
|
|
|
|
|
|
message = "Could not find service #{missing}. Have #{available}"
|
|
|
|
|
2012-12-09 16:51:07 -05:00
|
|
|
raise Fog::Errors::NotFound, message
|
2012-12-04 23:22:31 -05:00
|
|
|
end
|
|
|
|
|
2012-10-30 02:03:31 -04:00
|
|
|
if service['endpoints'].count > 1
|
2012-11-26 17:45:27 -05:00
|
|
|
regions = service["endpoints"].map{ |e| e['region'] }.uniq.join(',')
|
2012-12-09 16:51:07 -05:00
|
|
|
raise Fog::Errors::NotFound.new("Multiple regions available choose one of these '#{regions}'")
|
2012-07-16 11:28:28 -04:00
|
|
|
end
|
|
|
|
|
2012-12-10 07:55:54 -05:00
|
|
|
identity_service = get_service(body, identity_service_type) if identity_service_type
|
2012-04-20 10:25:00 -04:00
|
|
|
tenant = body['access']['token']['tenant']
|
|
|
|
user = body['access']['user']
|
|
|
|
|
2012-10-30 02:03:31 -04:00
|
|
|
management_url = service['endpoints'].detect{|s| s[endpoint_type]}[endpoint_type]
|
|
|
|
identity_url = identity_service['endpoints'].detect{|s| s['publicURL']}['publicURL'] if identity_service
|
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
|
|
|
:identity_public_endpoint => identity_url,
|
2012-10-30 02:03:31 -04:00
|
|
|
:server_management_url => management_url,
|
|
|
|
:token => body['access']['token']['id'],
|
|
|
|
:expires => body['access']['token']['expires'],
|
2012-11-14 21:04:53 -05:00
|
|
|
:current_user_id => body['access']['user']['id'],
|
|
|
|
:unscoped_token => options[:unscoped_token]
|
2012-03-04 07:29:27 -05:00
|
|
|
}
|
2012-12-10 07:55:54 -05:00
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.get_service(body, service_type=[], service_name=nil)
|
|
|
|
body['access']['serviceCatalog'].detect do |s|
|
|
|
|
if service_name.nil? or service_name.empty?
|
|
|
|
service_type.include?(s['type'])
|
|
|
|
else
|
|
|
|
service_type.include?(s['type']) and s['name'] == service_name
|
|
|
|
end
|
|
|
|
end
|
2012-02-21 11:09:26 -05:00
|
|
|
end
|
|
|
|
|
2012-10-30 02:03:31 -04:00
|
|
|
def self.retrieve_tokens_v2(options, connection_options = {})
|
|
|
|
api_key = options[:openstack_api_key].to_s
|
|
|
|
username = options[:openstack_username].to_s
|
|
|
|
tenant_name = options[:openstack_tenant].to_s
|
2012-11-14 21:04:53 -05:00
|
|
|
auth_token = options[:openstack_auth_token] || options[:unscoped_token]
|
2012-10-30 02:03:31 -04:00
|
|
|
uri = options[:openstack_auth_uri]
|
|
|
|
|
|
|
|
connection = Fog::Connection.new(uri.to_s, false, connection_options)
|
|
|
|
request_body = {:auth => Hash.new}
|
|
|
|
|
|
|
|
if auth_token
|
|
|
|
request_body[:auth][:token] = {
|
|
|
|
:id => auth_token
|
|
|
|
}
|
|
|
|
else
|
|
|
|
request_body[:auth][:passwordCredentials] = {
|
|
|
|
:username => username,
|
|
|
|
:password => api_key
|
|
|
|
}
|
|
|
|
end
|
|
|
|
request_body[:auth][:tenantName] = tenant_name if tenant_name
|
|
|
|
|
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-10-11 16:59:12 -04:00
|
|
|
:method => 'POST',
|
2012-10-30 02:03:31 -04:00
|
|
|
:path => (uri.path and not uri.path.empty?) ? uri.path : 'v2.0'
|
2011-09-26 02:41:54 -04:00
|
|
|
})
|
2012-02-21 11:09:26 -05:00
|
|
|
|
|
|
|
Fog::JSON.decode(response.body)
|
2011-09-26 02:41:54 -04:00
|
|
|
end
|
2013-03-26 11:25:04 -04:00
|
|
|
|
|
|
|
def self.get_supported_version(supported_versions, uri, auth_token, connection_options = {})
|
|
|
|
connection = Fog::Connection.new("#{uri.scheme}://#{uri.host}:#{uri.port}", false, connection_options)
|
|
|
|
response = connection.request({
|
|
|
|
:expects => [200, 204, 300],
|
|
|
|
:headers => {'Content-Type' => 'application/json',
|
|
|
|
'Accept' => 'application/json',
|
|
|
|
'X-Auth-Token' => auth_token},
|
|
|
|
:method => 'GET'
|
|
|
|
})
|
|
|
|
|
|
|
|
body = Fog::JSON.decode(response.body)
|
|
|
|
version = nil
|
|
|
|
unless body['versions'].empty?
|
|
|
|
supported_version = body['versions'].detect do |x|
|
|
|
|
x["id"].match(supported_versions) &&
|
|
|
|
(x["status"] == "CURRENT" || x["status"] == "SUPPORTED")
|
|
|
|
end
|
|
|
|
version = supported_version["id"] if supported_version
|
|
|
|
end
|
|
|
|
if version.nil?
|
|
|
|
raise Fog::OpenStack::Errors::ServiceUnavailable.new(
|
|
|
|
"OpenStack service only supports API versions #{supported_versions.inspect}")
|
|
|
|
end
|
|
|
|
|
|
|
|
version
|
|
|
|
end
|
|
|
|
|
2013-01-23 14:53:29 -05:00
|
|
|
# CGI.escape, but without special treatment on spaces
|
|
|
|
def self.escape(str,extra_exclude_chars = '')
|
|
|
|
str.gsub(/([^a-zA-Z0-9_.-#{extra_exclude_chars}]+)/) do
|
|
|
|
'%' + $1.unpack('H2' * $1.bytesize).join('%').upcase
|
|
|
|
end
|
|
|
|
end
|
2011-09-26 02:41:54 -04:00
|
|
|
|
|
|
|
end
|
|
|
|
end
|