diff --git a/source/lib/vagrant-openstack-provider/client/glance.rb b/source/lib/vagrant-openstack-provider/client/glance.rb index 16c0288..2256d2a 100644 --- a/source/lib/vagrant-openstack-provider/client/glance.rb +++ b/source/lib/vagrant-openstack-provider/client/glance.rb @@ -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 diff --git a/source/lib/vagrant-openstack-provider/client/http_utils.rb b/source/lib/vagrant-openstack-provider/client/http_utils.rb index 07e6798..1afe106 100644 --- a/source/lib/vagrant-openstack-provider/client/http_utils.rb +++ b/source/lib/vagrant-openstack-provider/client/http_utils.rb @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/source/lib/vagrant-openstack-provider/client/keystone.rb b/source/lib/vagrant-openstack-provider/client/keystone.rb index 7c16f89..63553be 100644 --- a/source/lib/vagrant-openstack-provider/client/keystone.rb +++ b/source/lib/vagrant-openstack-provider/client/keystone.rb @@ -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' diff --git a/source/lib/vagrant-openstack-provider/client/rest_utils.rb b/source/lib/vagrant-openstack-provider/client/rest_utils.rb index b599adb..76f1b5a 100644 --- a/source/lib/vagrant-openstack-provider/client/rest_utils.rb +++ b/source/lib/vagrant-openstack-provider/client/rest_utils.rb @@ -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