2010-06-12 14:13:49 -04:00
|
|
|
module Fog
|
2010-09-03 04:11:45 -04:00
|
|
|
class Service
|
2010-09-02 16:39:01 -04:00
|
|
|
|
2010-09-03 04:11:45 -04:00
|
|
|
class Error < Fog::Errors::Error; end
|
|
|
|
class NotFound < Fog::Errors::NotFound; end
|
2010-06-12 16:21:32 -04:00
|
|
|
|
2010-09-03 04:11:45 -04:00
|
|
|
module Collections
|
|
|
|
|
|
|
|
def collections
|
|
|
|
service.collections
|
|
|
|
end
|
|
|
|
|
|
|
|
def requests
|
|
|
|
service.requests
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
class << self
|
|
|
|
|
2010-12-06 22:58:15 -05:00
|
|
|
# this is to accomodate Real implementations of Service subclasses
|
|
|
|
# NOTE: it might be good to enforce parameter specs to Mock classes as well.
|
|
|
|
def inject_parameter_specs
|
|
|
|
lambda do |spec|
|
|
|
|
implementation = "Real"
|
|
|
|
self.const_set(implementation, Class.new) unless self.const_defined? implementation
|
|
|
|
realclass = self.const_get implementation
|
|
|
|
|
|
|
|
if realclass.declared_parameters_for(:'self.new', :required).empty?
|
|
|
|
required = declared_parameters_for(:'self.new', :required)
|
|
|
|
realclass.send(:requires, *required)
|
|
|
|
end
|
|
|
|
|
|
|
|
if realclass.declared_parameters_for(:'self.new', :optional).empty?
|
|
|
|
optional = declared_parameters_for(:'self.new', :optional)
|
|
|
|
realclass.send(:recognizes, *optional)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-09-03 04:11:45 -04:00
|
|
|
def inherited(child)
|
|
|
|
child.class_eval <<-EOS, __FILE__, __LINE__
|
|
|
|
module Collections
|
|
|
|
include Fog::Service::Collections
|
|
|
|
|
|
|
|
def service
|
|
|
|
#{child}
|
2010-06-12 14:13:49 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-09-03 04:11:45 -04:00
|
|
|
def self.service
|
|
|
|
#{child}
|
2010-06-12 14:13:49 -04:00
|
|
|
end
|
2010-09-03 04:11:45 -04:00
|
|
|
EOS
|
|
|
|
end
|
2010-06-17 19:58:09 -04:00
|
|
|
|
2010-11-21 05:56:41 -05:00
|
|
|
def requirements
|
2010-12-06 22:58:15 -05:00
|
|
|
declared_parameters_for :'self.new', :required
|
2010-11-21 05:56:41 -05:00
|
|
|
end
|
|
|
|
|
2010-09-03 04:11:45 -04:00
|
|
|
def new(options={})
|
|
|
|
if Fog.bin
|
2010-11-21 05:56:41 -05:00
|
|
|
default_credentials = filter_parameters(Fog.credentials)
|
2010-09-03 04:11:45 -04:00
|
|
|
options = default_credentials.merge(options)
|
2010-06-12 14:13:49 -04:00
|
|
|
end
|
2010-09-02 19:01:19 -04:00
|
|
|
|
2010-09-14 13:40:02 -04:00
|
|
|
setup_requirements
|
|
|
|
|
|
|
|
if Fog.mocking?
|
|
|
|
service::Mock.send(:include, service::Collections)
|
|
|
|
service::Mock.new(options)
|
|
|
|
else
|
|
|
|
service::Real.send(:include, service::Collections)
|
|
|
|
service::Real.new(options)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def setup_requirements
|
|
|
|
if superclass.respond_to?(:setup_requirements)
|
|
|
|
superclass.setup_requirements
|
|
|
|
end
|
|
|
|
|
2010-10-12 19:22:12 -04:00
|
|
|
@required ||= false
|
2010-09-03 04:11:45 -04:00
|
|
|
unless @required
|
|
|
|
for collection in collections
|
|
|
|
require [@model_path, collection].join('/')
|
|
|
|
constant = collection.to_s.split('_').map {|characters| characters[0...1].upcase << characters[1..-1]}.join('')
|
|
|
|
service::Collections.module_eval <<-EOS, __FILE__, __LINE__
|
2010-09-14 13:40:02 -04:00
|
|
|
def #{collection}(attributes = {})
|
2010-09-03 04:11:45 -04:00
|
|
|
#{service}::#{constant}.new({:connection => self}.merge(attributes))
|
|
|
|
end
|
|
|
|
EOS
|
2010-09-02 19:01:19 -04:00
|
|
|
end
|
2010-09-03 04:11:45 -04:00
|
|
|
for model in models
|
|
|
|
require [@model_path, model].join('/')
|
|
|
|
end
|
|
|
|
for request in requests
|
|
|
|
require [@request_path, request].join('/')
|
2010-09-02 19:01:19 -04:00
|
|
|
end
|
2010-09-03 04:11:45 -04:00
|
|
|
@required = true
|
|
|
|
end
|
|
|
|
end
|
2010-09-02 19:01:19 -04:00
|
|
|
|
2010-09-03 04:11:45 -04:00
|
|
|
def model_path(new_path)
|
|
|
|
@model_path = new_path
|
|
|
|
end
|
2010-06-12 14:13:49 -04:00
|
|
|
|
2010-09-03 04:11:45 -04:00
|
|
|
def collection(new_collection)
|
|
|
|
collections << new_collection
|
|
|
|
end
|
2010-06-12 14:13:49 -04:00
|
|
|
|
2010-09-03 04:11:45 -04:00
|
|
|
def collections
|
|
|
|
@collections ||= []
|
|
|
|
end
|
2010-09-02 19:01:19 -04:00
|
|
|
|
2010-09-03 04:11:45 -04:00
|
|
|
def model(new_model)
|
|
|
|
models << new_model
|
|
|
|
end
|
2010-09-02 19:01:19 -04:00
|
|
|
|
2010-09-03 04:11:45 -04:00
|
|
|
def models
|
|
|
|
@models ||= []
|
|
|
|
end
|
2010-06-12 14:13:49 -04:00
|
|
|
|
2010-09-03 04:11:45 -04:00
|
|
|
def request_path(new_path)
|
|
|
|
@request_path = new_path
|
|
|
|
end
|
2010-06-12 14:13:49 -04:00
|
|
|
|
2010-09-03 04:11:45 -04:00
|
|
|
def request(new_request)
|
|
|
|
requests << new_request
|
|
|
|
end
|
2010-06-12 14:13:49 -04:00
|
|
|
|
2010-09-03 04:11:45 -04:00
|
|
|
def requests
|
|
|
|
@requests ||= []
|
|
|
|
end
|
2010-06-12 14:13:49 -04:00
|
|
|
|
2010-09-03 04:11:45 -04:00
|
|
|
def reset_data(keys=Mock.data.keys)
|
|
|
|
Mock.reset_data(keys)
|
|
|
|
end
|
2010-06-12 16:21:32 -04:00
|
|
|
|
2010-06-12 14:13:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|