Skip to content

Commit

Permalink
Follow HTTP redirection
Browse files Browse the repository at this point in the history
Fixes #393
Fixes #171
  • Loading branch information
ggiamarchi committed Nov 24, 2022
1 parent b6a51c1 commit 364e619
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 57 deletions.
21 changes: 9 additions & 12 deletions source/lib/vagrant-openstack-provider/client/glance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,15 @@ def initialize
end

def get_api_version_list(env)
json = RestUtils.get(env, @session.endpoints[:image],
'X-Auth-Token' => @session.token,
:accept => :json) do |response|
log_response(response)
case response.code
when 200, 300
response
when 401
fail Errors::AuthenticationFailed
else
fail Errors::VagrantOpenstackError, message: response.to_s
end
response = RestUtils.get(env, @session.endpoints[:image], 'X-Auth-Token' => @session.token, :accept => :json)
log_response(response)
case response.code
when 200, 300
json = response
when 401
fail Errors::AuthenticationFailed
else
fail Errors::VagrantOpenstackError, message: response.to_s
end
JSON.parse(json)['versions']
end
Expand Down
41 changes: 22 additions & 19 deletions source/lib/vagrant-openstack-provider/client/http_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ def get(env, url, headers = {})
log_request(:GET, url, headers)

authenticated(env) do
RestUtils.get(env, url, headers) { |res| handle_response(res) }.tap do
@logger.debug("#{calling_method} - end")
end
res = RestUtils.get(env, url, headers)
handle_response(res)
@logger.debug("#{calling_method} - end")
res
end
end

Expand All @@ -34,9 +35,10 @@ def post(env, url, body = nil, headers = {})
log_request(:POST, url, body, headers)

authenticated(env) do
RestUtils.post(env, url, body, headers) { |res| handle_response(res) }.tap do
@logger.debug("#{calling_method} - end")
end
res = RestUtils.post(env, url, body, headers)
handle_response(res)
@logger.debug("#{calling_method} - end")
res
end
end

Expand All @@ -49,9 +51,10 @@ def delete(env, url, headers = {})
log_request(:DELETE, url, headers)

authenticated(env) do
RestUtils.delete(env, url, headers) { |res| handle_response(res) }.tap do
@logger.debug("#{calling_method} - end")
end
res = RestUtils.delete(env, url, headers)
handle_response(res)
@logger.debug("#{calling_method} - end")
res
end
end

Expand All @@ -60,17 +63,17 @@ def get_api_version_list(env, service_type)
headers = { 'X-Auth-Token' => @session.token, :accept => :json }
log_request(:GET, url, headers)

json = RestUtils.get(env, url, headers) do |response|
log_response(response)
case response.code
when 200, 300
response
when 401
fail Errors::AuthenticationFailed
else
fail Errors::VagrantOpenstackError, message: response.to_s
end
response = RestUtils.get(env, url, headers)
log_response(response)
case response.code
when 200, 300
json = response
when 401
fail Errors::AuthenticationFailed
else
fail Errors::VagrantOpenstackError, message: response.to_s
end

JSON.parse(json)['versions']
end

Expand Down
25 changes: 11 additions & 14 deletions source/lib/vagrant-openstack-provider/client/keystone.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,17 @@ def authenticate(env)
post_body[:auth][:identity][:password][:user][:password] = config.password
end

authentication = RestUtils.post(env, auth_url, post_body.to_json, headers) do |response|
log_response(response)
case response.code
when 200
response
when 201
response
when 401
fail Errors::AuthenticationFailed
when 404
fail Errors::BadAuthenticationEndpoint
else
fail Errors::VagrantOpenstackError, message: response.to_s
end
response = RestUtils.post(env, auth_url, post_body.to_json, headers)
log_response(response)
case response.code
when 200, 201
authentication = response
when 401
fail Errors::AuthenticationFailed
when 404
fail Errors::BadAuthenticationEndpoint
else
fail Errors::VagrantOpenstackError, message: response.to_s
end

if config.identity_api_version == '2'
Expand Down
42 changes: 30 additions & 12 deletions source/lib/vagrant-openstack-provider/client/rest_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,46 @@ def self._set_proxy(config)
end
end

def self.get(env, url, headers = {}, &block)
def self.get(env, url, headers = {})
config = env[:machine].provider_config
_set_proxy(config)
RestClient::Request.execute(method: :get, url: url, headers: headers,
timeout: config.http.read_timeout, open_timeout: config.http.open_timeout,
ssl_ca_file: config.ssl_ca_file, verify_ssl: config.ssl_verify_peer, &block)
begin
resp = RestClient::Request.execute(method: :get, url: url, headers: headers,
timeout: config.http.read_timeout, open_timeout: config.http.open_timeout,
ssl_ca_file: config.ssl_ca_file, verify_ssl: config.ssl_verify_peer)
rescue RestClient::ExceptionWithResponse => err
resp = err.response
end

resp
end

def self.post(env, url, payload, headers = {}, &block)
def self.post(env, url, payload, headers = {})
config = env[:machine].provider_config
_set_proxy(config)
RestClient::Request.execute(method: :post, url: url, payload: payload, headers: headers,
timeout: config.http.read_timeout, open_timeout: config.http.open_timeout,
ssl_ca_file: config.ssl_ca_file, verify_ssl: config.ssl_verify_peer, &block)
begin
resp = RestClient::Request.execute(method: :post, url: url, payload: payload, headers: headers,
timeout: config.http.read_timeout, open_timeout: config.http.open_timeout,
ssl_ca_file: config.ssl_ca_file, verify_ssl: config.ssl_verify_peer)
rescue RestClient::ExceptionWithResponse => err
resp = err.response
end

resp
end

def self.delete(env, url, headers = {}, &block)
def self.delete(env, url, headers = {})
config = env[:machine].provider_config
_set_proxy(config)
RestClient::Request.execute(method: :delete, url: url, headers: headers,
timeout: config.http.read_timeout, open_timeout: config.http.open_timeout,
ssl_ca_file: config.ssl_ca_file, verify_ssl: config.ssl_verify_peer, &block)
begin
resp = RestClient::Request.execute(method: :delete, url: url, headers: headers,
timeout: config.http.read_timeout, open_timeout: config.http.open_timeout,
ssl_ca_file: config.ssl_ca_file, verify_ssl: config.ssl_verify_peer)
rescue RestClient::ExceptionWithResponse => err
resp = err.response
end

resp
end
end
end
Expand Down

0 comments on commit 364e619

Please sign in to comment.