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
|
|
|
|
# @ return [String, 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
|
2010-12-15 18:32:33 -05:00
|
|
|
@credential = new_credential
|
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
|
|
|
|
@credential ||= :default
|
|
|
|
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
|
|
|
|
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-03-10 17:49:17 -05:00
|
|
|
|
|
|
|
def self.symbolize_credentials(args)
|
|
|
|
if args.is_a? Hash
|
|
|
|
Hash[ args.collect do |key, value|
|
|
|
|
[key.to_sym, self.symbolize_credentials(value)]
|
|
|
|
end ]
|
|
|
|
else
|
|
|
|
args
|
|
|
|
end
|
|
|
|
end
|
2010-12-09 21:44:02 -05:00
|
|
|
|
2010-04-02 13:14:37 -04:00
|
|
|
end
|