module Beaker::Shared::FogCredentials

A set of functions to read .fog files

Public Instance Methods

fog_credential_error(path = nil, from_env = nil, reason = nil) click to toggle source

Constructs ArgumentError with common phrasing for get_fog_credentials errors

@param path [String] path to offending file @param from_env [String] if the path was overridden in ENV @param reason [String] explanation for the failure @return [ArgumentError] ArgumentError with preformatted message

# File lib/beaker/shared/fog_credentials.rb, line 13
def fog_credential_error(path = nil, from_env = nil, reason = nil)
  message = "Failed loading credentials from .fog file"
  message << " '#{path}'" if path
  message << " #{from_env}" if from_env
  message << "."
  message << "Reason: #{reason}" if reason
  ArgumentError.new(message)
end
get_fog_credentials(fog_file_path = '~/.fog', credential_group = :default) click to toggle source

Load credentials from a .fog file

@note Loaded .fog files may use symbols for keys.

Although not clearly documented, it is valid:
https://www.rubydoc.info/gems/fog-core/1.42.0/Fog#credential-class_method
https://github.com/fog/fog-core/blob/7865ef77ea990fd0d085e49c28e15957b7ce0d2b/spec/utils_spec.rb#L11

@param fog_file_path [String] dot fog path. Overridden by ENV @param credential_group [String, Symbol] Credential group to use. Overridden by ENV @return [StringifyHash] credentials stored in fog_file_path @raise [ArgumentError] when the credentials cannot be loaded, describing the reson

# 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.message}"
  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