Permalink
Browse files

Allow auth tokens to be shared among connections to rackspace api.

  • Loading branch information...
1 parent d1dc40c commit 852ee37f85d606a38926eb39f38d515a5e05b9b7 @xtoddx xtoddx committed with geemus Jun 15, 2011
Showing with 18 additions and 8 deletions.
  1. +18 −8 lib/fog/compute/rackspace.rb
@@ -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
@@ -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])
@@ -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
@@ -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

0 comments on commit 852ee37

Please sign in to comment.