Skip to content

Commit

Permalink
Allow setting timeout value for REST calls
Browse files Browse the repository at this point in the history
Fix #183
  • Loading branch information
julienvey authored and ggiamarchi committed Jan 30, 2015
1 parent fc26a6a commit 670e815
Show file tree
Hide file tree
Showing 22 changed files with 173 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require 'log4r'
require 'restclient'
require 'json'

require 'vagrant-openstack-provider/client/openstack'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 0 additions & 1 deletion source/lib/vagrant-openstack-provider/client/cinder.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require 'log4r'
require 'restclient'
require 'json'

require 'vagrant-openstack-provider/client/http_utils'
Expand Down
1 change: 0 additions & 1 deletion source/lib/vagrant-openstack-provider/client/domain.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require 'log4r'
require 'restclient'
require 'json'

module VagrantPlugins
Expand Down
8 changes: 5 additions & 3 deletions source/lib/vagrant-openstack-provider/client/glance.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
1 change: 0 additions & 1 deletion source/lib/vagrant-openstack-provider/client/heat.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require 'log4r'
require 'restclient'
require 'json'

require 'vagrant-openstack-provider/client/http_utils'
Expand Down
13 changes: 7 additions & 6 deletions source/lib/vagrant-openstack-provider/client/http_utils.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
9 changes: 5 additions & 4 deletions source/lib/vagrant-openstack-provider/client/keystone.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require 'log4r'
require 'restclient'
require 'json'

require 'vagrant-openstack-provider/client/request_logger'
Expand Down Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion source/lib/vagrant-openstack-provider/client/neutron.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require 'log4r'
require 'restclient'
require 'json'

require 'vagrant-openstack-provider/client/http_utils'
Expand Down
1 change: 0 additions & 1 deletion source/lib/vagrant-openstack-provider/client/nova.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require 'log4r'
require 'restclient'
require 'json'

require 'vagrant-openstack-provider/client/http_utils'
Expand Down
1 change: 0 additions & 1 deletion source/lib/vagrant-openstack-provider/client/openstack.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require 'log4r'
require 'restclient'
require 'json'

require 'vagrant-openstack-provider/client/heat'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require 'log4r'
require 'restclient'
require 'json'

module VagrantPlugins
Expand Down
25 changes: 25 additions & 0 deletions source/lib/vagrant-openstack-provider/client/rest_utils.rb
Original file line number Diff line number Diff line change
@@ -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
9 changes: 9 additions & 0 deletions source/lib/vagrant-openstack-provider/config.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'vagrant'
require 'colorize'
require 'vagrant-openstack-provider/config/http'

module VagrantPlugins
module Openstack
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand Down
39 changes: 39 additions & 0 deletions source/lib/vagrant-openstack-provider/config/http.rb
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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',
Expand All @@ -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',
Expand Down Expand Up @@ -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',
Expand Down
18 changes: 17 additions & 1 deletion source/spec/vagrant-openstack-provider/client/cinder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 18 additions & 2 deletions source/spec/vagrant-openstack-provider/client/glance_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions source/spec/vagrant-openstack-provider/client/heat_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,21 @@
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' }
config.stub(:openstack_orchestration_url) { nil }
config.stub(:tenant_name) { 'testTenant' }
config.stub(:username) { 'username' }
config.stub(:password) { 'password' }
config.stub(:http) { http }
end
end

Expand Down
Loading

0 comments on commit 670e815

Please sign in to comment.