mirror of
https://github.com/fog/fog.git
synced 2022-11-09 13:51:43 -05:00
Improved Hash-arg keys validation (see: https://github.com/geemus/fog/issues#issue/41)
This commit is contained in:
parent
2a71ed0a4c
commit
ec15def557
3 changed files with 38 additions and 15 deletions
|
@ -67,7 +67,7 @@ module Fog
|
|||
{ :duration => duration }
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
end
|
||||
|
||||
require 'fog/aws'
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue