From a3af5d78004165c852130620f78433928e5a3152 Mon Sep 17 00:00:00 2001 From: geemus Date: Thu, 2 Sep 2010 16:01:19 -0700 Subject: [PATCH] working toward more consistent/simpler collection/bin management --- lib/fog/aws/bin.rb | 45 +++------- lib/fog/aws/ec2.rb | 106 ++++++++++++------------ lib/fog/aws/elb.rb | 16 ++-- lib/fog/aws/models/s3/files.rb | 11 +++ lib/fog/aws/s3.rb | 54 ++++++------ lib/fog/aws/simpledb.rb | 18 ++-- lib/fog/bluebox.rb | 29 +++---- lib/fog/bluebox/bin.rb | 16 ++-- lib/fog/go_grid.rb | 18 ++-- lib/fog/linode.rb | 18 ++-- lib/fog/local.rb | 8 +- lib/fog/local/bin.rb | 8 +- lib/fog/local/models/files.rb | 19 +++-- lib/fog/new_servers.rb | 14 ++-- lib/fog/rackspace/bin.rb | 24 +++--- lib/fog/rackspace/files.rb | 28 +++---- lib/fog/rackspace/models/files/files.rb | 22 ++--- lib/fog/rackspace/servers.rb | 48 +++++------ lib/fog/service.rb | 30 ++++++- lib/fog/slicehost.rb | 32 +++---- lib/fog/slicehost/bin.rb | 16 ++-- lib/fog/vcloud.rb | 4 +- 22 files changed, 302 insertions(+), 282 deletions(-) diff --git a/lib/fog/aws/bin.rb b/lib/fog/aws/bin.rb index 6e5c29e4c..2f1618bf7 100644 --- a/lib/fog/aws/bin.rb +++ b/lib/fog/aws/bin.rb @@ -23,40 +23,20 @@ module AWS @@connections[service] end - def addresses - self[:ec2].addresses + for collection in Fog::AWS::EC2.collections + module_eval <<-EOS, __FILE__, __LINE__ + def #{collection} + self[:ec2].#{collection} + end + EOS end - def directories - self[:s3].directories - end - - def flavors - self[:ec2].flavors - end - - def images - self[:ec2].images - end - - def servers - self[:ec2].servers - end - - def key_pairs - self[:ec2].key_pairs - end - - def security_groups - self[:ec2].security_groups - end - - def snapshots - self[:ec2].snapshots - end - - def volumes - self[:ec2].volumes + for collection in Fog::AWS::S3.collections + module_eval <<-EOS, __FILE__, __LINE__ + def #{collection} + self[:s3].#{collection} + end + EOS end else @@ -67,4 +47,5 @@ module AWS end end + end diff --git a/lib/fog/aws/ec2.rb b/lib/fog/aws/ec2.rb index 6f6699f5b..7bbe28a2f 100644 --- a/lib/fog/aws/ec2.rb +++ b/lib/fog/aws/ec2.rb @@ -6,63 +6,63 @@ module Fog requires :aws_access_key_id, :aws_secret_access_key model_path 'fog/aws/models/ec2' - model 'address' - model 'addresses' - model 'flavor' - model 'flavors' - model 'image' - model 'images' - model 'key_pair' - model 'key_pairs' - model 'security_group' - model 'security_groups' - model 'server' - model 'servers' - model 'snapshot' - model 'snapshots' - model 'volume' - model 'volumes' + model :address + collection :addresses + model :flavor + collection :flavors + model :image + collection :images + model :key_pair + collection :key_pairs + model :security_group + collection :security_groups + model :server + collection :servers + model :snapshot + collection :snapshots + model :volume + collection :volumes require 'fog/aws/parsers/ec2/basic' request_path 'fog/aws/requests/ec2' - request 'allocate_address' - request 'associate_address' - request 'attach_volume' - request 'authorize_security_group_ingress' - request 'create_key_pair' - request 'create_security_group' - request 'create_snapshot' - request 'create_image' - request 'create_volume' - request 'delete_key_pair' - request 'delete_security_group' - request 'delete_snapshot' - request 'delete_volume' - request 'deregister_image' - request 'describe_addresses' - request 'describe_availability_zones' - request 'describe_images' - request 'describe_instances' - request 'describe_reserved_instances' - request 'describe_key_pairs' - request 'describe_regions' - request 'describe_security_groups' - request 'describe_snapshots' - request 'describe_volumes' - request 'detach_volume' - request 'disassociate_address' - request 'get_console_output' - request 'modify_image_attributes' - request 'modify_snapshot_attribute' - request 'reboot_instances' - request 'release_address' - request 'register_image' - request 'revoke_security_group_ingress' - request 'run_instances' - request 'terminate_instances' - request 'start_instances' - request 'stop_instances' + request :allocate_address + request :associate_address + request :attach_volume + request :authorize_security_group_ingress + request :create_key_pair + request :create_security_group + request :create_snapshot + request :create_image + request :create_volume + request :delete_key_pair + request :delete_security_group + request :delete_snapshot + request :delete_volume + request :deregister_image + request :describe_addresses + request :describe_availability_zones + request :describe_images + request :describe_instances + request :describe_reserved_instances + request :describe_key_pairs + request :describe_regions + request :describe_security_groups + request :describe_snapshots + request :describe_volumes + request :detach_volume + request :disassociate_address + request :get_console_output + request :modify_image_attributes + request :modify_snapshot_attribute + request :reboot_instances + request :release_address + request :register_image + request :revoke_security_group_ingress + request :run_instances + request :terminate_instances + request :start_instances + request :stop_instances class Mock include Collections diff --git a/lib/fog/aws/elb.rb b/lib/fog/aws/elb.rb index 2dc0d497b..2316cb8a2 100644 --- a/lib/fog/aws/elb.rb +++ b/lib/fog/aws/elb.rb @@ -6,14 +6,14 @@ module Fog requires :aws_access_key_id, :aws_secret_access_key request_path 'fog/aws/requests/elb' - request 'create_load_balancer' - request 'delete_load_balancer' - request 'deregister_instances_from_load_balancer' - request 'describe_instance_health' - request 'describe_load_balancers' - request 'disable_availability_zones_for_load_balancer' - request 'enable_availability_zones_for_load_balancer' - request 'register_instances_with_load_balancer' + request :create_load_balancer + request :delete_load_balancer + request :deregister_instances_from_load_balancer + request :describe_instance_health + request :describe_load_balancers + request :disable_availability_zones_for_load_balancer + request :enable_availability_zones_for_load_balancer + request :register_instances_with_load_balancer class Mock include Collections diff --git a/lib/fog/aws/models/s3/files.rb b/lib/fog/aws/models/s3/files.rb index 69553a374..b631da7b1 100644 --- a/lib/fog/aws/models/s3/files.rb +++ b/lib/fog/aws/models/s3/files.rb @@ -5,6 +5,12 @@ module Fog module AWS module S3 + module Collections + def files + Fog::AWS::S3::Files.new(:connection => self) + end + end + class Files < Fog::Collection attribute :delimiter, 'Delimiter' @@ -17,6 +23,7 @@ module Fog model Fog::AWS::S3::File def all(options = {}) + requires :directory options = { 'delimiter' => @delimiter, 'marker' => @marker, @@ -38,6 +45,7 @@ module Fog end def get(key, options = {}, &block) + requires :directory data = connection.get_object(directory.key, key, options, &block) file_data = { :body => data.body, @@ -54,10 +62,12 @@ module Fog end def get_url(key, expires) + requires :directory connection.get_object_url(directory.key, key, expires) end def head(key, options = {}) + requires :directory data = connection.head_object(directory.key, key, options) file_data = { :key => key @@ -73,6 +83,7 @@ module Fog end def new(attributes = {}) + requires :directory super({ :directory => directory }.merge!(attributes)) end diff --git a/lib/fog/aws/s3.rb b/lib/fog/aws/s3.rb index c3bc0b6fc..4ece65272 100644 --- a/lib/fog/aws/s3.rb +++ b/lib/fog/aws/s3.rb @@ -6,35 +6,35 @@ module Fog requires :aws_access_key_id, :aws_secret_access_key model_path 'fog/aws/models/s3' - model 'directories' - model 'directory' - model 'files' - model 'file' + collection :directories + model :directory + collection :files + model :file request_path 'fog/aws/requests/s3' - request 'copy_object' - request 'delete_bucket' - request 'delete_object' - request 'get_bucket' - request 'get_bucket_acl' - request 'get_bucket_location' - request 'get_bucket_logging' - request 'get_bucket_object_versions' - request 'get_bucket_versioning' - request 'get_object' - request 'get_object_acl' - request 'get_object_torrent' - request 'get_object_url' - request 'get_request_payment' - request 'get_service' - request 'head_object' - request 'put_bucket' - request 'put_bucket_acl' - request 'put_bucket_logging' - request 'put_bucket_versioning' - request 'put_object' - request 'put_object_url' - request 'put_request_payment' + request :copy_object + request :delete_bucket + request :delete_object + request :get_bucket + request :get_bucket_acl + request :get_bucket_location + request :get_bucket_logging + request :get_bucket_object_versions + request :get_bucket_versioning + request :get_object + request :get_object_acl + request :get_object_torrent + request :get_object_url + request :get_request_payment + request :get_service + request :head_object + request :put_bucket + request :put_bucket_acl + request :put_bucket_logging + request :put_bucket_versioning + request :put_object + request :put_object_url + request :put_request_payment module Utils diff --git a/lib/fog/aws/simpledb.rb b/lib/fog/aws/simpledb.rb index 39ce0e8b1..b59f65076 100644 --- a/lib/fog/aws/simpledb.rb +++ b/lib/fog/aws/simpledb.rb @@ -6,15 +6,15 @@ module Fog requires :aws_access_key_id, :aws_secret_access_key request_path 'fog/aws/requests/simpledb' - request 'batch_put_attributes' - request 'create_domain' - request 'delete_attributes' - request 'delete_domain' - request 'domain_metadata' - request 'get_attributes' - request 'list_domains' - request 'put_attributes' - request 'select' + request :batch_put_attributes + request :create_domain + request :delete_attributes + request :delete_domain + request :domain_metadata + request :get_attributes + request :list_domains + request :put_attributes + request :select class Mock include Collections diff --git a/lib/fog/bluebox.rb b/lib/fog/bluebox.rb index b7585a617..c6018e9fe 100644 --- a/lib/fog/bluebox.rb +++ b/lib/fog/bluebox.rb @@ -5,22 +5,23 @@ module Fog requires :bluebox_api_key, :bluebox_customer_id model_path 'fog/bluebox/models' - model 'flavor' - model 'flavors' - model 'images' - model 'server' - model 'servers' + model :flavor + collection :flavors + model :image + collection :images + model :server + collection :servers request_path 'fog/bluebox/requests' - request 'create_block' - request 'destroy_block' - request 'get_block' - request 'get_blocks' - request 'get_product' - request 'get_products' - request 'get_template' - request 'get_templates' - request 'reboot_block' + request :create_block + request :destroy_block + request :get_block + request :get_blocks + request :get_product + request :get_products + request :get_template + request :get_templates + request :reboot_block class Mock include Collections diff --git a/lib/fog/bluebox/bin.rb b/lib/fog/bluebox/bin.rb index d40d29423..13ef0aad5 100644 --- a/lib/fog/bluebox/bin.rb +++ b/lib/fog/bluebox/bin.rb @@ -17,16 +17,12 @@ module Bluebox @@connections[service] end - def flavors - self[:blocks].flavors - end - - def images - self[:blocks].images - end - - def servers - self[:blocks].servers + for collection in Fog::Bluebox.collections + module_eval <<-EOS, __FILE__, __LINE__ + def #{collection} + self[:blocks].#{collection} + end + EOS end else diff --git a/lib/fog/go_grid.rb b/lib/fog/go_grid.rb index ac3ec8cd5..02499aa33 100644 --- a/lib/fog/go_grid.rb +++ b/lib/fog/go_grid.rb @@ -8,15 +8,15 @@ module Fog model_path 'fog/go_grid/models' request_path 'fog/go_grid/requests' - request 'common_lookup_list' - request 'grid_image_list' - request 'grid_ip_list' - request 'grid_loadbalancer_list' - request 'grid_server_add' - request 'grid_server_delete' - request 'grid_server_get' - request 'grid_server_list' - request 'grid_server_power' + request :common_lookup_list + request :grid_image_list + request :grid_ip_list + request :grid_loadbalancer_list + request :grid_server_add + request :grid_server_delete + request :grid_server_get + request :grid_server_list + request :grid_server_power class Mock include Collections diff --git a/lib/fog/linode.rb b/lib/fog/linode.rb index 3d8cbf972..8cc489b67 100644 --- a/lib/fog/linode.rb +++ b/lib/fog/linode.rb @@ -7,15 +7,15 @@ module Fog model_path 'fog/linode/models' request_path 'fog/linode/requests' - request 'avail_datacenters' - request 'avail_distributions' - request 'avail_kernels' - request 'avail_linodeplans' - request 'avail_stackscripts' - request 'linode_create' - request 'linode_delete' - request 'linode_list' - request 'linode_reboot' + request :avail_datacenters + request :avail_distributions + request :avail_kernels + request :avail_linodeplans + request :avail_stackscripts + request :linode_create + request :linode_delete + request :linode_list + request :linode_reboot class Mock include Collections diff --git a/lib/fog/local.rb b/lib/fog/local.rb index 7ab967c8a..3b725ef5e 100644 --- a/lib/fog/local.rb +++ b/lib/fog/local.rb @@ -5,10 +5,10 @@ module Fog requires :local_root model_path 'fog/local/models' - model 'directories' - model 'directory' - model 'file' - model 'files' + collection :directories + model :directory + model :file + collection :files class Mock include Collections diff --git a/lib/fog/local/bin.rb b/lib/fog/local/bin.rb index b06f61a7c..69099be81 100644 --- a/lib/fog/local/bin.rb +++ b/lib/fog/local/bin.rb @@ -16,8 +16,12 @@ module Local @@connections[service] end - def directories - self[:files].directories + for collection in Fog::Local.collections + module_eval <<-EOS, __FILE__, __LINE__ + def #{collection} + self[:files].#{collection} + end + EOS end else diff --git a/lib/fog/local/models/files.rb b/lib/fog/local/models/files.rb index cf6c52111..5135df83d 100644 --- a/lib/fog/local/models/files.rb +++ b/lib/fog/local/models/files.rb @@ -4,11 +4,20 @@ require 'fog/local/models/file' module Fog module Local + module Collections + def files + Fog::Local::Files.new(:connection => self) + end + end + class Files < Fog::Collection + attribute :directory + model Fog::Local::File def all + requires :directory if directory.collection.get(directory.key) data = Dir.entries(connection.path_to(directory.key)).select do |key| key[0...1] != '.' && !::File.directory?(connection.path_to(key)) @@ -26,11 +35,8 @@ module Fog end end - def directory - @directory - end - def get(key, &block) + requires :directory path = file_path(key) if ::File.exists?(path) data = { @@ -56,15 +62,12 @@ module Fog end def new(attributes = {}) + requires :directory super({ :directory => directory }.merge!(attributes)) end private - def directory=(new_directory) - @directory = new_directory - end - def file_path(key) connection.path_to(::File.join(directory.key, key)) end diff --git a/lib/fog/new_servers.rb b/lib/fog/new_servers.rb index f774ab5dc..deab193b0 100644 --- a/lib/fog/new_servers.rb +++ b/lib/fog/new_servers.rb @@ -10,13 +10,13 @@ module Fog model_path 'fog/new_servers/models' request_path 'fog/new_servers/requests' - request 'add_server' - request 'cancel_server' - request 'get_server' - request 'list_images' - request 'list_plans' - request 'list_servers' - request 'reboot_server' + request :add_server + request :cancel_server + request :get_server + request :list_images + request :list_plans + request :list_servers + request :reboot_server class Mock include Collections diff --git a/lib/fog/rackspace/bin.rb b/lib/fog/rackspace/bin.rb index e806aca57..9ab6aa573 100644 --- a/lib/fog/rackspace/bin.rb +++ b/lib/fog/rackspace/bin.rb @@ -18,22 +18,22 @@ module Rackspace @@connections[service] end - def directories - self[:files].directories + for collection in Fog::Rackspace::Files.collections + module_eval <<-EOS, __FILE__, __LINE__ + def #{collection} + self[:files].#{collection} + end + EOS end - def flavors - self[:servers].flavors + for collection in Fog::Rackspace::Servers.collections + module_eval <<-EOS, __FILE__, __LINE__ + def #{collection} + self[:servers].#{collection} + end + EOS end - def images - self[:servers].images - end - - def servers - self[:servers].servers - end - else def initialized? diff --git a/lib/fog/rackspace/files.rb b/lib/fog/rackspace/files.rb index 6a5b7c9f0..e028aa790 100644 --- a/lib/fog/rackspace/files.rb +++ b/lib/fog/rackspace/files.rb @@ -6,22 +6,22 @@ module Fog requires :rackspace_api_key, :rackspace_username model_path 'fog/rackspace/models/files' - model 'directory' - model 'directories' - model 'file' - model 'files' + model :directory + collection :directories + model :file + collection :files request_path 'fog/rackspace/requests/files' - request 'delete_container' - request 'delete_object' - request 'get_container' - request 'get_containers' - request 'get_object' - request 'head_container' - request 'head_containers' - request 'head_object' - request 'put_container' - request 'put_object' + request :delete_container + request :delete_object + request :get_container + request :get_containers + request :get_object + request :head_container + request :head_containers + request :head_object + request :put_container + request :put_object module Utils diff --git a/lib/fog/rackspace/models/files/files.rb b/lib/fog/rackspace/models/files/files.rb index 6b030b16f..1d56f4160 100644 --- a/lib/fog/rackspace/models/files/files.rb +++ b/lib/fog/rackspace/models/files/files.rb @@ -5,8 +5,15 @@ module Fog module Rackspace module Files + module Collections + def files + Fog::Rackspace::Files::Files.new(:connection => self) + end + end + class Files < Fog::Collection + attribute :directory attribute :limit attribute :marker attribute :path @@ -15,6 +22,7 @@ module Fog model Fog::Rackspace::Files::File def all(options = {}) + requires :directory merge_attributes(options) parent = directory.collection.get( directory.key, @@ -27,11 +35,8 @@ module Fog end end - def directory - @directory - end - def get(key, options = {}, &block) + requires :directory options = { 'limit' => @limit, 'marker' => @marker, @@ -54,10 +59,12 @@ module Fog end def get_url(key, expires) + requires :directory connection.get_object_url(directory.name, key, expires) end def head(key, options = {}) + requires :directory data = connection.head_object(directory.name, key, options) file_data = { :key => key } for key, value in data.headers @@ -71,15 +78,10 @@ module Fog end def new(attributes = {}) + requires :directory super({ :directory => directory }.merge!(attributes)) end - private - - def directory=(new_directory) - @directory = new_directory - end - end end diff --git a/lib/fog/rackspace/servers.rb b/lib/fog/rackspace/servers.rb index fc98084d6..718aaba68 100644 --- a/lib/fog/rackspace/servers.rb +++ b/lib/fog/rackspace/servers.rb @@ -6,32 +6,32 @@ module Fog requires :rackspace_api_key, :rackspace_username model_path 'fog/rackspace/models/servers' - model 'flavor' - model 'flavors' - model 'image' - model 'images' - model 'server' - model 'servers' + model :flavor + collection :flavors + model :image + collection :images + model :server + collection :servers request_path 'fog/rackspace/requests/servers' - request 'create_image' - request 'create_server' - request 'delete_image' - request 'delete_server' - request 'get_flavor_details' - request 'get_image_details' - request 'get_server_details' - request 'list_addresses' - request 'list_private_addresses' - request 'list_public_addresses' - request 'list_flavors' - request 'list_flavors_detail' - request 'list_images' - request 'list_images_detail' - request 'list_servers' - request 'list_servers_detail' - request 'reboot_server' - request 'update_server' + request :create_image + request :create_server + request :delete_image + request :delete_server + request :get_flavor_details + request :get_image_details + request :get_server_details + request :list_addresses + request :list_private_addresses + request :list_public_addresses + request :list_flavors + request :list_flavors_detail + request :list_images + request :list_images_detail + request :list_servers + request :list_servers_detail + request :reboot_server + request :update_server class Mock include Collections diff --git a/lib/fog/service.rb b/lib/fog/service.rb index 0dc28ee88..568195b93 100644 --- a/lib/fog/service.rb +++ b/lib/fog/service.rb @@ -7,8 +7,6 @@ module Fog class Error < Fog::Errors::Error; end class NotFound < Fog::Errors::NotFound; end - module Collections; end - def self.new(options={}) if Fog.bin default_credentials = Fog.credentials.reject {|key, value| !requirements.include?(key)} @@ -28,6 +26,9 @@ module Fog end unless @required + for collection in collections + require [@model_path, collection].join('/') + end for model in models require [@model_path, model].join('/') end @@ -48,6 +49,23 @@ module Fog end instance end + + module Collections + + def collections + service.collections + end + + def requests + service.requests + end + + def service + @service ||= eval(self.class.to_s.split('::')[0...-1].join('::')) + end + + end + EOS end @@ -55,6 +73,14 @@ module Fog @model_path = new_path end + def collection(new_collection) + collections << new_collection + end + + def collections + @collections ||= [] + end + def model(new_model) models << new_model end diff --git a/lib/fog/slicehost.rb b/lib/fog/slicehost.rb index 0afad97b3..71a4c32e9 100644 --- a/lib/fog/slicehost.rb +++ b/lib/fog/slicehost.rb @@ -5,24 +5,24 @@ module Fog requires :slicehost_password model_path 'fog/slicehost/models' - model 'flavor' - model 'flavors' - model 'image' - model 'images' - model 'server' - model 'servers' + model :flavor + collection :flavors + model :image + collection :images + model :server + collection :servers request_path 'fog/slicehost/requests' - request 'create_slice' - request 'delete_slice' - request 'get_backups' - request 'get_flavor' - request 'get_flavors' - request 'get_image' - request 'get_images' - request 'get_slice' - request 'get_slices' - request 'reboot_slice' + request :create_slice + request :delete_slice + request :get_backups + request :get_flavor + request :get_flavors + request :get_image + request :get_images + request :get_slice + request :get_slices + request :reboot_slice class Mock include Collections diff --git a/lib/fog/slicehost/bin.rb b/lib/fog/slicehost/bin.rb index 1fbddf80a..eb9c47182 100644 --- a/lib/fog/slicehost/bin.rb +++ b/lib/fog/slicehost/bin.rb @@ -16,16 +16,12 @@ module Slicehost @@connections[service] end - def flavors - self[:slices].flavors - end - - def images - self[:slices].images - end - - def servers - self[:slices].servers + for collection in Fog::Slicehost.collections + module_eval <<-EOS, __FILE__, __LINE__ + def #{collection} + self[:slices].#{collection} + end + EOS end else diff --git a/lib/fog/vcloud.rb b/lib/fog/vcloud.rb index daba40d32..cd8b7e4de 100644 --- a/lib/fog/vcloud.rb +++ b/lib/fog/vcloud.rb @@ -22,8 +22,8 @@ module Fog requires :username, :password, :versions_uri model_path 'fog/vcloud/models' - model 'vdc' - model 'vdcs' + model :vdc + model :vdcs request_path 'fog/vcloud/requests' request :login