diff --git a/lib/fog/compute/rackspace.rb b/lib/fog/compute/rackspace.rb index 3793838162..a84af97d76 100644 --- a/lib/fog/compute/rackspace.rb +++ b/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