Skip to content
This repository has been archived by the owner on Aug 29, 2023. It is now read-only.

Commit

Permalink
Merge branch 'fix/1' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
relaxdiego committed Aug 31, 2013
2 parents cc59009 + f2212eb commit ede18a1
Show file tree
Hide file tree
Showing 16 changed files with 243 additions and 63 deletions.
1 change: 1 addition & 0 deletions lib/aviator/core.rb
Expand Up @@ -3,6 +3,7 @@
require 'faraday'

require "aviator/version"
require "aviator/core/dsl"
require "aviator/core/request"
require "aviator/core/response"
require "aviator/core/service"
Expand Down
37 changes: 37 additions & 0 deletions lib/aviator/core/dsl.rb
@@ -0,0 +1,37 @@
module Aviator

class << self

def define_request(request_name, &block)
class_obj = Class.new(Request, &block)

build_or_get_request_class(
Aviator,
class_obj,

class_obj.provider,
class_obj.service,
class_obj.api_version,
class_obj.endpoint_type,
request_name
)
end


private

def build_or_get_request_class(base, obj, *hierarchy)
const_name = hierarchy.shift.to_s.camelize

const = if base.const_defined?(const_name)
base.const_get(const_name)
else
base.const_set(const_name, (hierarchy.empty? ? obj : Module.new))
end

hierarchy.empty? ? const : build_or_get_request_class(const, obj, *hierarchy)
end

end # class << self

end # module Aviator
18 changes: 18 additions & 0 deletions lib/aviator/core/request.rb
Expand Up @@ -191,6 +191,15 @@ def params_class
end


def provider(value=nil)
if value
@provider = value
else
@provider
end
end


def optional_params
@optional_params ||= []
end
Expand All @@ -206,6 +215,15 @@ def required_params
end


def service(value=nil)
if value
@service = value
else
@service
end
end


def url?
instance_methods.include? :url
end
Expand Down
72 changes: 36 additions & 36 deletions lib/aviator/core/service.rb
Expand Up @@ -49,25 +49,25 @@ def initialize(app, logger=nil)
# request is initialized it doesn't get polluted by instance variables and methods
# of the containing class. This builder class makes that happen by being a
# scope gate for the file. See Metaprogramming Ruby, specifically on blocks and scope
class RequestBuilder

# This method gets called by the request file eval'd in self.build below
def define_request(request_name, &block)
klass = Class.new(Aviator::Request, &block)
return klass, request_name
end


def self.build(path_to_request_file)
clean_room = new
clean_room.instance_eval(File.read(path_to_request_file))
end


private_class_method :new

end

# class RequestBuilder
#
# # This method gets called by the request file eval'd in self.build below
# def define_request(request_name, &block)
# klass = Class.new(Aviator::Request, &block)
# return klass, request_name
# end
#
#
# def self.build(path_to_request_file)
# # clean_room = new
# # clean_room.instance_eval(File.read(path_to_request_file))
# Kernel.load(path_to_request_file, true)
# end
#
#
# private_class_method :new
#
# end

attr_accessor :default_session_data

Expand Down Expand Up @@ -134,19 +134,27 @@ def http_connection
# Candidate for extraction to aviator/openstack
def find_request(name, session_data, endpoint_type=nil)
endpoint_types = if endpoint_type
[endpoint_type.to_sym]
[endpoint_type.to_s.camelize]
else
[:public, :admin]
['Public', 'Admin']
end

version = infer_version(session_data)
namespace = Aviator.const_get(provider.camelize)
.const_get(service.camelize)

return nil unless version && requests[version]
version = infer_version(session_data).to_s.camelize

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

namespace = namespace.const_get(version)

endpoint_types.each do |endpoint_type|
next unless requests[version][endpoint_type]
pair = requests[version][endpoint_type].find{ |k, v| k == name }
return pair[1] unless pair.nil?
name = name.to_s.camelize

next unless namespace.const_defined?(endpoint_type)
next unless namespace.const_get(endpoint_type).const_defined?(name)

return namespace.const_get(endpoint_type).const_get(name)
end

nil
Expand Down Expand Up @@ -182,22 +190,14 @@ def load_requests
).expand_path
)

@requests ||= {}

request_file_paths.each do |path_to_file|
klass, request_name = RequestBuilder.build(path_to_file)

api_version = @requests[klass.api_version] ||= {}
endpoint_type = api_version[klass.endpoint_type] ||= {}
endpoint_type[request_name] = klass
end
request_file_paths.each{ |path| Kernel.load(path, true) }
end


def log_file
@log_file
end

end

end
7 changes: 5 additions & 2 deletions lib/aviator/openstack/compute/v2/public/list_images.rb
@@ -1,7 +1,10 @@
define_request :list_images do
Aviator.define_request :list_images do

