From b9315bf7fe192593729d2ae0fb40db333fcd9c06 Mon Sep 17 00:00:00 2001 From: Juris Galang Date: Mon, 6 Dec 2010 19:49:58 -0800 Subject: [PATCH] Added self.class_for(key) method. This allows us to inspect the requirements without having to create an instance of the service. --- lib/fog/aws/bin.rb | 42 +++++++++++++++++++++++--------------- lib/fog/bluebox/bin.rb | 38 +++++++++++++++++++--------------- lib/fog/brightbox/bin.rb | 14 +++++++++---- lib/fog/core/bin.rb | 12 +++++++---- lib/fog/go_grid/bin.rb | 16 ++++++++++----- lib/fog/google/bin.rb | 15 +++++++++----- lib/fog/linode/bin.rb | 16 ++++++++++----- lib/fog/local/bin.rb | 16 ++++++++++----- lib/fog/new_servers/bin.rb | 16 ++++++++++----- lib/fog/rackspace/bin.rb | 26 +++++++++++++++-------- lib/fog/slicehost/bin.rb | 16 ++++++++++----- 11 files changed, 149 insertions(+), 78 deletions(-) diff --git a/lib/fog/aws/bin.rb b/lib/fog/aws/bin.rb index 0ce565fdf..994b2faf2 100644 --- a/lib/fog/aws/bin.rb +++ b/lib/fog/aws/bin.rb @@ -1,35 +1,45 @@ class AWS < Fog::Bin class << self + def class_for(key) + case key + when :cdn + Fog::AWS::CDN + when :compute, :ec2 + Fog::AWS::Compute + when :elb + Fog::AWS::ELB + when :iam + Fog::AWS::IAM + when :sdb + Fog::AWS::SimpleDB + when :eu_storage, :s3, :storage + Fog::AWS::Storage + else + raise ArgumentError, "Unsupported #{self} service: #{key}" + end + end + def [](service) @@connections ||= Hash.new do |hash, key| - hash[key] = case key - when :cdn - Fog::AWS::CDN.new - when :compute - Fog::AWS::Compute.new + klazz = class_for(key) + hash[key] = case key when :ec2 location = caller.first warning = "[yellow][WARN] AWS[:ec2] is deprecated, use AWS[:compute] instead[/]" warning << " [light_black](" << location << ")[/] " Formatador.display_line(warning) - Fog::AWS::Compute.new - when :elb - Fog::AWS::ELB.new + klazz.new when :eu_storage - Fog::AWS::Storage.new(:region => 'eu-west-1') - when :iam - Fog::AWS::IAM.new - when :sdb - Fog::AWS::SimpleDB.new + klazz.new(:region => 'eu-west-1') when :s3 location = caller.first warning = "[yellow][WARN] AWS[:s3] is deprecated, use AWS[:storage] instead[/]" warning << " [light_black](" << location << ")[/] " Formatador.display_line(warning) - Fog::AWS::Storage.new - when :storage - Fog::AWS::Storage.new + klazz.new + else + klazz.new end end @@connections[service] diff --git a/lib/fog/bluebox/bin.rb b/lib/fog/bluebox/bin.rb index e9e5ce8de..30e33dd3a 100644 --- a/lib/fog/bluebox/bin.rb +++ b/lib/fog/bluebox/bin.rb @@ -1,25 +1,31 @@ class Bluebox < Fog::Bin class << self - def [](service) - @@connections ||= Hash.new do |hash, key| - hash[key] = case key - when :blocks - location = caller.first - warning = "[yellow][WARN] Bluebox[:blocks] is deprecated, use Bluebox[:compute] instead[/]" - warning << " [light_black](" << location << ")[/] " - Formatador.display_line(warning) - Fog::Bluebox::Compute.new - when :compute - Fog::Bluebox::Compute.new - end - end - @@connections[service] + def class_for(key) + case key + when :blocks, :compute + Fog::Bluebox::Compute + else + raise ArgumentError, "Unsupported #{self} service: #{key}" end + end - def services - [:compute] + def [](service) + @@connections ||= Hash.new do |hash, key| + if key == :blocks + location = caller.first + warning = "[yellow][WARN] Bluebox[:blocks] is deprecated, use Bluebox[:compute] instead[/]" + warning << " [light_black](" << location << ")[/] " + Formatador.display_line(warning) + end + hash[key] = class_for(key).new end + @@connections[service] + end + + def services + [:compute] + end end end diff --git a/lib/fog/brightbox/bin.rb b/lib/fog/brightbox/bin.rb index 90dc99ba1..9f0cc14ff 100644 --- a/lib/fog/brightbox/bin.rb +++ b/lib/fog/brightbox/bin.rb @@ -1,12 +1,18 @@ class Brightbox < Fog::Bin class << self + def class_for(key) + case key + when :compute + Fog::Brightbox::Compute + else + raise ArgumentError, "Unrecognized service: #{key}" + end + end + def [](service) @@connections ||= Hash.new do |hash, key| - hash[key] = case key - when :compute - Fog::Brightbox::Compute.new - end + hash[key] = class_for(key).new end @@connections[service] end diff --git a/lib/fog/core/bin.rb b/lib/fog/core/bin.rb index efcec9862..ef7caa679 100644 --- a/lib/fog/core/bin.rb +++ b/lib/fog/core/bin.rb @@ -34,16 +34,20 @@ module Fog availability = true for service in services begin - service = eval(self[service].class.to_s.split('::')[0...-1].join('::')) - availability &&= service.requirements.all? {|requirement| Fog.credentials.include?(requirement)} - rescue + service = self.class_for(service) + availability &&= service.requirements.all? { |requirement| Fog.credentials.include?(requirement) } + rescue ArgumentError => e + warning = "[yellow][WARN] #{e.message}[/]" + Formatador.display_line(warning) + availability = false + rescue => e availability = false end end if availability for service in services - for collection in self[service].collections + for collection in self.class_for(service).collections unless self.respond_to?(collection) self.class_eval <<-EOS, __FILE__, __LINE__ def self.#{collection} diff --git a/lib/fog/go_grid/bin.rb b/lib/fog/go_grid/bin.rb index 7869ddabe..8d07481d4 100644 --- a/lib/fog/go_grid/bin.rb +++ b/lib/fog/go_grid/bin.rb @@ -1,18 +1,24 @@ class GoGrid < Fog::Bin class << self + def class_for(key) + case key + when :compute, :servers + Fog::GoGrid::Compute + else + raise ArgumentError, "Unsupported #{self} service: #{key}" + end + end + def [](service) @@connections ||= Hash.new do |hash, key| - hash[key] = case key - when :compute - Fog::GoGrid::Compute.new - when :servers + if key == :servers location = caller.first warning = "[yellow][WARN] GoGrid[:servers] is deprecated, use GoGrid[:compute] instead[/]" warning << " [light_black](" << location << ")[/] " Formatador.display_line(warning) - Fog::GoGrid::Compute.new end + hash[key] = class_for(key).new end @@connections[service] end diff --git a/lib/fog/google/bin.rb b/lib/fog/google/bin.rb index 40286fb3c..37c77dbfa 100644 --- a/lib/fog/google/bin.rb +++ b/lib/fog/google/bin.rb @@ -1,12 +1,18 @@ class Google < Fog::Bin class << self + def class_for(key) + case key + when :storage + Fog::Google::Storage + else + raise ArgumentError, "Unsupported #{self} service: #{key}" + end + end + def [](service) @@connections ||= Hash.new do |hash, key| - hash[key] = case key - when :storage - Fog::Google::Storage.new - end + hash[key] = class_for(key).new end @@connections[service] end @@ -16,5 +22,4 @@ class Google < Fog::Bin end end - end diff --git a/lib/fog/linode/bin.rb b/lib/fog/linode/bin.rb index cf0fbdab7..d5f1a73d4 100644 --- a/lib/fog/linode/bin.rb +++ b/lib/fog/linode/bin.rb @@ -1,18 +1,24 @@ class Linode < Fog::Bin class << self + def class_for(key) + case key + when :compute, :linode + Fog::Linode::Compute + else + raise ArgumentError, "Unsupported #{self} service: #{key}" + end + end + def [](service) @@connections ||= Hash.new do |hash, key| - hash[key] = case key - when :compute - Fog::Linode::Compute.new - when :linode + if key == :linode location = caller.first warning = "[yellow][WARN] Linode[:linode] is deprecated, use Linode[:compute] instead[/]" warning << " [light_black](" << location << ")[/] " Formatador.display_line(warning) - Fog::Linode::Compute.new end + hash[key] = class_for(key).new end @@connections[service] end diff --git a/lib/fog/local/bin.rb b/lib/fog/local/bin.rb index bd753de8c..3ee8b83a8 100644 --- a/lib/fog/local/bin.rb +++ b/lib/fog/local/bin.rb @@ -1,18 +1,24 @@ class Local < Fog::Bin class << self + def class_for(key) + case key + when :files, :storage + Fog::Local::Storage + else + raise ArgumentError, "Unsupported #{self} service: #{key}" + end + end + def [](service) @@connections ||= Hash.new do |hash, key| - hash[key] = case key - when :files + if key == :files location = caller.first warning = "[yellow][WARN] Local[:files] is deprecated, use Local[:storage] instead[/]" warning << " [light_black](" << location << ")[/] " Formatador.display_line(warning) - Fog::Local::Storage.new - when :storage - Fog::Local::Storage.new end + hash[key] = class_for(key).new end @@connections[service] end diff --git a/lib/fog/new_servers/bin.rb b/lib/fog/new_servers/bin.rb index 097a4920a..64898b774 100644 --- a/lib/fog/new_servers/bin.rb +++ b/lib/fog/new_servers/bin.rb @@ -1,18 +1,24 @@ class NewServers < Fog::Bin class << self + def class_for(key) + case key + when :compute, :new_servers + Fog::NewServers::Compute + else + raise ArgumentError, "Unsupported #{self} service: #{key}" + end + end + def [](service) @@connections ||= Hash.new do |hash, key| - hash[key] = case key - when :compute - Fog::NewServers::Compute.new - when :new_servers + if key == :new_servers location = caller.first warning = "[yellow][WARN] NewServers[:servers] is deprecated, use NewServers[:compute] instead[/]" warning << " [light_black](" << location << ")[/] " Formatador.display_line(warning) - Fog::NewServers::Compute.new end + hash[key] = class_for(key).new end @@connections[service] end diff --git a/lib/fog/rackspace/bin.rb b/lib/fog/rackspace/bin.rb index a460852ea..61460a081 100644 --- a/lib/fog/rackspace/bin.rb +++ b/lib/fog/rackspace/bin.rb @@ -1,27 +1,37 @@ class Rackspace < Fog::Bin class << self + def class_for(key) + case key + when :cdn + Fog::Rackspace::CDN + when :compute, :servers + Fog::Rackspace::Compute + when :files, :storage + Fog::Rackspace::Storage + else + raise ArgumentError, "Unrecognized service: #{key}" + end + end + def [](service) @@connections ||= Hash.new do |hash, key| + klazz = class_for(key) hash[key] = case key - when :cdn - Fog::Rackspace::CDN.new - when :compute - Fog::Rackspace::Compute.new when :files location = caller.first warning = "[yellow][WARN] Rackspace[:files] is deprecated, use Rackspace[:storage] instead[/]" warning << " [light_black](" << location << ")[/] " Formatador.display_line(warning) - Fog::Rackspace::Storage.new + klazz.new when :servers location = caller.first warning = "[yellow][WARN] Rackspace[:servers] is deprecated, use Rackspace[:compute] instead[/]" warning << " [light_black](" << location << ")[/] " Formatador.display_line(warning) - Fog::Rackspace::Compute.new - when :storage - Fog::Rackspace::Storage.new + klazz.new + else + klazz.new end end @@connections[service] diff --git a/lib/fog/slicehost/bin.rb b/lib/fog/slicehost/bin.rb index 71199132a..783e10b2d 100644 --- a/lib/fog/slicehost/bin.rb +++ b/lib/fog/slicehost/bin.rb @@ -1,18 +1,24 @@ class Slicehost < Fog::Bin class << self + def class_for(key) + case key + when :compute, :slices + Fog::Slicehost::Compute + else + raise ArgumentError, "Unrecognized service: #{key}" + end + end + def [](service) @@connections ||= Hash.new do |hash, key| - hash[key] = case key - when :compute - Fog::Slicehost::Compute.new - when :slices + if key == :slices location = caller.first warning = "[yellow][WARN] Slicehost[:blocks] is deprecated, use Bluebox[:compute] instead[/]" warning << " [light_black](" << location << ")[/] " Formatador.display_line(warning) - Fog::Slicehost::Compute.new end + hash[key] = class_for(key).new end @@connections[service] end