Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

[rackspace|compute] get new auth token when one expires

  • Loading branch information...
commit 8897f1d1029b019f9bcc33d9ae3138bbe4e6e26b 1 parent 923f449
@geemus authored
Showing with 28 additions and 7 deletions.
  1. +28 −7 lib/fog/compute/rackspace.rb
View
35 lib/fog/compute/rackspace.rb
@@ -100,13 +100,10 @@ def initialize(options={})
end
require 'json'
- credentials = Fog::Rackspace.authenticate(options)
- @auth_token = credentials['X-Auth-Token']
- uri = URI.parse(credentials['X-Server-Management-Url'])
- @host = uri.host
- @path = uri.path
- @port = uri.port
- @scheme = uri.scheme
+ @rackspace_api_key = options[:rackspace_api_key]
+ @rackspace_username = options[:rackspace_username]
+ @rackspace_auth_url = options[:rackspace_auth_url]
+ authenticate
@connection = Fog::Connection.new("#{@scheme}://#{@host}:#{@port}", options[:persistent])
end
@@ -124,6 +121,13 @@ def request(params)
:host => @host,
:path => "#{@path}/#{params[:path]}"
}))
+ rescue Excon::Errors::Unauthorized => error
+ if JSON.parse(response.body)['unauthorized']['message'] == 'Invalid authentication token. Please renew.'
+ authenticate
+ retry
+ else
+ raise error
+ end
rescue Excon::Errors::HTTPStatusError => error
raise case error
when Excon::Errors::NotFound
@@ -138,6 +142,23 @@ def request(params)
response
end
+ 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'])
+ @host = uri.host
+ @path = uri.path
+ @port = uri.port
+ @scheme = uri.scheme
+ end
+
end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.