[core] simplify credential management

add ability to use Fog.credentials_path to use non '~/.fog'
try to use available credentials automagically even outside bin
This commit is contained in:
geemus 2010-12-15 15:32:33 -08:00
parent 05d38d315d
commit 4d739e6054
7 changed files with 39 additions and 42 deletions

View File

@ -2,9 +2,7 @@
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib', 'fog'))
require 'irb'
require 'yaml'
require File.join('fog', 'core', 'credentials')
Fog.credential = ARGV.first ? ARGV.first.to_sym : nil
Fog.bin = true
Fog.mock! if ENV['FOG_MOCK']
unless Fog.credentials
exit
@ -35,7 +33,7 @@ else
@irb.context.workspace = IRB::WorkSpace.new(binding)
Formatador.display_line('Welcome to fog interactive!')
Formatador.display_line(":#{Fog.credential.to_s} credentials provide #{providers}")
Formatador.display_line(":#{Fog.credential} provides #{providers}")
providers = Fog.providers
Fog.modules.each do |_module_|
if _module_.respond_to?(:startup_notice)

View File

@ -39,14 +39,6 @@ module Fog
end
def self.bin
@bin ||= false
end
def self.bin=(new_bin)
@bin = new_bin
end
def self.mock!
@mocking = true
end

View File

@ -1,42 +1,43 @@
require 'yaml'
module Fog
# Assign a new set of credentials for use in Fog
# @param [String, Symbol] new_credential The name of the new credential to use in Fog
# @ return [String, Symbol] The name of the new Fog credential
require 'fog/core/deprecation'
extend Fog::Deprecation
self_deprecate(:config_path, :credentials_path)
# Assign a new credential to use from configuration file
# @param [String, Symbol] new_credential name of new credential to use
# @ return [String, Symbol] name of the new credential
def self.credential=(new_credential)
@credentials = nil
@credential = new_credential
@credential = new_credential
end
# @return [String, Symbol] The credential in use by Fog
# @return [String, Symbol] The credential to use in Fog
def self.credential
@credential ||= :default
end
# @return [String] The credential's configuration path, read by Fog
def self.config_path
File.expand_path(ENV["FOG_RC"] || '~/.fog')
# @return [String] The path for configuration_file
def self.credentials_path
@credential_path ||= File.expand_path(ENV["FOG_RC"] || '~/.fog')
end
# @return [Hash] The credentials pulled from the config_path file
# @return [String] The new path for credentials file
def self.credentials_path=(new_credentials_path)
@credential_path = new_credentials_path
end
# @return [Hash] The credentials pulled from the configuration file
# @raise [LoadError] Configuration unavailable in configuration file
def self.credentials
@credentials ||= parse_config
end
# @return [Hash] The credentials pulled from the config_path file
# @raise [LoadError] Incorrect credential for config file
def self.parse_config
config = YAML.load_file(config_path)
if config && config[credential]
return config[credential]
else
raise LoadError.new missing_credentials
@credentials ||= begin
credentials = YAML.load_file(credentials_path)
(credentials && credentials[credential]) or raise LoadError.new missing_credentials
end
end
private
# @return [String] The error message that will be raised, if credentials cannot be found
def self.missing_credentials
<<-YML

View File

@ -2,12 +2,18 @@ module Fog
module Deprecation
def deprecate(older, newer)
class_eval <<-EOS, __FILE__, __LINE__
module_eval <<-EOS, __FILE__, __LINE__
def #{older}(*args)
location = caller.first
warning = "[yellow][WARN] #{self} => ##{older} is deprecated, use ##{newer} instead[/]"
warning << " [light_black](" << location << ")[/] "
Formatador.display_line(warning)
Formatador.display_line("[yellow][WARN] #{self} => ##{older} is deprecated, use ##{newer} instead[/] [light_black](#{caller.first})[/]")
send(:#{newer}, *args)
end
EOS
end
def self_deprecate(older, newer)
module_eval <<-EOS, __FILE__, __LINE__
def self.#{older}(*args)
Formatador.display_line("[yellow][WARN] #{self} => ##{older} is deprecated, use ##{newer} instead[/] [light_black](#{caller.first})[/]")
send(:#{newer}, *args)
end
EOS

View File

@ -59,9 +59,12 @@ module Fog
end
def new(options={})
if Fog.bin
# attempt to load credentials from config file
begin
default_credentials = filter_parameters(Fog.credentials)
options = default_credentials.merge(options)
rescue LoadError
# if there are no configured credentials, do nothing
end
setup_requirements

View File

@ -1,7 +1,6 @@
require 'spec'
require 'open-uri'
require 'fog'
Fog.bin = true
require 'fog/core/bin'
require 'fog/vcloud/bin'

View File

@ -1,8 +1,6 @@
require File.join(File.dirname(__FILE__), '..', 'lib', 'fog')
require 'fog/core/bin'
Fog.bin = true
__DIR__ = File.dirname(__FILE__)
$LOAD_PATH.unshift __DIR__ unless