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