endpoint_type :public
provider :openstack
service :compute
api_version :v2
endpoint_type :public

http_method :get

link_to 'documentation',
Expand Down
7 changes: 5 additions & 2 deletions lib/aviator/openstack/identity/v2/admin/create_tenant.rb
@@ -1,7 +1,10 @@
define_request :create_tenant do
Aviator.define_request :create_tenant do

endpoint_type :admin
provider :openstack
service :identity
api_version :v2
endpoint_type :admin

http_method :post

link_to 'documentation',
Expand Down
7 changes: 5 additions & 2 deletions lib/aviator/openstack/identity/v2/admin/list_tenants.rb
@@ -1,7 +1,10 @@
define_request :list_tenants do
Aviator.define_request :list_tenants do

endpoint_type :admin
provider :openstack
service :identity
api_version :v2
endpoint_type :admin

http_method :get

link_to 'documentation',
Expand Down
7 changes: 5 additions & 2 deletions lib/aviator/openstack/identity/v2/public/create_token.rb
@@ -1,9 +1,12 @@
define_request :create_token do
Aviator.define_request :create_token do

anonymous

endpoint_type :public
provider :openstack
service :identity
api_version :v2
endpoint_type :public

http_method :post

link_to 'documentation',
Expand Down
7 changes: 5 additions & 2 deletions lib/aviator/openstack/identity/v2/public/list_tenants.rb
@@ -1,7 +1,10 @@
define_request :list_tenants do
Aviator.define_request :list_tenants do

endpoint_type :public
provider :openstack
service :identity
api_version :v2
endpoint_type :public

http_method :get

link_to 'documentation',
Expand Down
83 changes: 83 additions & 0 deletions test.rb
@@ -0,0 +1,83 @@
require 'pry'
require 'active_support/inflector'

module Aviator

class << self

def define_request(request_name, &block)
class_obj = Class.new(Object, &block)

build_or_get_request_class(
Aviator,
class_obj,

class_obj.provider,
class_obj.service,
class_obj.api_version,
class_obj.endpoint_type,
request_name
)
end


private

def build_or_get_request_class(base, obj, *hierarchy)
const_name = hierarchy.shift.to_s.classify

const = if base.const_defined?(const_name)
base.const_get(const_name)
else
base.const_set(const_name, (hierarchy.empty? ? obj : Module.new))
end

hierarchy.empty? ? const : build_or_get_request_class(const, obj, *hierarchy)
end

end # class << self

end # module Aviator

test1 = Aviator.define_request :test1 do

def self.provider
:openstack
end

def self.service
:identity
end

def self.api_version
:v2
end

def self.endpoint_type
:admin
end

end


test2 = Aviator.define_request :test2 do

def self.provider
:openstack
end

def self.service
:identity
end

def self.api_version
:v3
end

def self.endpoint_type
:admin
end

end

binding.pry
2 changes: 1 addition & 1 deletion test/aviator/core/service_test.rb
Expand Up @@ -119,7 +119,7 @@ def service(default_session_data=nil)
it 'accepts an endpoint type option for selecting a specific request' do
default_session_data = do_auth_request.body
s = service(default_session_data)

response1 = s.request :list_tenants, endpoint_type: 'admin'
response2 = s.request :list_tenants, endpoint_type: 'public'

Expand Down
14 changes: 6 additions & 8 deletions test/aviator/openstack/compute/v2/public/list_images_test.rb
Expand Up @@ -32,9 +32,7 @@ def helper


def klass
path = helper.request_path('compute', 'v2', 'public', 'list_images.rb')
klass, request_name = Aviator::Service::RequestBuilder.build(path)
klass
@klass ||= helper.load_request('openstack', 'compute', 'v2', 'public', 'list_images.rb')
end


Expand Down Expand Up @@ -105,13 +103,13 @@ def klass
[ :status, 'ACTIVE' ],
[ :type, 'application/vnd.openstack.image' ]
]

url += "/detail" if params.first[1]

filters = []

params[1, params.length-1].each { |pair| filters << "#{ pair[0] }=#{ pair[1] }" }

url += "?#{ filters.join('&') }" unless filters.empty?

request = klass.new(session_data) do |p|
Expand Down Expand Up @@ -154,8 +152,8 @@ def klass
response.body[:images].length.must_equal 0
response.headers.wont_be_nil
end


validate_response 'parameters are valid' do
service = Aviator::Service.new(
provider: 'openstack',
Expand Down
Expand Up @@ -36,9 +36,7 @@ def helper


def klass
path = helper.request_path('identity', 'v2', 'admin', 'create_tenant.rb')
klass, request_name = Aviator::Service::RequestBuilder.build(path)
klass
@klass ||= helper.load_request('openstack', 'identity', 'v2', 'admin', 'create_tenant.rb')
end


Expand Down

0 comments on commit ede18a1

Please sign in to comment.