diff --git a/lib/fog.rb b/lib/fog.rb index 1228d0c46..5154ac2b2 100644 --- a/lib/fog.rb +++ b/lib/fog.rb @@ -67,7 +67,7 @@ module Fog { :duration => duration } end end - + end require 'fog/aws' diff --git a/lib/fog/aws/compute.rb b/lib/fog/aws/compute.rb index 0a72a7327..d58ac3357 100644 --- a/lib/fog/aws/compute.rb +++ b/lib/fog/aws/compute.rb @@ -2,7 +2,8 @@ module Fog module AWS class Compute < Fog::Service - requires :aws_access_key_id, :aws_secret_access_key + requires :aws_access_key_id, :aws_secret_access_key + recognizes :endpoint, :region, :host, :path, :port, :scheme, :persistent model_path 'fog/aws/models/compute' model :address @@ -192,7 +193,7 @@ module Fog end private - + def request(params) idempotent = params.delete(:idempotent) parser = params.delete(:parser) diff --git a/lib/fog/core/service.rb b/lib/fog/core/service.rb index f2c284bf2..e0373deae 100644 --- a/lib/fog/core/service.rb +++ b/lib/fog/core/service.rb @@ -40,18 +40,10 @@ module Fog options = default_credentials.merge(options) end - missing = [] - for requirement in requirements - missing << requirement unless options[requirement] - end - unless missing.empty? - if missing.length == 1 - raise(ArgumentError, [missing.first, "is required for this service"].join(' ')) - else - raise(ArgumentError, [missing[0...-1].join(", "), 'and', missing[-1], 'are required for this service'].join(' ')) - end - end - + validate_arguments options, \ + :required => requirements, + :optional => recognized + setup_requirements if Fog.mocking? @@ -129,10 +121,40 @@ module Fog @requirements ||= [] end + def recognizes(*args) + recognized.concat(args) + end + + def recognized + @recognized ||= [] + end + def reset_data(keys=Mock.data.keys) Mock.reset_data(keys) end + def validate_arguments(opts, spec = {}) + spec[:required] ||= [] + spec[:optional] ||= [] + + sorter = lambda{ |x, y| x.to_s <=> y.to_s } + allowed = (spec[:optional] + spec[:required]).sort &sorter + keys = opts.keys.map{ |k| k.to_sym } + + keys.sort! &sorter + spec[:required].sort! &sorter + + unless spec[:required].empty? + k = spec[:required] & keys + raise ArgumentError, "Missing required arguments: #{(spec[:required] - k).join(', ')}" \ + unless k == spec[:required] + end + + k = keys - allowed + raise ArgumentError, "Unrecognized arguments: #{k.join(', ')}" \ + unless k.empty? + end + end end