lib/fog/compute/rackspace.rb in fog-0.4.1 vs lib/fog/compute/rackspace.rb in fog-0.5.0

- old
+ new

@@ -98,17 +98,14 @@ warning << " [light_black](" << location << ")[/] " Formatador.display_line(warning) 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 def reload @connection.reset @@ -122,10 +119,17 @@ 'X-Auth-Token' => @auth_token }.merge!(params[:headers] || {}), :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 Fog::Rackspace::Compute::NotFound.slurp(error) else @@ -134,9 +138,26 @@ end unless response.body.empty? response.body = JSON.parse(response.body) end 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