Skip to content

Commit

Permalink
Allow request api_version override
Browse files Browse the repository at this point in the history
This is an experimental patch that provides a caller two ways to override
a request's api_version.

First method: In the call to a request.

   compute_service.request :create_server, :api_version => v1 { ... }

Second method: In the configuration file.

   production:
     provider: openstack
     auth_service:
       # declare auth service here
     compute_service:
       api_version: v2

Change-Id: Ic951a3a222eecc250219cb7ec891f038f7e167ea
  • Loading branch information
relaxdiego committed Jul 10, 2014
1 parent fd7f489 commit 1517042
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 10 deletions.
11 changes: 8 additions & 3 deletions lib/aviator/core/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ def initialize


class UnknownRequestError < StandardError
def initialize(request_name)
super "Unknown request #{ request_name }."
def initialize(request_name, options)
super "Unknown request #{ request_name } #{ options }."
end
end

Expand Down Expand Up @@ -60,6 +60,7 @@ def initialize(opts={})
@provider = opts[:provider] || (raise ProviderNotDefinedError.new)
@service = opts[:service] || (raise ServiceNameNotDefinedError.new)
@log_file = opts[:log_file]
@default_options = opts[:default_options] || {}

@default_session_data = opts[:default_session_data]

Expand All @@ -68,6 +69,10 @@ def initialize(opts={})


def request(request_name, options={}, &params)
if options[:api_version].nil? && @default_options[:api_version]
options[:api_version] = @default_options[:api_version]
end

session_data = options[:session_data] || default_session_data

raise SessionDataNotProvidedError.new unless session_data
Expand All @@ -78,7 +83,7 @@ def request(request_name, options={}, &params)

request_class = provider_module.find_request(service, request_name, session_data, options)

raise UnknownRequestError.new(request_name) unless request_class
raise UnknownRequestError.new(request_name, options) unless request_class

request = request_class.new(session_data, &params)

Expand Down
17 changes: 11 additions & 6 deletions lib/aviator/core/session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,17 @@ def get_service_obj(service_name)

@services ||= {}

@services[service_name] ||= Service.new(
:provider => environment[:provider],
:service => service_name,
:default_session_data => auth_info,
:log_file => log_file
)
if @services[service_name].nil?
default_options = environment["#{ service_name }_service"]

@services[service_name] = Service.new(
:provider => environment[:provider],
:service => service_name,
:default_session_data => auth_info,
:default_options => default_options,
:log_file => log_file
)
end

@services[service_name]
end
Expand Down
7 changes: 6 additions & 1 deletion lib/aviator/openstack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ def find_request(service, name, session_data, options)
namespace = Aviator.const_get('Openstack') \
.const_get(service.camelize)

version = infer_version(session_data, name, service).to_s.camelize
if options[:api_version]
m = options[:api_version].to_s.match(/(v\d+)\.?\d*/)
version = m[1].to_s.camelize unless m.nil?
end

version ||= infer_version(session_data, name, service).to_s.camelize

return nil unless version && namespace.const_defined?(version)

Expand Down
23 changes: 23 additions & 0 deletions test/aviator/openstack_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,29 @@ def modyul
end


it 'can find the correct request class if based on the provided version' do
load_service

bootstrap = {
:auth_service => {
:name => 'identity',
:host_uri => 'http://devstack:5000',
:request => 'create_token'
}
}
api_version = :v2
request_class = modyul.find_request(
bootstrap[:auth_service][:name],
bootstrap[:auth_service][:request],
bootstrap,
{ :api_version => api_version }
)

request_class.wont_be_nil
request_class.api_version.must_equal api_version
end


it 'raises an error if session data does not have the endpoint information' do
load_service
request_name = config[:auth_service][:request].to_sym
Expand Down

0 comments on commit 1517042

Please sign in to comment.