Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Allow auth tokens to be shared among connections to rackspace api.
  • Loading branch information
xtoddx authored and geemus committed Jun 20, 2011
1 parent d1dc40c commit 852ee37
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions lib/fog/compute/rackspace.rb
Expand Up @@ -4,6 +4,7 @@ class Rackspace < Fog::Service

requires :rackspace_api_key, :rackspace_username
recognizes :rackspace_auth_url, :rackspace_servicenet, :persistent
recognizes :rackspace_auth_token, :rackspace_management_url

model_path 'fog/compute/models/rackspace'
model :flavor
Expand Down Expand Up @@ -78,6 +79,9 @@ def initialize(options={})
@rackspace_username = options[:rackspace_username]
@rackspace_auth_url = options[:rackspace_auth_url]
@rackspace_servicenet = options[:rackspace_servicenet]
@rackspace_auth_token = options[:rackspace_auth_token]
@rackspace_management_url = options[:rackspace_management_url]
@rackspace_must_reauthenticate = false
authenticate
Excon.ssl_verify_peer = false if options[:rackspace_servicenet] == true
@connection = Fog::Connection.new("#{@scheme}://#{@host}:#{@port}", options[:persistent])
Expand All @@ -100,6 +104,7 @@ def request(params)
}))
rescue Excon::Errors::Unauthorized => error
if JSON.parse(response.body)['unauthorized']['message'] == 'Invalid authentication token. Please renew.'
@rackspace_must_reauthenticate = true
authenticate
retry
else
Expand All @@ -122,14 +127,19 @@ def request(params)
private

def authenticate
options = {
:rackspace_api_key => @rackspace_api_key,
:rackspace_username => @rackspace_username,
:rackspace_auth_url => @rackspace_auth_url
}
credentials = Fog::Rackspace.authenticate(options)
@auth_token = credentials['X-Auth-Token']
uri = URI.parse(credentials['X-Server-Management-Url'])
if @rackspace_must_reauthenticate or @rackspace_auth_token.empty?
options = {
:rackspace_api_key => @rackspace_api_key,
:rackspace_username => @rackspace_username,
:rackspace_auth_url => @rackspace_auth_url
}
credentials = Fog::Rackspace.authenticate(options)
@auth_token = credentials['X-Auth-Token']
uri = URI.parse(credentials['X-Server-Management-Url'])
else
@auth_token = @rackspace_auth_token
uri = URI.parse(@rackspace_management_url)
end
@host = @rackspace_servicenet == true ? "snet-#{uri.host}" : uri.host
@path = uri.path
@port = uri.port
Expand Down

0 comments on commit 852ee37

Please sign in to comment.