diff --git a/source/lib/vagrant-openstack-provider/action/connect_openstack.rb b/source/lib/vagrant-openstack-provider/action/connect_openstack.rb index be4aeff..6a5e169 100644 --- a/source/lib/vagrant-openstack-provider/action/connect_openstack.rb +++ b/source/lib/vagrant-openstack-provider/action/connect_openstack.rb @@ -1,5 +1,4 @@ require 'log4r' -require 'restclient' require 'json' require 'vagrant-openstack-provider/client/openstack' diff --git a/source/lib/vagrant-openstack-provider/catalog/openstack_catalog.rb b/source/lib/vagrant-openstack-provider/catalog/openstack_catalog.rb index 7c6192a..6662802 100644 --- a/source/lib/vagrant-openstack-provider/catalog/openstack_catalog.rb +++ b/source/lib/vagrant-openstack-provider/catalog/openstack_catalog.rb @@ -31,11 +31,11 @@ def read(env, catalog) end endpoints[:network] = choose_api_version('Neutron', 'openstack_network_url', 'v2') do - client.neutron.get_api_version_list(:network) + client.neutron.get_api_version_list(env, :network) end if config.openstack_network_url.nil? && !endpoints[:network].nil? endpoints[:image] = choose_api_version('Glance', 'openstack_image_url', nil, false) do - client.glance.get_api_version_list(:image) + client.glance.get_api_version_list(env) end if config.openstack_image_url.nil? && !endpoints[:image].nil? end diff --git a/source/lib/vagrant-openstack-provider/client/cinder.rb b/source/lib/vagrant-openstack-provider/client/cinder.rb index 1fb52b2..ca44397 100644 --- a/source/lib/vagrant-openstack-provider/client/cinder.rb +++ b/source/lib/vagrant-openstack-provider/client/cinder.rb @@ -1,5 +1,4 @@ require 'log4r' -require 'restclient' require 'json' require 'vagrant-openstack-provider/client/http_utils' diff --git a/source/lib/vagrant-openstack-provider/client/domain.rb b/source/lib/vagrant-openstack-provider/client/domain.rb index 189f105..dee4bdd 100644 --- a/source/lib/vagrant-openstack-provider/client/domain.rb +++ b/source/lib/vagrant-openstack-provider/client/domain.rb @@ -1,5 +1,4 @@ require 'log4r' -require 'restclient' require 'json' module VagrantPlugins diff --git a/source/lib/vagrant-openstack-provider/client/glance.rb b/source/lib/vagrant-openstack-provider/client/glance.rb index c476525..16c0288 100644 --- a/source/lib/vagrant-openstack-provider/client/glance.rb +++ b/source/lib/vagrant-openstack-provider/client/glance.rb @@ -1,8 +1,8 @@ require 'log4r' -require 'restclient' require 'json' require 'vagrant-openstack-provider/client/http_utils' +require 'vagrant-openstack-provider/client/rest_utils' require 'vagrant-openstack-provider/client/domain' module VagrantPlugins @@ -17,8 +17,10 @@ def initialize @session = VagrantPlugins::Openstack.session end - def get_api_version_list(_env) - json = RestClient.get(@session.endpoints[:image], 'X-Auth-Token' => @session.token, :accept => :json) do |response| + 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 diff --git a/source/lib/vagrant-openstack-provider/client/heat.rb b/source/lib/vagrant-openstack-provider/client/heat.rb index e8ed765..47ce288 100644 --- a/source/lib/vagrant-openstack-provider/client/heat.rb +++ b/source/lib/vagrant-openstack-provider/client/heat.rb @@ -1,5 +1,4 @@ require 'log4r' -require 'restclient' require 'json' require 'vagrant-openstack-provider/client/http_utils' diff --git a/source/lib/vagrant-openstack-provider/client/http_utils.rb b/source/lib/vagrant-openstack-provider/client/http_utils.rb index 2ac7782..07e6798 100644 --- a/source/lib/vagrant-openstack-provider/client/http_utils.rb +++ b/source/lib/vagrant-openstack-provider/client/http_utils.rb @@ -1,9 +1,9 @@ require 'log4r' -require 'restclient' require 'json' require 'vagrant-openstack-provider/client/keystone' require 'vagrant-openstack-provider/client/request_logger' +require 'vagrant-openstack-provider/client/rest_utils' module VagrantPlugins module Openstack @@ -19,7 +19,7 @@ def get(env, url, headers = {}) log_request(:GET, url, headers) authenticated(env) do - RestClient.get(url, headers) { |res| handle_response(res) }.tap do + RestUtils.get(env, url, headers) { |res| handle_response(res) }.tap do @logger.debug("#{calling_method} - end") end end @@ -34,7 +34,7 @@ def post(env, url, body = nil, headers = {}) log_request(:POST, url, body, headers) authenticated(env) do - RestClient.post(url, body, headers) { |res| handle_response(res) }.tap do + RestUtils.post(env, url, body, headers) { |res| handle_response(res) }.tap do @logger.debug("#{calling_method} - end") end end @@ -49,17 +49,18 @@ def delete(env, url, headers = {}) log_request(:DELETE, url, headers) authenticated(env) do - RestClient.delete(url, headers) { |res| handle_response(res) }.tap do + RestUtils.delete(env, url, headers) { |res| handle_response(res) }.tap do @logger.debug("#{calling_method} - end") end end end - def get_api_version_list(service_type) + def get_api_version_list(env, service_type) url = @session.endpoints[service_type] headers = { 'X-Auth-Token' => @session.token, :accept => :json } log_request(:GET, url, headers) - json = RestClient.get(url, headers) do |response| + + json = RestUtils.get(env, url, headers) do |response| log_response(response) case response.code when 200, 300 diff --git a/source/lib/vagrant-openstack-provider/client/keystone.rb b/source/lib/vagrant-openstack-provider/client/keystone.rb index c91d242..ad8d591 100644 --- a/source/lib/vagrant-openstack-provider/client/keystone.rb +++ b/source/lib/vagrant-openstack-provider/client/keystone.rb @@ -1,5 +1,4 @@ require 'log4r' -require 'restclient' require 'json' require 'vagrant-openstack-provider/client/request_logger' @@ -37,9 +36,11 @@ def authenticate(env) post_body[:auth][:passwordCredentials][:password] = config.password - authentication = RestClient.post(config.openstack_auth_url, post_body.to_json, - content_type: :json, - accept: :json) do |response| + authentication = RestUtils.post(env, config.openstack_auth_url, post_body.to_json, + content_type: :json, + accept: :json, + timeout: config.http.read_timeout, + open_timeout: config.http.open_timeout) do |response| log_response(response) case response.code when 200 diff --git a/source/lib/vagrant-openstack-provider/client/neutron.rb b/source/lib/vagrant-openstack-provider/client/neutron.rb index 3d16d63..e8cac60 100644 --- a/source/lib/vagrant-openstack-provider/client/neutron.rb +++ b/source/lib/vagrant-openstack-provider/client/neutron.rb @@ -1,5 +1,4 @@ require 'log4r' -require 'restclient' require 'json' require 'vagrant-openstack-provider/client/http_utils' diff --git a/source/lib/vagrant-openstack-provider/client/nova.rb b/source/lib/vagrant-openstack-provider/client/nova.rb index c3d9c83..afafb02 100644 --- a/source/lib/vagrant-openstack-provider/client/nova.rb +++ b/source/lib/vagrant-openstack-provider/client/nova.rb @@ -1,5 +1,4 @@ require 'log4r' -require 'restclient' require 'json' require 'vagrant-openstack-provider/client/http_utils' diff --git a/source/lib/vagrant-openstack-provider/client/openstack.rb b/source/lib/vagrant-openstack-provider/client/openstack.rb index 2030188..f612169 100644 --- a/source/lib/vagrant-openstack-provider/client/openstack.rb +++ b/source/lib/vagrant-openstack-provider/client/openstack.rb @@ -1,5 +1,4 @@ require 'log4r' -require 'restclient' require 'json' require 'vagrant-openstack-provider/client/heat' diff --git a/source/lib/vagrant-openstack-provider/client/request_logger.rb b/source/lib/vagrant-openstack-provider/client/request_logger.rb index a15d2e9..8112dcf 100644 --- a/source/lib/vagrant-openstack-provider/client/request_logger.rb +++ b/source/lib/vagrant-openstack-provider/client/request_logger.rb @@ -1,5 +1,4 @@ require 'log4r' -require 'restclient' require 'json' module VagrantPlugins diff --git a/source/lib/vagrant-openstack-provider/client/rest_utils.rb b/source/lib/vagrant-openstack-provider/client/rest_utils.rb new file mode 100644 index 0000000..aed6372 --- /dev/null +++ b/source/lib/vagrant-openstack-provider/client/rest_utils.rb @@ -0,0 +1,25 @@ +require 'restclient' + +module VagrantPlugins + module Openstack + module RestUtils + def self.get(env, url, headers = {}, &block) + config = env[:machine].provider_config + RestClient::Request.execute(method: :get, url: url, headers: headers, + timeout: config.http.read_timeout, open_timeout: config.http.open_timeout, &block) + end + + def self.post(env, url, payload, headers = {}, &block) + config = env[:machine].provider_config + RestClient::Request.execute(method: :post, url: url, payload: payload, headers: headers, + timeout: config.http.read_timeout, open_timeout: config.http.open_timeout, &block) + end + + def self.delete(env, url, headers = {}, &block) + config = env[:machine].provider_config + RestClient::Request.execute(method: :delete, url: url, headers: headers, + timeout: config.http.read_timeout, open_timeout: config.http.open_timeout, &block) + end + end + end +end diff --git a/source/lib/vagrant-openstack-provider/config.rb b/source/lib/vagrant-openstack-provider/config.rb index 40beb65..d34b8ab 100644 --- a/source/lib/vagrant-openstack-provider/config.rb +++ b/source/lib/vagrant-openstack-provider/config.rb @@ -1,5 +1,6 @@ require 'vagrant' require 'colorize' +require 'vagrant-openstack-provider/config/http' module VagrantPlugins module Openstack @@ -189,6 +190,10 @@ class Config < Vagrant.plugin('2', :config) # @return [Integer] attr_accessor :stack_delete_timeout + # + # @return [HttpConfig] + attr_accessor :http + def initialize @password = UNSET_VALUE @openstack_compute_url = UNSET_VALUE @@ -230,6 +235,7 @@ def initialize @server_delete_timeout = UNSET_VALUE @stack_create_timeout = UNSET_VALUE @stack_delete_timeout = UNSET_VALUE + @http = HttpConfig.new end def merge(other) @@ -254,6 +260,8 @@ def merge(other) if [:@networks, :@volumes, :@rsync_includes, :@ignore_files, :@floating_ip_pool, :@stacks].include? key result.instance_variable_set(key, value) unless value.empty? + elsif [:@http].include? key + result.instance_variable_set(key, instance_variable_get(key).merge(other.instance_variable_get(key))) if value != UNSET_VALUE else result.instance_variable_set(key, value) if value != UNSET_VALUE end @@ -313,6 +321,7 @@ def finalize! @networks = nil if @networks.empty? @volumes = nil if @volumes.empty? @stacks = nil if @stacks.empty? + @http.finalize! end # rubocop:enable Style/CyclomaticComplexity diff --git a/source/lib/vagrant-openstack-provider/config/http.rb b/source/lib/vagrant-openstack-provider/config/http.rb new file mode 100644 index 0000000..ffd0076 --- /dev/null +++ b/source/lib/vagrant-openstack-provider/config/http.rb @@ -0,0 +1,39 @@ +module VagrantPlugins + module Openstack + class HttpConfig + UNSET_VALUE = Vagrant.plugin('2', :config).const_get(:UNSET_VALUE) + + # + # @return [Integer] + attr_accessor :open_timeout + + # + # @return [Integer] + attr_accessor :read_timeout + + def initialize + @open_timeout = UNSET_VALUE + @read_timeout = UNSET_VALUE + end + + def finalize! + @open_timeout = 60 if @open_timeout == UNSET_VALUE + @read_timeout = 30 if @read_timeout == UNSET_VALUE + end + + def merge(other) + result = self.class.new + + [self, other].each do |obj| + obj.instance_variables.each do |key| + next if key.to_s.start_with?('@__') + + value = obj.instance_variable_get(key) + result.instance_variable_set(key, value) if value != UNSET_VALUE + end + end + result + end + end + end +end diff --git a/source/spec/vagrant-openstack-provider/action/connect_openstack_spec.rb b/source/spec/vagrant-openstack-provider/action/connect_openstack_spec.rb index 9e7c88d..fa45b7a 100644 --- a/source/spec/vagrant-openstack-provider/action/connect_openstack_spec.rb +++ b/source/spec/vagrant-openstack-provider/action/connect_openstack_spec.rb @@ -28,7 +28,7 @@ let(:neutron) do double.tap do |neutron| - neutron.stub(:get_api_version_list).with(anything) do + neutron.stub(:get_api_version_list).with(anything, anything) do [ { 'status' => 'CURRENT', @@ -47,7 +47,7 @@ let(:neutron_admin_url) do double.tap do |neutron| - neutron.stub(:get_api_version_list).with(anything) do + neutron.stub(:get_api_version_list).with(anything, anything) do [ { 'status' => 'CURRENT', @@ -66,7 +66,7 @@ let(:neutron_france) do double.tap do |neutron| - neutron.stub(:get_api_version_list).with(anything) do + neutron.stub(:get_api_version_list).with(anything, anything) do [ { 'status' => 'CURRENT', @@ -606,7 +606,7 @@ let(:neutron) do double.tap do |neutron| - neutron.stub(:get_api_version_list).with(anything) do + neutron.stub(:get_api_version_list).with(anything, anything) do [ { 'status' => 'CURRENT', diff --git a/source/spec/vagrant-openstack-provider/client/cinder_spec.rb b/source/spec/vagrant-openstack-provider/client/cinder_spec.rb index 85a67c3..75c14d7 100644 --- a/source/spec/vagrant-openstack-provider/client/cinder_spec.rb +++ b/source/spec/vagrant-openstack-provider/client/cinder_spec.rb @@ -2,8 +2,24 @@ describe VagrantPlugins::Openstack::CinderClient do + let(:http) do + double('http').tap do |http| + http.stub(:read_timeout) { 42 } + http.stub(:open_timeout) { 43 } + end + end + + let(:config) do + double('config').tap do |config| + config.stub(:http) { http } + end + end + let(:env) do - Hash.new + Hash.new.tap do |env| + env[:machine] = double('machine') + env[:machine].stub(:provider_config) { config } + end end let(:session) do diff --git a/source/spec/vagrant-openstack-provider/client/glance_spec.rb b/source/spec/vagrant-openstack-provider/client/glance_spec.rb index c2ba664..36066e9 100644 --- a/source/spec/vagrant-openstack-provider/client/glance_spec.rb +++ b/source/spec/vagrant-openstack-provider/client/glance_spec.rb @@ -2,8 +2,24 @@ describe VagrantPlugins::Openstack::GlanceClient do + let(:http) do + double('http').tap do |http| + http.stub(:read_timeout) { 42 } + http.stub(:open_timeout) { 43 } + end + end + + let(:config) do + double('config').tap do |config| + config.stub(:http) { http } + end + end + let(:env) do - Hash.new + Hash.new.tap do |env| + env[:machine] = double('machine') + env[:machine].stub(:provider_config) { config } + end end let(:session) do @@ -120,7 +136,7 @@ } ]}') - versions = @glance_client.get_api_version_list(:image) + versions = @glance_client.get_api_version_list(env) expect(versions.size).to eq(2) end diff --git a/source/spec/vagrant-openstack-provider/client/heat_spec.rb b/source/spec/vagrant-openstack-provider/client/heat_spec.rb index 7fdf424..ee79d1e 100644 --- a/source/spec/vagrant-openstack-provider/client/heat_spec.rb +++ b/source/spec/vagrant-openstack-provider/client/heat_spec.rb @@ -3,6 +3,13 @@ describe VagrantPlugins::Openstack::NovaClient do include FakeFS::SpecHelpers::All + let(:http) do + double('http').tap do |http| + http.stub(:read_timeout) { 42 } + http.stub(:open_timeout) { 43 } + end + end + let(:config) do double('config').tap do |config| config.stub(:openstack_auth_url) { 'http://heatAuthV2' } @@ -10,6 +17,7 @@ config.stub(:tenant_name) { 'testTenant' } config.stub(:username) { 'username' } config.stub(:password) { 'password' } + config.stub(:http) { http } end end diff --git a/source/spec/vagrant-openstack-provider/client/keystone_spec.rb b/source/spec/vagrant-openstack-provider/client/keystone_spec.rb index 1b2a244..b829935 100644 --- a/source/spec/vagrant-openstack-provider/client/keystone_spec.rb +++ b/source/spec/vagrant-openstack-provider/client/keystone_spec.rb @@ -2,6 +2,13 @@ describe VagrantPlugins::Openstack::KeystoneClient do + let(:http) do + double('http').tap do |http| + http.stub(:read_timeout) { 42 } + http.stub(:open_timeout) { 43 } + end + end + let(:config) do double('config').tap do |config| config.stub(:openstack_auth_url) { 'http://keystoneAuthV2' } @@ -10,6 +17,7 @@ config.stub(:tenant_name) { 'testTenant' } config.stub(:username) { 'username' } config.stub(:password) { 'password' } + config.stub(:http) { http } end end diff --git a/source/spec/vagrant-openstack-provider/client/neutron_spec.rb b/source/spec/vagrant-openstack-provider/client/neutron_spec.rb index 3211367..8e4eff9 100644 --- a/source/spec/vagrant-openstack-provider/client/neutron_spec.rb +++ b/source/spec/vagrant-openstack-provider/client/neutron_spec.rb @@ -2,8 +2,24 @@ describe VagrantPlugins::Openstack::NeutronClient do + let(:http) do + double('http').tap do |http| + http.stub(:read_timeout) { 42 } + http.stub(:open_timeout) { 43 } + end + end + + let(:config) do + double('config').tap do |config| + config.stub(:http) { http } + end + end + let(:env) do - Hash.new + Hash.new.tap do |env| + env[:machine] = double('machine') + env[:machine].stub(:provider_config) { config } + end end let(:session) do @@ -150,7 +166,7 @@ } ]}') - versions = @neutron_client.get_api_version_list(:network) + versions = @neutron_client.get_api_version_list(env, :network) expect(versions.size).to eq(2) end diff --git a/source/spec/vagrant-openstack-provider/client/nova_spec.rb b/source/spec/vagrant-openstack-provider/client/nova_spec.rb index 1ca300b..221e673 100644 --- a/source/spec/vagrant-openstack-provider/client/nova_spec.rb +++ b/source/spec/vagrant-openstack-provider/client/nova_spec.rb @@ -6,6 +6,13 @@ let(:filename) { 'key.pub' } let(:ssh_key_content) { 'my public key' } + let(:http) do + double('http').tap do |http| + http.stub(:read_timeout) { 42 } + http.stub(:open_timeout) { 43 } + end + end + let(:config) do double('config').tap do |config| config.stub(:openstack_auth_url) { 'http://novaAuthV2' } @@ -13,6 +20,7 @@ config.stub(:tenant_name) { 'testTenant' } config.stub(:username) { 'username' } config.stub(:password) { 'password' } + config.stub(:http) { http } end end