1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00

[rackspace|compute_v2] updated to support auth 2.0

This commit is contained in:
Kyle Rames 2013-02-20 15:48:19 -06:00
parent ca911a1bdc
commit 65af5b018e
7 changed files with 85 additions and 54 deletions

View file

@ -15,6 +15,10 @@ module Fog
def v1_authentication?
@identity_service.nil?
end
def v2_authentication?
@identity_service != nil
end
def authenticate_v2(options)
h = {

View file

@ -76,7 +76,7 @@ module Fog
url = @rackspace_cdn_url || service_endpoint_url
unless url
if v1_authentication?
raise "Service Endpoint must be specified via :rackspace_cdn_url parameter" unless url
raise "Service Endpoint must be specified via :rackspace_cdn_url parameter"
else
url = @identity_service.service_catalog.get_endpoint(:cloudFilesCDN, @rackspace_region)
end

View file

@ -17,6 +17,7 @@ module Fog
recognizes :rackspace_auth_url
recognizes :rackspace_auth_token
recognizes :rackspace_region
recognizes :rackspace_compute_url
model_path 'fog/rackspace/models/compute_v2'
model :server
@ -70,21 +71,8 @@ module Fog
request :create_network
request :delete_network
module Base
private
def authentication_method
if @rackspace_auth_url && @rackspace_auth_url =~ /v1(\.\d)?\w*$/
:authenticate_v1
else
:authenticate_v2
end
end
end
class Mock
include Base
include Fog::Rackspace::Authentication
include Fog::Rackspace::MockData
def initialize(options)
@ -109,29 +97,32 @@ module Fog
end
class Real
include Base
include Fog::Rackspace::Authentication
def initialize(options = {})
@rackspace_api_key = options[:rackspace_api_key]
@rackspace_username = options[:rackspace_username]
@rackspace_auth_url = options[:rackspace_auth_url]
@rackspace_endpoint = options[:rackspace_endpoint]
@rackspace_endpoint = options[:rackspace_compute_url] || options[:rackspace_endpoint]
@rackspace_region = options[:rackspace_region] || :dfw
@rackspace_must_reauthenticate = false
@connection_options = options[:connection_options] || {}
uri = authenticate
@uri = authenticate
@host = uri.host
@persistent = options[:persistent] || false
@path = uri.path
@port = uri.port
@scheme = uri.scheme
# account_id = credentials['X-Server-Management-Url'].match(/.*\/([\d]+)$/)[1]
# @path = "#{@path}/#{account_id}"
deprecation_warnings(options)
@connection = Fog::Connection.new(uri.to_s, @persistent, @connection_options)
@persistent = options[:persistent] || false
@connection = Fog::Connection.new(@uri, @persistent, @connection_options)
end
def deprecation_warnings(options)
Fog::Logger.deprecation("The :rackspace_endpoint option is deprecated. Please use :rackspace_compute_url for custom endpoints") if options[:rackspace_endpoint]
if [DFW_ENDPOINT, ORD_ENDPOINT, LON_ENDPOINT].include?(@rackspace_endpoint) && v2_authentication?
regions = @identity_service.service_catalog.display_service_regions(:cloudServersOpenStack)
Fog::Logger.deprecation("Please specify region using :rackspace_region rather than :rackspace_endpoint. Valid region for :rackspace_region are #{regions}.")
end
end
def request(params)
@ -141,8 +132,8 @@ module Fog
'Content-Type' => 'application/json',
'X-Auth-Token' => @auth_token
}.merge!(params[:headers] || {}),
:host => @host,
:path => "#{@path}/#{params[:path]}"
:host => @uri.host,
:path => "#{@uri.path}/#{params[:path]}"
}))
rescue Excon::Errors::NotFound => error
raise NotFound.slurp error
@ -163,6 +154,21 @@ module Fog
end
response
end
def endpoint_uri(service_endpoint_url=nil)
return @uri if @uri
url = @rackspace_endpoint || service_endpoint_url
unless url
if v1_authentication?
raise "Service Endpoint must be specified via :rackspace_compute_url parameter"
else
url = @identity_service.service_catalog.get_endpoint(:cloudServersOpenStack, @rackspace_region)
end
end
@uri = URI.parse url
end
private
@ -173,14 +179,7 @@ module Fog
:rackspace_auth_url => @rackspace_auth_url,
}
self.send authentication_method, options
end
def authenticate_v2(options)
@identity_service = Fog::Rackspace::Identity.new(options)
@auth_token = @identity_service.auth_token
url = @identity_service.service_catalog.get_endpoint(:cloudServersOpenStack, @rackspace_region)
URI.parse url
end
end
def authenticate_v1(options)
credentials = Fog::Rackspace.authenticate(options, @connection_options)
@ -188,9 +187,9 @@ module Fog
account_id = credentials['X-Server-Management-Url'].match(/.*\/([\d]+)$/)[1]
endpoint = @rackspace_endpoint || DFW_ENDPOINT
uri = URI.parse(endpoint)
uri.path = "#{uri.path}/#{account_id}"
uri
@uri = URI.parse(endpoint)
@uri.path = "#{@uri.path}/#{account_id}"
@uri
end
end
end

View file

