From c96614a11dc847ba901843dc79456381a51a37d4 Mon Sep 17 00:00:00 2001 From: geemus Date: Thu, 2 Sep 2010 13:39:01 -0700 Subject: [PATCH] consolidate credential management when running from bin --- bin/fog | 3 ++- lib/fog.rb | 8 ++++++++ lib/fog/aws/bin.rb | 11 ++++------- lib/fog/aws/ec2.rb | 2 +- lib/fog/bluebox/bin.rb | 5 +---- lib/fog/go_grid/bin.rb | 5 +---- lib/fog/linode/bin.rb | 5 +---- lib/fog/local/bin.rb | 5 +---- lib/fog/new_servers/bin.rb | 5 +---- lib/fog/rackspace/bin.rb | 7 ++----- lib/fog/service.rb | 5 +++++ lib/fog/slicehost/bin.rb | 5 +---- 12 files changed, 28 insertions(+), 38 deletions(-) diff --git a/bin/fog b/bin/fog index e673bdbfd..f3e14279c 100755 --- a/bin/fog +++ b/bin/fog @@ -3,7 +3,8 @@ require File.join(File.dirname(__FILE__), '..', 'lib', 'fog') require 'irb' require 'yaml' require File.join('fog', 'credentials') -Fog.credential = ARGV.first ? :"#{ARGV.first}" : :default +Fog.credential = ARGV.first +Fog.bin = true unless Fog.credentials exit end diff --git a/lib/fog.rb b/lib/fog.rb index 9f85fb3ae..08b340c0d 100644 --- a/lib/fog.rb +++ b/lib/fog.rb @@ -61,6 +61,14 @@ module Fog end + def self.bin + @bin ||= false + end + + def self.bin=(new_bin) + @bin = new_bin + end + def self.mock! @mocking = true end diff --git a/lib/fog/aws/bin.rb b/lib/fog/aws/bin.rb index 7fb4cbaa0..6e5c29e4c 100644 --- a/lib/fog/aws/bin.rb +++ b/lib/fog/aws/bin.rb @@ -9,18 +9,15 @@ module AWS def [](service) @@connections ||= Hash.new do |hash, key| - credentials = Fog.credentials.reject do |k, v| - ![:aws_access_key_id, :aws_secret_access_key].include?(k) - end hash[key] = case key when :ec2 - Fog::AWS::EC2.new(credentials) + Fog::AWS::EC2.new when :elb - Fog::AWS::ELB.new(credentials) + Fog::AWS::ELB.new when :simpledb - Fog::AWS::SimpleDB.new(credentials) + Fog::AWS::SimpleDB.new when :s3 - Fog::AWS::S3.new(credentials) + Fog::AWS::S3.new end end @@connections[service] diff --git a/lib/fog/aws/ec2.rb b/lib/fog/aws/ec2.rb index c78b7f594..6f6699f5b 100644 --- a/lib/fog/aws/ec2.rb +++ b/lib/fog/aws/ec2.rb @@ -1,4 +1,4 @@ - module Fog +module Fog module AWS module EC2 extend Fog::Service diff --git a/lib/fog/bluebox/bin.rb b/lib/fog/bluebox/bin.rb index 2524f019a..d40d29423 100644 --- a/lib/fog/bluebox/bin.rb +++ b/lib/fog/bluebox/bin.rb @@ -9,12 +9,9 @@ module Bluebox def [](service) @@connections ||= Hash.new do |hash, key| - credentials = Fog.credentials.reject do |k,v| - ![:bluebox_api_key, :bluebox_customer_id].include?(k) - end hash[key] = case key when :blocks - Fog::Bluebox.new(credentials) + Fog::Bluebox.new end end @@connections[service] diff --git a/lib/fog/go_grid/bin.rb b/lib/fog/go_grid/bin.rb index 0af05c230..fab561fd2 100644 --- a/lib/fog/go_grid/bin.rb +++ b/lib/fog/go_grid/bin.rb @@ -8,12 +8,9 @@ module GoGrid def [](service) @@connections ||= Hash.new do |hash, key| - credentials = Fog.credentials.reject do |k,v| - ![:go_grid_api_key, :go_grid_shared_secret].include?(k) - end hash[key] = case key when :go_grid - Fog::GoGrid.new(credentials) + Fog::GoGrid.new end end @@connections[service] diff --git a/lib/fog/linode/bin.rb b/lib/fog/linode/bin.rb index 751016482..7174d6157 100644 --- a/lib/fog/linode/bin.rb +++ b/lib/fog/linode/bin.rb @@ -8,12 +8,9 @@ module Linode def [](service) @@connections ||= Hash.new do |hash, key| - credentials = Fog.credentials.reject do |k,v| - ![:linode_api_key].include?(k) - end hash[key] = case key when :linode - Fog::Linode.new(credentials) + Fog::Linode.new end end @@connections[service] diff --git a/lib/fog/local/bin.rb b/lib/fog/local/bin.rb index 8f7940392..b06f61a7c 100644 --- a/lib/fog/local/bin.rb +++ b/lib/fog/local/bin.rb @@ -8,12 +8,9 @@ module Local def [](service) @@connections ||= Hash.new do |hash, key| - credentials = Fog.credentials.reject do |k,v| - ![:local_root].include?(k) - end hash[key] = case key when :files - Fog::Local.new(credentials) + Fog::Local.new end end @@connections[service] diff --git a/lib/fog/new_servers/bin.rb b/lib/fog/new_servers/bin.rb index b30102e18..e51697260 100644 --- a/lib/fog/new_servers/bin.rb +++ b/lib/fog/new_servers/bin.rb @@ -8,12 +8,9 @@ module NewServers def [](service) @@connections ||= Hash.new do |hash, key| - credentials = Fog.credentials.reject do |k,v| - ![:new_servers_password, :new_servers_username].include?(k) - end hash[key] = case key when :new_servers - Fog::NewServers.new(credentials) + Fog::NewServers.new end end @@connections[service] diff --git a/lib/fog/rackspace/bin.rb b/lib/fog/rackspace/bin.rb index 68a3b45cf..e806aca57 100644 --- a/lib/fog/rackspace/bin.rb +++ b/lib/fog/rackspace/bin.rb @@ -8,14 +8,11 @@ module Rackspace def [](service) @@connections ||= Hash.new do |hash, key| - credentials = Fog.credentials.reject do |k,v| - ![:rackspace_api_key, :rackspace_username].include?(k) - end hash[key] = case key when :files - Fog::Rackspace::Files.new(credentials) + Fog::Rackspace::Files.new when :servers - Fog::Rackspace::Servers.new(credentials) + Fog::Rackspace::Servers.new end end @@connections[service] diff --git a/lib/fog/service.rb b/lib/fog/service.rb index 42825ebf2..0dc28ee88 100644 --- a/lib/fog/service.rb +++ b/lib/fog/service.rb @@ -10,6 +10,11 @@ module Fog module Collections; end def self.new(options={}) + if Fog.bin + default_credentials = Fog.credentials.reject {|key, value| !requirements.include?(key)} + options = default_credentials.merge(options) + end + missing = [] for requirement in requirements missing << requirement unless options[requirement] diff --git a/lib/fog/slicehost/bin.rb b/lib/fog/slicehost/bin.rb index 7593398a7..1fbddf80a 100644 --- a/lib/fog/slicehost/bin.rb +++ b/lib/fog/slicehost/bin.rb @@ -8,12 +8,9 @@ module Slicehost def [](service) @@connections ||= Hash.new do |hash, key| - credentials = Fog.credentials.reject do |k,v| - ![:slicehost_password].include?(k) - end hash[key] = case key when :slices - Fog::Slicehost.new(credentials) + Fog::Slicehost.new end end @@connections[service]