Skip to content

Commit 8897f1d

Browse files
committed
[rackspace|compute] get new auth token when one expires
1 parent 923f449 commit 8897f1d

File tree

1 file changed

+28
-7
lines changed

1 file changed

+28
-7
lines changed

lib/fog/compute/rackspace.rb

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,10 @@ def initialize(options={})
100100
end
101101

102102
require 'json'
103-
credentials = Fog::Rackspace.authenticate(options)
104-
@auth_token = credentials['X-Auth-Token']
105-
uri = URI.parse(credentials['X-Server-Management-Url'])
106-
@host = uri.host
107-
@path = uri.path
108-
@port = uri.port
109-
@scheme = uri.scheme
103+
@rackspace_api_key = options[:rackspace_api_key]
104+
@rackspace_username = options[:rackspace_username]
105+
@rackspace_auth_url = options[:rackspace_auth_url]
106+
authenticate
110107
@connection = Fog::Connection.new("#{@scheme}://#{@host}:#{@port}", options[:persistent])
111108
end
112109

@@ -124,6 +121,13 @@ def request(params)
124121
:host => @host,
125122
:path => "#{@path}/#{params[:path]}"
126123
}))
124+
rescue Excon::Errors::Unauthorized => error
125+
if JSON.parse(response.body)['unauthorized']['message'] == 'Invalid authentication token. Please renew.'
126+
authenticate
127+
retry
128+
else
129+
raise error
130+
end
127131
rescue Excon::Errors::HTTPStatusError => error
128132
raise case error
129133
when Excon::Errors::NotFound
@@ -138,6 +142,23 @@ def request(params)
138142
response
139143
end
140144

145+
private
146+
147+
def authenticate
148+
options = {
149+
:rackspace_api_key => @rackspace_api_key,
150+
:rackspace_username => @rackspace_username,
151+
:rackspace_auth_url => @rackspace_auth_url
152+
}
153+
credentials = Fog::Rackspace.authenticate(options)
154+
@auth_token = credentials['X-Auth-Token']
155+
uri = URI.parse(credentials['X-Server-Management-Url'])
156+
@host = uri.host
157+
@path = uri.path
158+
@port = uri.port
159+
@scheme = uri.scheme
160+
end
161+
141162
end
142163
end
143164
end

0 commit comments

Comments
 (0)