class Wavefront::Credentials

Helper methods to get Wavefront credentials.

@return [Wavefront::Credentials]

@!attribute config [r]

@return [Map] the entire loaded config

@!attribute creds [r]

@return [Map] credentials for speaking to the Wavefront API

@!attribute proxy [r]

@return [Map] information for speaking to a Wavefront proxy

Attributes

all[R]
config[R]
creds[R]
opts[R]
proxy[R]

Public Class Methods

new(options = {}) click to toggle source

Gives you an object of credentials and options for speaking to Wavefront. It will look in the following places:

~/.wavefront ~/.wavefront.conf /etc/wavefront/credentials WAVEFRONT_ENDPOINT and WAVEFRONT_TOKEN environment variables

@param options [Hash] keys may be 'file', which

specifies a config file which will be loaded and parsed. If
no file is supplied, those listed above will be used.;
and/or 'profile' which select a profile section from 'file'.
Specify the key :raise_noprofile to have an exception thrown
if a given profile cannot be found. Otherwise that is ignored and
options are built from other sources.
# File lib/wavefront-sdk/credentials.rb, line 39
def initialize(options = {})
  @raise_noprofile = options[:raise_on_no_profile] || false
  raw = load_from_file(real_files(cred_files(options)),
                       options[:profile] || 'default')

  populate(env_override(raw))
end

Public Instance Methods

cred_files(opts = {}) click to toggle source

@return [Array] a list of possible credential files

# File lib/wavefront-sdk/credentials.rb, line 80
def cred_files(opts = {})
  if opts.key?(:file)
    Array(Pathname.new(opts[:file]))
  else
    [Pathname.new('/etc/wavefront/credentials'),
     Pathname.new(ENV['HOME']) + '.wavefront.conf',
     Pathname.new(ENV['HOME']) + '.wavefront']
  end
end
env_override(raw) click to toggle source

If the user has set certain environment variables, their values will override values from the config file or command-line. @param raw [Hash] the existing credentials @return [Hash] the modified credentials

# File lib/wavefront-sdk/credentials.rb, line 53
def env_override(raw)
  { endpoint: 'WAVEFRONT_ENDPOINT',
    token: 'WAVEFRONT_TOKEN',
    proxy: 'WAVEFRONT_PROXY' }.each { |k, v| raw[k] = ENV[v] if ENV[v] }
  raw
end
load_from_file(files, profile = 'default') click to toggle source

@param files [Array] a list of ini-style config files @param profile [String] a profile name @param disallow_missing [Bool] whether or not to raise an exception if

we are given a profile but can't find it.

@return [Hash] the given profile from the given list of files.

If multiple files match, the last one will be used
# File lib/wavefront-sdk/credentials.rb, line 101
def load_from_file(files, profile = 'default')
  ret = {}
  profiles_found = conf_files_found = 0

  files.each do |f|
    ret = load_profile(f, profile)
    conf_files_found += 1
    profiles_found += 1 unless ret.empty?
    ret[:file] = f
  end

  raise_on_missing_profile(profile, profiles_found, conf_files_found)
  ret
end
load_profile(file, profile = 'default') click to toggle source

Load in an (optionally) given section of an ini-style configuration file. If the section is not there, we don't consider that an error.

@param file [Pathname] the file to read @param profile [String] the section in the config to read @return [Hash] options loaded from file. Each key becomes a symbol

# File lib/wavefront-sdk/credentials.rb, line 130
def load_profile(file, profile = 'default')
  IniFile.load(file)[profile].transform_keys(&:to_sym)
rescue StandardError
  raise Wavefront::Exception::InvalidConfigFile, file
end
populate(raw) click to toggle source

Make the helper values. We use a Map so they're super-easy to access

@param raw [Hash] the combined options from config file,

command-line and env vars.

@return void

# File lib/wavefront-sdk/credentials.rb, line 67
def populate(raw)
  creds_keys = %i[endpoint token]
  proxy_keys = %i[proxy port]
  all_keys = creds_keys + proxy_keys

  @config = Map(raw)
  @creds  = Map(raw.select { |k, _v| creds_keys.include?(k) })
  @proxy  = Map(raw.select { |k, _v| proxy_keys.include?(k) })
  @all    = Map(raw.select { |k, _v| all_keys.include?(k) })
end
raise_on_missing_profile(profile, profiles_found, conf_files_found) click to toggle source
# File lib/wavefront-sdk/credentials.rb, line 116
def raise_on_missing_profile(profile, profiles_found, conf_files_found)
  return true unless @raise_noprofile
  return true unless profiles_found.zero? && conf_files_found.positive?

  raise Wavefront::Exception::MissingConfigProfile, profile
end
real_files(files) click to toggle source
# File lib/wavefront-sdk/credentials.rb, line 90
def real_files(files)
  files.select { |f| f.exist? && f.file? }
end