2010-06-02 14:40:25 -07:00
|
|
|
require 'yaml'
|
2010-12-10 10:44:02 +08:00
|
|
|
|
2010-04-02 10:14:37 -07:00
|
|
|
module Fog
|
2010-12-15 15:32:33 -08:00
|
|
|
require 'fog/core/deprecation'
|
|
|
|
|
|
|
|
# Assign a new credential to use from configuration file
|
|
|
|
# @param [String, Symbol] new_credential name of new credential to use
|
2011-11-16 15:08:32 -08:00
|
|
|
# @ return [Symbol] name of the new credential
|
2010-12-10 10:44:02 +08:00
|
|
|
def self.credential=(new_credential)
|
2011-02-28 03:08:56 +08:00
|
|
|
@credentials = nil
|
2011-11-16 15:08:32 -08:00
|
|
|
@credential = new_credential && new_credential.to_sym
|
2010-12-10 10:44:02 +08:00
|
|
|
end
|
2010-04-02 10:14:37 -07:00
|
|
|
|
2010-12-15 15:32:33 -08:00
|
|
|
# @return [String, Symbol] The credential to use in Fog
|
2010-12-10 10:44:02 +08:00
|
|
|
def self.credential
|
2011-11-16 15:08:32 -08:00
|
|
|
@credential ||= ( ENV["FOG_CREDENTIAL"] && ENV["FOG_CREDENTIAL"].to_sym ) || :default
|
2010-12-10 10:44:02 +08:00
|
|
|
end
|
2010-04-02 10:14:37 -07:00
|
|
|
|
2010-12-15 15:32:33 -08:00
|
|
|
# @return [String] The path for configuration_file
|
|
|
|
def self.credentials_path
|
2011-03-30 17:50:35 -06:00
|
|
|
@credential_path ||= begin
|
2011-05-13 12:58:40 -07:00
|
|
|
path = ENV["FOG_RC"] || (ENV['HOME'] && File.directory?(ENV['HOME']) && '~/.fog')
|
2011-03-30 17:50:35 -06:00
|
|
|
File.expand_path(path) if path
|
2011-07-13 11:17:00 -07:00
|
|
|
rescue
|
|
|
|
nil
|
2011-03-30 17:50:35 -06:00
|
|
|
end
|
2010-12-10 10:44:02 +08:00
|
|
|
end
|
2010-04-02 10:14:37 -07:00
|
|
|
|
2010-12-15 15:32:33 -08:00
|
|
|
# @return [String] The new path for credentials file
|
|
|
|
def self.credentials_path=(new_credentials_path)
|
2011-02-28 03:08:56 +08:00
|
|
|
@credentials = nil
|
2010-12-15 15:32:33 -08:00
|
|
|
@credential_path = new_credentials_path
|
2010-12-10 10:44:02 +08:00
|
|
|
end
|
2010-05-14 02:50:34 +08:00
|
|
|
|
2010-12-15 15:32:33 -08:00
|
|
|
# @return [Hash] The credentials pulled from the configuration file
|
|
|
|
# @raise [LoadError] Configuration unavailable in configuration file
|
|
|
|
def self.credentials
|
|
|
|
@credentials ||= begin
|
2011-03-07 11:02:54 -08:00
|
|
|
if credentials_path && File.exists?(credentials_path)
|
2011-03-11 06:49:17 +08:00
|
|
|
credentials = self.symbolize_credentials(YAML.load_file(credentials_path))
|
2011-05-04 13:12:52 -07:00
|
|
|
(credentials && credentials[credential]) || Fog::Errors.missing_credentials
|
2010-12-22 10:20:13 -08:00
|
|
|
else
|
|
|
|
{}
|
|
|
|
end
|
2010-04-02 10:14:37 -07:00
|
|
|
end
|
2010-12-10 10:44:02 +08:00
|
|
|
end
|
2011-02-07 22:42:18 -05:00
|
|
|
|
2011-09-23 16:26:56 -05:00
|
|
|
# @return [Hash] The newly assigned credentials
|
|
|
|
def self.credentials=(new_credentials)
|
|
|
|
@credentials = new_credentials
|
|
|
|
end
|
|
|
|
|
2011-03-11 06:49:17 +08:00
|
|
|
def self.symbolize_credentials(args)
|
|
|
|
if args.is_a? Hash
|
2012-05-04 18:46:39 +08:00
|
|
|
copy = Array.new
|
|
|
|
args.each do |key, value|
|
|
|
|
copy.push(key.to_sym, self.symbolize_credentials(value))
|
|
|
|
end
|
|
|
|
Hash[*copy]
|
2011-03-11 06:49:17 +08:00
|
|
|
else
|
|
|
|
args
|
|
|
|
end
|
|
|
|
end
|
2010-12-10 10:44:02 +08:00
|
|
|
|
2010-04-02 10:14:37 -07:00
|
|
|
end
|