1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00

Went to town on the credentials.rb file!

This commit is contained in:
Scott Gonyea 2010-12-10 10:44:02 +08:00 committed by Wesley Beary
parent 42909d2d20
commit c0608d2ca1

View file

@ -1,61 +1,81 @@
require 'yaml'
module Fog
class << self
# 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
def self.credential=(new_credential)
@credentials = nil
@credential = new_credential
end
def credential=(new_credential)
@credential = new_credential
@credentials = nil
# @return [String, Symbol] The credential in use by 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')
end
# @return [Hash] The credentials pulled from the config_path 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(credential, config_path)
end
end
def credential
@credential || :default
end
private
# @param [String] _cred The name of the credential being used
# @param [String] _path Resource Config File's Path
# @return [String] The error message that will be raised, if credentials cannot be found
def self.missing_credentials(_cred, _path) <<-YML
Missing Credentials
def config_path
ENV["FOG_RC"] || '~/.fog'
end
To run as '#{_cred}', add the following to your resource config file: #{_path}
An alternate file may be used by placing its path in the FOG_RC environment variable
#######################################################
# Fog Resource Config File
#
# Key-value pairs should look like:
# :aws_access_key_id: 022QF06E7MXBSAMPLE
:#{_cred}:
:aws_access_key_id:
:aws_secret_access_key:
:bluebox_api_key:
:bluebox_customer_id:
:brightbox_client_id:
:brightbox_secret:
:go_grid_api_key:
:go_grid_shared_secret:
:google_storage_access_key_id:
:google_storage_secret_access_key:
:local_root:
:new_servers_password:
:new_servers_username:
:public_key_path:
:private_key_path:
:rackspace_api_key:
:rackspace_username:
:slicehost_password:
:terremark_username:
:terremark_password:
#
# End of Fog Resource Config File
#######################################################
def credentials
@credentials ||= begin
path = File.expand_path(config_path)
credentials = if File.exists?(path)
File.open(path) do |file|
YAML.load(file.read)
end
else
nil
end
unless credentials && credentials[credential]
print("\n To run as '#{credential}', add the following to #{config_path}\n")
yml = <<-YML
:#{credential}:
:aws_access_key_id: INTENTIONALLY_LEFT_BLANK
:aws_secret_access_key: INTENTIONALLY_LEFT_BLANK
:bluebox_api_key: INTENTIONALLY_LEFT_BLANK
:bluebox_customer_id: INTENTIONALLY_LEFT_BLANK
:brightbox_client_id: INTENTIONALLY_LEFT_BLANK
:brightbox_secret: INTENTIONALLY_LEFT_BLANK
:go_grid_api_key: INTENTIONALLY_LEFT_BLANK
:go_grid_shared_secret: INTENTIONALLY_LEFT_BLANK
:google_storage_access_key_id: INTENTIONALLY_LEFT_BLANK
:google_storage_secret_access_key: INTENTIONALLY_LEFT_BLANK
:local_root: INTENTIONALLY_LEFT_BLANK
:new_servers_password: INTENTIONALLY_LEFT_BLANK
:new_servers_username: INTENTIONALLY_LEFT_BLANK
:public_key_path: INTENTIONALLY_LEFT_BLANK
:private_key_path: INTENTIONALLY_LEFT_BLANK
:rackspace_api_key: INTENTIONALLY_LEFT_BLANK
:rackspace_username: INTENTIONALLY_LEFT_BLANK
:slicehost_password: INTENTIONALLY_LEFT_BLANK
:terremark_username: INTENTIONALLY_LEFT_BLANK
:terremark_password: INTENTIONALLY_LEFT_BLANK
YML
print("\n#{yml}\n")
raise(ArgumentError.new("Missing Credentials"))
end
credentials[credential]
end
end
end
end