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

Instance Method Details

#fog_credential_error(path = nil, from_env = nil, reason = nil) ⇒ ArgumentError

Constructs ArgumentError with common phrasing for #get_fog_credentials errors

Parameters:

  • path (String) (defaults to: nil)

    path to offending file

  • from_env (String) (defaults to: nil)

    if the path was overridden in ENV

  • reason (String) (defaults to: nil)

    explanation for the failure

Returns:

  • (ArgumentError)

    ArgumentError with preformatted message



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)
  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) ⇒ StringifyHash

Load credentials from a .fog file

Parameters:

  • fog_file_path (String) (defaults to: '~/.fog')

    dot fog path. Overridden by ENV

  • credential_group (String, Symbol) (defaults to: :default)

    Credential group to use. Overridden by ENV

Returns:

  • (StringifyHash)

    credentials stored in fog_file_path

Raises:

  • (ArgumentError)

    when the credentials cannot be loaded, describing the reson



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.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