Class: Wavefront::Credentials

Inherits:
Object
  • Object
show all
Defined in:
lib/wavefront-sdk/credentials.rb

Overview

Helper methods to get Wavefront credentials.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Credentials

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

Parameters:

  • options (Hash) (defaults to: {})

    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.



39
40
41
42
43
44
45
# 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

Instance Attribute Details

#allObject (readonly)

Returns the value of attribute all.



21
22
23
# File 'lib/wavefront-sdk/credentials.rb', line 21

def all
  @all
end

#configMap (readonly)

Returns the entire loaded config.

Returns:

  • (Map)

    the entire loaded config



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/wavefront-sdk/credentials.rb', line 20

class Credentials
  attr_reader :opts, :config, :creds, :proxy, :all

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

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

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

  # @return [Array] a list of possible credential files
  #
  def cred_files(opts = {})
    if opts.key?(:file)
      Array(Pathname.new(opts[:file]))
    else
      [Pathname.new('/etc/wavefront/credentials'),
       Pathname.new(Dir.home).join('.wavefront.conf'),
       Pathname.new(Dir.home).join('.wavefront')]
    end
  end

  def real_files(files)
    files.select { |f| f.exist? && f.file? }
  end

  # @param files [Array][Pathname] 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
  #
  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

  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

  # 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
  #
  def load_profile(file, profile = 'default')
    IniFile.load(file)[profile].transform_keys(&:to_sym)
  rescue StandardError
    raise Wavefront::Exception::InvalidConfigFile, file
  end
end

#credsMap (readonly)

Returns credentials for speaking to the Wavefront API.

Returns:

  • (Map)

    credentials for speaking to the Wavefront API



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/wavefront-sdk/credentials.rb', line 20

class Credentials
  attr_reader :opts, :config, :creds, :proxy, :all

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

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

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

  # @return [Array] a list of possible credential files
  #
  def cred_files(opts = {})
    if opts.key?(:file)
      Array(Pathname.new(opts[:file]))
    else
      [Pathname.new('/etc/wavefront/credentials'),
       Pathname.new(Dir.home).join('.wavefront.conf'),
       Pathname.new(Dir.home).join('.wavefront')]
    end
  end

  def real_files(files)
    files.select { |f| f.exist? && f.file? }
  end

  # @param files [Array][Pathname] 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
  #
  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

  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

  # 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
  #
  def load_profile(file, profile = 'default')
    IniFile.load(file)[profile].transform_keys(&:to_sym)
  rescue StandardError
    raise Wavefront::Exception::InvalidConfigFile, file
  end
end

#optsObject (readonly)

Returns the value of attribute opts.



21
22
23
# File 'lib/wavefront-sdk/credentials.rb', line 21

def opts
  @opts
end

#proxyMap (readonly)

Returns information for speaking to a Wavefront proxy.

Returns:

  • (Map)

    information for speaking to a Wavefront proxy



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/wavefront-sdk/credentials.rb', line 20

class Credentials
  attr_reader :opts, :config, :creds, :proxy, :all

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

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

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

  # @return [Array] a list of possible credential files
  #
  def cred_files(opts = {})
    if opts.key?(:file)
      Array(Pathname.new(opts[:file]))
    else
      [Pathname.new('/etc/wavefront/credentials'),
       Pathname.new(Dir.home).join('.wavefront.conf'),
       Pathname.new(Dir.home).join('.wavefront')]
    end
  end

  def real_files(files)
    files.select { |f| f.exist? && f.file? }
  end

  # @param files [Array][Pathname] 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
  #
  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

  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

  # 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
  #
  def load_profile(file, profile = 'default')
    IniFile.load(file)[profile].transform_keys(&:to_sym)
  rescue StandardError
    raise Wavefront::Exception::InvalidConfigFile, file
  end
end

Instance Method Details

#cred_files(opts = {}) ⇒ Array

Returns a list of possible credential files.

Returns:

  • (Array)

    a list of possible credential files



80
81
82
83
84
85
86
87
88
# 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(Dir.home).join('.wavefront.conf'),
     Pathname.new(Dir.home).join('.wavefront')]
  end
end

#env_override(raw) ⇒ Hash

If the user has set certain environment variables, their values will override values from the config file or command-line.

Parameters:

  • raw (Hash)

    the existing credentials

Returns:

  • (Hash)

    the modified credentials



53
54
55
56
57
58
# 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') ⇒ Hash

Returns the given profile from the given list of files. If multiple files match, the last one will be used.

Parameters:

  • files (Array)
    Pathname

    a list of ini-style config files

  • profile (String) (defaults to: 'default')

    a profile name

  • disallow_missing (Bool)

    whether or not to raise an exception if we are given a profile but can’t find it.

Returns:

  • (Hash)

    the given profile from the given list of files. If multiple files match, the last one will be used



101
102
103
104
105
106
107
108
109
110
111
112
113
114
# 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') ⇒ Hash

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.

Parameters:

  • file (Pathname)

    the file to read

  • profile (String) (defaults to: 'default')

    the section in the config to read

Returns:

  • (Hash)

    options loaded from file. Each key becomes a symbol



130
131
132
133
134
# 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) ⇒ Object

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

Parameters:

  • raw (Hash)

    the combined options from config file, command-line and env vars.

Returns:

  • void



67
68
69
70
71
72
73
74
75
76
# 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) ⇒ Object



116
117
118
119
120
121
# 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) ⇒ Object



90
91
92
# File 'lib/wavefront-sdk/credentials.rb', line 90

def real_files(files)
  files.select { |f| f.exist? && f.file? }
end