From 827c029a9957d37ec1e127c3cc946ddfe35f9007 Mon Sep 17 00:00:00 2001 From: Juris Galang Date: Sun, 21 Nov 2010 02:56:41 -0800 Subject: [PATCH] This is a combination of 23 commits (included merges from upstream); this commit(s) include changes to enforces recognizes/requires parameters for all supported services. Comments from the included commits follow: - Added google_storage_* keys - Fixed indentations. - Factored out requires and recognizes method implementation (now relies on the requires and recognizes clause from the NamedParameters module) - Added dependency to named-parameters gem. - Added recognizes declaration to classes for all supported services to enforce parameter name checks - - passing an unrecognized key when instantiating a service object will now cause an ArgumentError to be raised. - Added NOTE - comment added - check/filter-out keys from credentials that are not required by the class being instantiated - [local|storage] properly write out file contents - Added google_storage_* keys - Fixed indentations. - added put_object_acl request (ref: https://github.com/geemus/fog/issues#issue/74) - Release 0.3.24 - remove tracker reference from README - issues is now the goto for bugs/todo - notify and gracefully skip credential-less testsa - [rackspace|storage] fixes for directory/files - [local|storage] CGI.escape file names - Release 0.3.25 - updated deps; recognized_parameters -> declared_parameters; restored options filtering if Fog.bin - Added requires/recognizes to Fog::Terremark::Ecloud - Updted to use latest named-parameters gem. - Filter out unwanted parameters when Fog.bin - Updated to latest named-parameters gem - commented out unnecessary code - fix missing "volume" parameter error when setting Fog::AWS::Volume#server to nil (in order to detach it) - documentation update for key_pairs and helper - [aws|compute] commented/documented flavors/volumes - Fixes for issue 38 and 39 Closes #96 --- Gemfile.lock | 3 +++ fog.gemspec | 1 + lib/fog.rb | 1 + lib/fog/aws/cdn.rb | 1 + lib/fog/aws/compute.rb | 2 +- lib/fog/aws/elb.rb | 1 + lib/fog/aws/iam.rb | 1 + lib/fog/aws/simpledb.rb | 3 ++- lib/fog/aws/storage.rb | 4 +++- lib/fog/bluebox/compute.rb | 1 + lib/fog/brightbox/compute.rb | 1 + lib/fog/core/attributes.rb | 1 + lib/fog/core/credentials.rb | 3 +-- lib/fog/core/service.rb | 38 +++++----------------------------- lib/fog/go_grid/compute.rb | 4 ++-- lib/fog/google/storage.rb | 2 ++ lib/fog/linode/compute.rb | 1 + lib/fog/new_servers/compute.rb | 4 ++-- lib/fog/rackspace.rb | 8 ++++++- lib/fog/rackspace/cdn.rb | 3 +++ lib/fog/rackspace/compute.rb | 3 +++ lib/fog/rackspace/storage.rb | 3 +++ lib/fog/slicehost/compute.rb | 1 + lib/fog/terremark/ecloud.rb | 4 +++- lib/fog/vcloud.rb | 3 ++- 25 files changed, 52 insertions(+), 45 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index d5c636ec2..d0e232446 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -7,6 +7,7 @@ PATH formatador (>= 0.0.16) json mime-types + named-parameters (>= 0.0.14) net-ssh (>= 2.0.23) nokogiri (>= 1.4.4) ruby-hmac @@ -21,6 +22,7 @@ GEM formatador (>= 0.0.12) json (1.4.6) mime-types (1.16) + named-parameters (0.0.14) net-ssh (2.0.23) nokogiri (1.4.4) rake (0.8.7) @@ -40,6 +42,7 @@ DEPENDENCIES formatador (>= 0.0.16) json mime-types + named-parameters (>= 0.0.14) net-ssh (>= 2.0.23) nokogiri (>= 1.4.4) rake diff --git a/fog.gemspec b/fog.gemspec index 391a02778..ac9f7b6bc 100644 --- a/fog.gemspec +++ b/fog.gemspec @@ -47,6 +47,7 @@ Gem::Specification.new do |s| s.add_dependency('formatador', '>=0.0.16') s.add_dependency('json') s.add_dependency('mime-types') + s.add_dependency('named-parameters', '>=0.0.14') s.add_dependency('net-ssh', '>=2.0.23') s.add_dependency('nokogiri', '>=1.4.4') s.add_dependency('ruby-hmac') diff --git a/lib/fog.rb b/lib/fog.rb index ac80190b3..65742e693 100644 --- a/lib/fog.rb +++ b/lib/fog.rb @@ -3,6 +3,7 @@ require 'base64' require 'cgi' require 'excon' require 'formatador' +require 'named-parameters' require 'time' __DIR__ = File.dirname(__FILE__) diff --git a/lib/fog/aws/cdn.rb b/lib/fog/aws/cdn.rb index a9898b200..f07ce3308 100644 --- a/lib/fog/aws/cdn.rb +++ b/lib/fog/aws/cdn.rb @@ -3,6 +3,7 @@ module Fog class CDN < Fog::Service requires :aws_access_key_id, :aws_secret_access_key + recognizes :host, :path, :port, :scheme, :version, :persistent model_path 'fog/aws/models/cdn' diff --git a/lib/fog/aws/compute.rb b/lib/fog/aws/compute.rb index 2bdebde93..b0610361c 100644 --- a/lib/fog/aws/compute.rb +++ b/lib/fog/aws/compute.rb @@ -2,7 +2,7 @@ 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' diff --git a/lib/fog/aws/elb.rb b/lib/fog/aws/elb.rb index 13f5b646b..e28b8f17f 100644 --- a/lib/fog/aws/elb.rb +++ b/lib/fog/aws/elb.rb @@ -3,6 +3,7 @@ module Fog class ELB < Fog::Service requires :aws_access_key_id, :aws_secret_access_key + recognizes :region, :host, :path, :port, :scheme, :persistent request_path 'fog/aws/requests/elb' request :create_load_balancer diff --git a/lib/fog/aws/iam.rb b/lib/fog/aws/iam.rb index 62fbbbe33..5ae5d43c3 100644 --- a/lib/fog/aws/iam.rb +++ b/lib/fog/aws/iam.rb @@ -3,6 +3,7 @@ module Fog class IAM < Fog::Service requires :aws_access_key_id, :aws_secret_access_key + recognizes :host, :path, :port, :scheme, :persistent request_path 'fog/aws/requests/iam' request :add_user_to_group diff --git a/lib/fog/aws/simpledb.rb b/lib/fog/aws/simpledb.rb index bd975defc..13018d8e6 100644 --- a/lib/fog/aws/simpledb.rb +++ b/lib/fog/aws/simpledb.rb @@ -3,7 +3,8 @@ module Fog class SimpleDB < Fog::Service requires :aws_access_key_id, :aws_secret_access_key - + recognizes :host, :nil_string, :path, :port, :scheme, :persistent + request_path 'fog/aws/requests/simpledb' request :batch_put_attributes request :create_domain diff --git a/lib/fog/aws/storage.rb b/lib/fog/aws/storage.rb index 7db767d24..ae5363613 100644 --- a/lib/fog/aws/storage.rb +++ b/lib/fog/aws/storage.rb @@ -3,7 +3,8 @@ module Fog class Storage < Fog::Service requires :aws_access_key_id, :aws_secret_access_key - + recognizes :endpoint, :region, :host, :path, :port, :scheme, :persistent + model_path 'fog/aws/models/storage' collection :directories model :directory @@ -175,6 +176,7 @@ module Fog end end + class Real include Utils extend Fog::Deprecation diff --git a/lib/fog/bluebox/compute.rb b/lib/fog/bluebox/compute.rb index 472bb2bd5..d543e9fc3 100644 --- a/lib/fog/bluebox/compute.rb +++ b/lib/fog/bluebox/compute.rb @@ -3,6 +3,7 @@ module Fog class Compute < Fog::Service requires :bluebox_api_key, :bluebox_customer_id + recognizes :bluebox_host, :bluebox_port, :bluebox_scheme, :persistent model_path 'fog/bluebox/models/compute' model :flavor diff --git a/lib/fog/brightbox/compute.rb b/lib/fog/brightbox/compute.rb index 5a3048429..c83046a1c 100644 --- a/lib/fog/brightbox/compute.rb +++ b/lib/fog/brightbox/compute.rb @@ -5,6 +5,7 @@ module Fog API_URL = "https://api.gb1.brightbox.com/" requires :brightbox_client_id, :brightbox_secret + recognizes :brightbox_auth_url, :brightbox_api_url model_path 'fog/brightbox/models/compute' model :account # Singular resource, no collection diff --git a/lib/fog/core/attributes.rb b/lib/fog/core/attributes.rb index aba404bae..eaa145a01 100644 --- a/lib/fog/core/attributes.rb +++ b/lib/fog/core/attributes.rb @@ -148,6 +148,7 @@ module Fog !identity end + # check that the attributes specified in args exist and is not nil def requires(*args) missing = [] for arg in [:connection] | args diff --git a/lib/fog/core/credentials.rb b/lib/fog/core/credentials.rb index 5bc663dc6..cefb63cad 100644 --- a/lib/fog/core/credentials.rb +++ b/lib/fog/core/credentials.rb @@ -28,7 +28,6 @@ module Fog unless credentials && credentials[credential] print("\n To run as '#{credential}', add the following to #{config_path}\n") yml = <<-YML - :#{credential}: :aws_access_key_id: INTENTIONALLY_LEFT_BLANK :aws_secret_access_key: INTENTIONALLY_LEFT_BLANK @@ -51,7 +50,7 @@ module Fog :terremark_username: INTENTIONALLY_LEFT_BLANK :terremark_password: INTENTIONALLY_LEFT_BLANK YML - print(yml) + print("\n#{yml}\n") raise(ArgumentError.new("Missing Credentials")) end credentials[credential] diff --git a/lib/fog/core/service.rb b/lib/fog/core/service.rb index 2a90aa020..de0f06ea7 100644 --- a/lib/fog/core/service.rb +++ b/lib/fog/core/service.rb @@ -34,13 +34,16 @@ module Fog EOS end + def requirements + declared_parameters_for :new, :required + end + def new(options={}) if Fog.bin - default_credentials = Fog.credentials.reject {|key, value| !requirements.include?(key)} + default_credentials = filter_parameters(Fog.credentials) options = default_credentials.merge(options) end - validate_arguments(options) setup_requirements if Fog.mocking? @@ -110,41 +113,10 @@ module Fog @requests ||= [] end - def requires(*args) - requirements.concat(args) - end - - def requirements - @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(options) - missing = requirements - options.keys - unless missing.empty? - raise ArgumentError, "Missing required arguments: #{missing.join(', ')}" - end - - # FIXME: avoid failing for the services that don't have recognizes yet - unless recognizes.empty? - unrecognized = options.keys - requirements - recognized - unless unrecognized.empty? - raise ArgumentError, "Unrecognized arguments: #{unrecognized.join(', ')}" - end - end - end - end end diff --git a/lib/fog/go_grid/compute.rb b/lib/fog/go_grid/compute.rb index c50126845..f71b85b23 100644 --- a/lib/fog/go_grid/compute.rb +++ b/lib/fog/go_grid/compute.rb @@ -2,8 +2,8 @@ module Fog module GoGrid class Compute < Fog::Service - requires :go_grid_api_key - requires :go_grid_shared_secret + requires :go_grid_api_key, :go_grid_shared_secret + recognizes :host, :path, :port, :scheme, :persistent model_path 'fog/go_grid/models/compute' model :image diff --git a/lib/fog/google/storage.rb b/lib/fog/google/storage.rb index cb29e3702..bf69a78b3 100644 --- a/lib/fog/google/storage.rb +++ b/lib/fog/google/storage.rb @@ -3,6 +3,7 @@ module Fog class Storage < Fog::Service requires :google_storage_access_key_id, :google_storage_secret_access_key + recognizes :host, :port, :scheme, :persistent model_path 'fog/google/models/storage' collection :directories @@ -135,6 +136,7 @@ module Fog end end + class Real include Utils extend Fog::Deprecation diff --git a/lib/fog/linode/compute.rb b/lib/fog/linode/compute.rb index 66eb9623c..f79d89745 100644 --- a/lib/fog/linode/compute.rb +++ b/lib/fog/linode/compute.rb @@ -3,6 +3,7 @@ module Fog class Compute < Fog::Service requires :linode_api_key + recognizes :port, :scheme, :persistent model_path 'fog/linode/models/compute' diff --git a/lib/fog/new_servers/compute.rb b/lib/fog/new_servers/compute.rb index 3615297b2..70aafba6a 100644 --- a/lib/fog/new_servers/compute.rb +++ b/lib/fog/new_servers/compute.rb @@ -4,8 +4,8 @@ module Fog module NewServers class Compute < Fog::Service - requires :new_servers_password - requires :new_servers_username + requires :new_servers_password, :new_servers_username + recognizes :host, :port, :scheme, :persistent model_path 'fog/new_servers/models/compute' diff --git a/lib/fog/rackspace.rb b/lib/fog/rackspace.rb index 55cb752b0..571f27d35 100644 --- a/lib/fog/rackspace.rb +++ b/lib/fog/rackspace.rb @@ -1,6 +1,7 @@ module Fog module Rackspace - + + include NamedParameters extend Fog::Provider service_path 'fog/rackspace' @@ -10,6 +11,11 @@ module Fog service 'servers' service 'storage' + # NOTE: might be better to rely on the caller alone to enforce parameter + # requirements... + has_named_parameters :'self.authenticate', + :required => [ :rackspace_api_key, :rackspace_username ], + :optional => [ :rackspace_auth_url ] def self.authenticate(options) rackspace_auth_url = options[:rackspace_auth_url] || "auth.api.rackspacecloud.com" connection = Fog::Connection.new("https://" + rackspace_auth_url) diff --git a/lib/fog/rackspace/cdn.rb b/lib/fog/rackspace/cdn.rb index 4a4b127c6..6bc1a12fb 100644 --- a/lib/fog/rackspace/cdn.rb +++ b/lib/fog/rackspace/cdn.rb @@ -3,6 +3,9 @@ module Fog class CDN < Fog::Service requires :rackspace_api_key, :rackspace_username + # NOTE: recognizes clause delegates to Fog::Rackspace.authenticate's so + # we also declare those parameters that the authenticate expects... + recognizes :rackspace_auth_url, :persistent model_path 'fog/rackspace/models/cdn' diff --git a/lib/fog/rackspace/compute.rb b/lib/fog/rackspace/compute.rb index b107b740b..43c47f3c8 100644 --- a/lib/fog/rackspace/compute.rb +++ b/lib/fog/rackspace/compute.rb @@ -3,6 +3,9 @@ module Fog class Compute < Fog::Service requires :rackspace_api_key, :rackspace_username + # NOTE: recognizes clause delegates to Fog::Rackspace.authenticate's so + # we also declare those parameters that the authenticate expects... + recognizes :rackspace_auth_url, :persistent model_path 'fog/rackspace/models/compute' model :flavor diff --git a/lib/fog/rackspace/storage.rb b/lib/fog/rackspace/storage.rb index 1dbe723df..843bdc17c 100644 --- a/lib/fog/rackspace/storage.rb +++ b/lib/fog/rackspace/storage.rb @@ -3,6 +3,9 @@ module Fog class Storage < Fog::Service requires :rackspace_api_key, :rackspace_username + # NOTE: recognizes clause delegates to Fog::Rackspace.authenticate's so + # we also declare those parameters that the authenticate expects... + recognizes :rackspace_auth_url, :persistent model_path 'fog/rackspace/models/storage' model :directory diff --git a/lib/fog/slicehost/compute.rb b/lib/fog/slicehost/compute.rb index 5909c2420..cb4a5df85 100644 --- a/lib/fog/slicehost/compute.rb +++ b/lib/fog/slicehost/compute.rb @@ -3,6 +3,7 @@ module Fog class Compute < Fog::Service requires :slicehost_password + recognizes :host, :port, :scheme, :persistent model_path 'fog/slicehost/models/compute' model :flavor diff --git a/lib/fog/terremark/ecloud.rb b/lib/fog/terremark/ecloud.rb index 1cd4c8334..4f24a819d 100644 --- a/lib/fog/terremark/ecloud.rb +++ b/lib/fog/terremark/ecloud.rb @@ -32,7 +32,9 @@ module Fog end class Real - + requires :terremark_ecloud_password, :terremark_ecloud_username + recognizes :host, :path, :port, :scheme, :persistent + include Fog::Terremark::Shared::Real include Fog::Terremark::Shared::Parser diff --git a/lib/fog/vcloud.rb b/lib/fog/vcloud.rb index 2e9a920b4..17b747a51 100644 --- a/lib/fog/vcloud.rb +++ b/lib/fog/vcloud.rb @@ -19,7 +19,8 @@ end module Fog class Vcloud < Fog::Service - requires :username, :password, :versions_uri + requires :username, :password, :module, :versions_uri + recognizes :version, :persistent model_path 'fog/vcloud/models' model :vdc