@ -17,21 +17,27 @@ module Fog
def get_endpoints(service_type)
service_type = service_type.is_a?(String) ? service_type.to_sym : service_type
catalog[service_type]
catalog[service_type]
end
def display_service_regions(service_type)
endpoints = get_endpoints(service_type)
endpoints.collect { |k,v| ":#{k}" }.join(", ")
end
def get_endpoint(service_type, region=nil)
endpoint = get_endpoints(service_type)
raise "Unable to locate endpoint for service #{service_type}" unless endpoint
return endpoint if endpoint.is_a?(String)
return endpoint if endpoint.is_a?(String) #There is only one endpoint for service
unless region
regions = endpoint.collect { |k,v| ":#{k}" }.join(", ")
raise "There are multiple endpoints avaliable for #{service_type}. Please specify one of the following regions: #{regions}."
raise "There are multiple endpoints avaliable for #{service_type}. Please specify one of the following regions: #{display_service_regions(service_type)}."
end
region = region.is_a?(String) ? region.to_sym : region
get_endpoints(service_type)[region]
endpoint = get_endpoints(service_type)[region]
raise "Unknown region :#{region} for service #{service_type}. Please use one of the following regions: #{display_service_regions(service_type)}" unless endpoint
endpoint
end
def reload

View file

@ -99,6 +99,7 @@ module Fog
@rackspace_temp_url_key = options[:rackspace_temp_url_key]
@rackspace_must_reauthenticate = false
@connection_options = options[:connection_options] || {}
authenticate
@persistent = options[:persistent] || false
Excon.defaults[:ssl_verify_peer] = false if service_net?
@ -156,7 +157,7 @@ module Fog
:rackspace_username => @rackspace_username,
:rackspace_auth_url => @rackspace_auth_url
}
uri = self.send authentication_method, options
@uri = self.send authentication_method, options
else
@auth_token = @rackspace_auth_token
@uri = URI.parse(@rackspace_storage_url)
@ -169,7 +170,7 @@ module Fog
url = @rackspace_storage_url || service_endpoint_url
unless url
if v1_authentication?
raise "Service Endpoint must be specified via :rackspace_storage_url parameter" unless url
raise "Service Endpoint must be specified via :rackspace_storage_url parameter"
else
url = @identity_service.service_catalog.get_endpoint(:cloudFiles, @rackspace_region)
end

View file

@ -24,14 +24,14 @@ Shindo.tests('Fog::Compute::RackspaceV2', ['rackspace']) do
tests('variables populated') do
returns(false, "auth token populated") { @service.instance_variable_get("@auth_token").nil? }
returns(false, "path populated") { @service.instance_variable_get("@path").nil? }
returns(false, "path populated") { @service.instance_variable_get("@uri").path.nil? }
returns(true, "identity_service was not used") { @service.instance_variable_get("@identity_service").nil? }
end
tests('custom endpoint') do
@service = Fog::Compute::RackspaceV2.new :rackspace_auth_url => 'https://identity.api.rackspacecloud.com/v1.0',
:rackspace_endpoint => 'https://my-custom-endpoint.com'
returns(true, "uses custom endpoint") { (@service.instance_variable_get("@host") =~ /my-custom-endpoint\.com/) != nil }
:rackspace_compute_url => 'https://my-custom-endpoint.com'
returns(true, "uses custom endpoint") { (@service.instance_variable_get("@uri").host =~ /my-custom-endpoint\.com/) != nil }
end
end
@ -41,18 +41,39 @@ Shindo.tests('Fog::Compute::RackspaceV2', ['rackspace']) do
tests('variables populated') do
returns(false, "auth token populated") { @service.instance_variable_get("@auth_token").nil? }
returns(false, "path populated") { @service.instance_variable_get("@path").nil? }
returns(false, "path populated") { @service.instance_variable_get("@uri").host.nil? }
returns(false, "identity service was used") { @service.instance_variable_get("@identity_service").nil? }
end
tests('dfw region') do
@service = Fog::Compute::RackspaceV2.new :rackspace_auth_url => 'https://identity.api.rackspacecloud.com/v2.0', :rackspace_region => :dfw
returns(true) { (@service.instance_variable_get("@host") =~ /dfw/) != nil }
returns(true) { (@service.instance_variable_get("@uri").host =~ /dfw/) != nil }
end
tests('ord region') do
@service = Fog::Compute::RackspaceV2.new :rackspace_auth_url => 'https://identity.api.rackspacecloud.com/v2.0', :rackspace_region => :ord
returns(true) { (@service.instance_variable_get("@host") =~ /ord/) != nil }
returns(true) { (@service.instance_variable_get("@uri").host =~ /ord/) != nil }
end
tests('custom endpoint') do
@service = Fog::Compute::RackspaceV2.new :rackspace_auth_url => 'https://identity.api.rackspacecloud.com/v2.0',
:rackspace_compute_url => 'https://my-custom-endpoint.com'
returns(true, "uses custom endpoint") { (@service.instance_variable_get("@uri").host =~ /my-custom-endpoint\.com/) != nil }
end
end
tests('default auth') do
pending if Fog.mocking?
tests('no params') do
@service = Fog::Compute::RackspaceV2.new
returns(true) { (@service.instance_variable_get("@uri").host =~ /dfw/) != nil }
end
tests('specify region') do
@service = Fog::Compute::RackspaceV2.new :rackspace_region => :ord
returns(true) { (@service.instance_variable_get("@uri").host =~ /ord/ ) != nil }
end
tests('custom endpoint') do
@service = Fog::Compute::RackspaceV2.new :rackspace_compute_url => 'https://my-custom-endpoint.com'
returns(true, "uses custom endpoint") { (@service.instance_variable_get("@uri").host =~ /my-custom-endpoint\.com/) != nil }
end
end
end

View file

@ -60,7 +60,7 @@ Shindo.tests('Fog::Rackspace::ServiceCatalog | users', ['rackspace']) do
tests('error conditions') do
raises(RuntimeError) { @service_catalog.get_endpoint(:cloudServersOpenStack) }
returns(nil) { @service_catalog.get_endpoint(:cloudServersOpenStack, :sat) }
raises(RuntimeError) { @service_catalog.get_endpoint(:cloudServersOpenStack, :sat) }
raises(RuntimeError) { @service_catalog.get_endpoint('non-existent') }
end
end