Module: Beaker::Shared::FogCredentials
- Included in:
- Beaker::Shared
- Defined in:
- lib/beaker/shared/fog_credentials.rb
Overview
A set of functions to read .fog files
Instance Method Summary collapse
-
#fog_credential_error(path = nil, from_env = nil, reason = nil) ⇒ ArgumentError
Constructs ArgumentError with common phrasing for #get_fog_credentials errors.
-
#get_fog_credentials(fog_file_path = '~/.fog', credential_group = :default) ⇒ StringifyHash
Load credentials from a .fog file.
Instance Method Details
#fog_credential_error(path = nil, from_env = nil, reason = nil) ⇒ ArgumentError
Constructs ArgumentError with common phrasing for #get_fog_credentials errors
13 14 15 16 17 18 19 20 |
# File 'lib/beaker/shared/fog_credentials.rb', line 13 def fog_credential_error(path = nil, from_env = nil, reason = nil) = "Failed loading credentials from .fog file" << " '#{path}'" if path << " #{from_env}" if from_env << "." << "Reason: #{reason}" if reason ArgumentError.new() end |
#get_fog_credentials(fog_file_path = '~/.fog', credential_group = :default) ⇒ StringifyHash
Note:
Loaded .fog files may use symbols for keys. Although not clearly documented, it is valid: www.rubydoc.info/gems/fog-core/1.42.0/Fog#credential-class_method github.com/fog/fog-core/blob/7865ef77ea990fd0d085e49c28e15957b7ce0d2b/spec/utils_spec.rb#L11
Load credentials from a .fog file
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/beaker/shared/fog_credentials.rb', line 33 def get_fog_credentials(fog_file_path = '~/.fog', credential_group = :default) # respect file location from env if ENV["FOG_RC"] fog_file_path = ENV["FOG_RC"] from_env = ' set in ENV["FOG_RC"]' end begin fog = YAML.load_file(fog_file_path) rescue Psych::SyntaxError, Errno::ENOENT => e raise fog_credential_error fog_file_path, from_env, "(#{e.class}) #{e.}" end raise fog_credential_error fog_file_path, from_env, "is empty." if fog == false # YAML.load => false for empty file # transparently support symbols or strings for keys fog = StringifyHash.new.merge!(fog) # respect credential from env # @note ENV must be a string, e.g. "default" not ":default" credential_group = ENV["FOG_CREDENTIAL"].to_sym if ENV["FOG_CREDENTIAL"] raise fog_credential_error fog_file_path, from_env, "could not load the specified credential group '#{credential_group}'." if not fog[credential_group] fog[credential_group] end |