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 compute_v2 to use auth 2.0 endpoints

This commit is contained in:
Kyle Rames 2013-02-20 08:14:34 -06:00
parent 65eba751da
commit ca7d7135f5
3 changed files with 101 additions and 9 deletions

View file

@ -82,7 +82,7 @@ module Fog
end end
def authenticate(options) def authenticate(options)
uri = self.send authentication_method, options self.send authentication_method, options
end end
def authenticate_v2(options) def authenticate_v2(options)

View file

@ -16,6 +16,7 @@ module Fog
recognizes :rackspace_endpoint recognizes :rackspace_endpoint
recognizes :rackspace_auth_url recognizes :rackspace_auth_url
recognizes :rackspace_auth_token recognizes :rackspace_auth_token
recognizes :rackspace_region
model_path 'fog/rackspace/models/compute_v2' model_path 'fog/rackspace/models/compute_v2'
model :server model :server
@ -69,8 +70,22 @@ module Fog
request :create_network request :create_network
request :delete_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 class Mock
include Fog::Rackspace::MockData include Base
include Fog::Rackspace::MockData
def initialize(options) def initialize(options)
@rackspace_api_key = options[:rackspace_api_key] @rackspace_api_key = options[:rackspace_api_key]
@ -94,15 +109,18 @@ module Fog
end end
class Real class Real
include Base
def initialize(options = {}) def initialize(options = {})
@rackspace_api_key = options[:rackspace_api_key] @rackspace_api_key = options[:rackspace_api_key]
@rackspace_username = options[:rackspace_username] @rackspace_username = options[:rackspace_username]
@rackspace_auth_url = options[:rackspace_auth_url] @rackspace_auth_url = options[:rackspace_auth_url]
@rackspace_endpoint = options[:rackspace_endpoint]
@rackspace_region = options[:rackspace_region] || :dfw
@rackspace_must_reauthenticate = false @rackspace_must_reauthenticate = false
@connection_options = options[:connection_options] || {} @connection_options = options[:connection_options] || {}
endpoint = options[:rackspace_endpoint] || DFW_ENDPOINT uri = authenticate
uri = URI.parse(endpoint)
@host = uri.host @host = uri.host
@persistent = options[:persistent] || false @persistent = options[:persistent] || false
@ -110,8 +128,9 @@ module Fog
@port = uri.port @port = uri.port
@scheme = uri.scheme @scheme = uri.scheme
authenticate # account_id = credentials['X-Server-Management-Url'].match(/.*\/([\d]+)$/)[1]
# @path = "#{@path}/#{account_id}"
@connection = Fog::Connection.new(uri.to_s, @persistent, @connection_options) @connection = Fog::Connection.new(uri.to_s, @persistent, @connection_options)
end end
@ -145,18 +164,33 @@ module Fog
response response
end end
private private
def authenticate def authenticate
options = { options = {
:rackspace_api_key => @rackspace_api_key, :rackspace_api_key => @rackspace_api_key,
:rackspace_username => @rackspace_username, :rackspace_username => @rackspace_username,
:rackspace_auth_url => @rackspace_auth_url :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
def authenticate_v1(options)
credentials = Fog::Rackspace.authenticate(options, @connection_options) credentials = Fog::Rackspace.authenticate(options, @connection_options)
@auth_token = credentials['X-Auth-Token'] @auth_token = credentials['X-Auth-Token']
account_id = credentials['X-Server-Management-Url'].match(/.*\/([\d]+)$/)[1] account_id = credentials['X-Server-Management-Url'].match(/.*\/([\d]+)$/)[1]
@path = "#{@path}/#{account_id}"
endpoint = @rackspace_endpoint || DFW_ENDPOINT
uri = URI.parse(endpoint)
uri.path = "#{uri.path}/#{account_id}"
uri
end end
end end
end end

View file

@ -0,0 +1,58 @@
Shindo.tests('Fog::Compute::RackspaceV2', ['rackspace']) do
def assert_method(url, method)
@service.instance_variable_set "@rackspace_auth_url", url
returns(method) { @service.send :authentication_method }
end
tests('#authentication_method') do
@service = Fog::Compute::RackspaceV2.new
assert_method nil, :authenticate_v2
assert_method 'https://identity.api.rackspacecloud.com/v1', :authenticate_v1
assert_method 'https://identity.api.rackspacecloud.com/v1.1', :authenticate_v1
assert_method 'https://identity.api.rackspacecloud.com/v2.0', :authenticate_v2
assert_method 'https://lon.identity.api.rackspacecloud.com/v1', :authenticate_v1
assert_method 'https://lon.identity.api.rackspacecloud.com/v1.1', :authenticate_v1
assert_method 'https://lon.identity.api.rackspacecloud.com/v2.0', :authenticate_v2
end
tests('legacy authentication') do
pending if Fog.mocking?
@service = Fog::Compute::RackspaceV2.new :rackspace_auth_url => 'https://identity.api.rackspacecloud.com/v1.0'
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(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 }
end
end
tests('current authentation') do
pending if Fog.mocking?
@service = Fog::Compute::RackspaceV2.new :rackspace_auth_url => 'https://identity.api.rackspacecloud.com/v2.0'
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, "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 }
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 }
end
end
end