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 /etc/wavefront/credentials WAVEFRONT_ENDPOINT and WAVEFRONT_TOKEN environment variables



32
33
34
35
36
# File 'lib/wavefront-sdk/credentials.rb', line 32

def initialize(options = {})
  raw = load_from_file(cred_files(options), options[:profile] ||
                       'default')
  populate(env_override(raw))
end

Instance Attribute Details

#allObject (readonly)

Returns the value of attribute all.



18
19
20
# File 'lib/wavefront-sdk/credentials.rb', line 18

def all
  @all
end

#configMap (readonly)



17
18
19
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
# File 'lib/wavefront-sdk/credentials.rb', line 17

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
  # /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'
  #
  def initialize(options = {})
    raw = load_from_file(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)
    @config = Map(raw)
    @creds  = Map(raw.select { |k, _v| i[endpoint token].include?(k) })
    @proxy  = Map(raw.select { |k, _v| i[proxy port].include?(k) })
    @all    = Map(raw.select do |k, _v|
      i[proxy port endpoint token].include?(k)
    end)
  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(ENV['HOME']) + '.wavefront']
    end
  end

  # @param files [Array][Pathname] a list of ini-style config files
  # @param profile [String] a profile name
  # @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 = {}

    files.each do |f|
      next unless f.exist?
      ret = load_profile(f, profile)
      ret[:file] = f
    end

    ret
  end

  # Load in an (optionally) given section of an ini-style
  # configuration file 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].each_with_object({}) do |(k, v), memo|
      memo[k.to_sym] = v
    end
  end
end

#credsMap (readonly)



17
18
19
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
# File 'lib/wavefront-sdk/credentials.rb', line 17

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
  # /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'
  #
  def initialize(options = {})
    raw = load_from_file(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)
    @config = Map(raw)
    @creds  = Map(raw.select { |k, _v| i[endpoint token].include?(k) })
    @proxy  = Map(raw.select { |k, _v| i[proxy port].include?(k) })
    @all    = Map(raw.select do |k, _v|
      i[proxy port endpoint token].include?(k)
    end)
  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(ENV['HOME']) + '.wavefront']
    end
  end

  # @param files [Array][Pathname] a list of ini-style config files
  # @param profile [String] a profile name
  # @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 = {}

    files.each do |f|
      next unless f.exist?
      ret = load_profile(f, profile)
      ret[:file] = f
    end

    ret
  end

  # Load in an (optionally) given section of an ini-style
  # configuration file 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].each_with_object({}) do |(k, v), memo|
      memo[k.to_sym] = v
    end
  end
end

#optsObject (readonly)

Returns the value of attribute opts.



18
19
20
# File 'lib/wavefront-sdk/credentials.rb', line 18

def opts
  @opts
end

#proxyMap (readonly)



17
18
19
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
# File 'lib/wavefront-sdk/credentials.rb', line 17

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
  # /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'
  #
  def initialize(options = {})
    raw = load_from_file(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)
    @config = Map(raw)
    @creds  = Map(raw.select { |k, _v| i[endpoint token].include?(k) })
    @proxy  = Map(raw.select { |k, _v| i[proxy port].include?(k) })
    @all    = Map(raw.select do |k, _v|
      i[proxy port endpoint token].include?(k)
    end)
  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(ENV['HOME']) + '.wavefront']
    end
  end

  # @param files [Array][Pathname] a list of ini-style config files
  # @param profile [String] a profile name
  # @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 = {}

    files.each do |f|
      next unless f.exist?
      ret = load_profile(f, profile)
      ret[:file] = f
    end

    ret
  end

  # Load in an (optionally) given section of an ini-style
  # configuration file 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].each_with_object({}) do |(k, v), memo|
      memo[k.to_sym] = v
    end
  end
end

Instance Method Details

#cred_files(opts = {}) ⇒ Array



69
70
71
72
73
74
75
76
# File 'lib/wavefront-sdk/credentials.rb', line 69

def cred_files(opts = {})
  if opts.key?(:file)
    Array(Pathname.new(opts[:file]))
  else
    [Pathname.new('/etc/wavefront/credentials'),
     Pathname.new(ENV['HOME']) + '.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.



44
45
46
47
48
49
# File 'lib/wavefront-sdk/credentials.rb', line 44

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



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/wavefront-sdk/credentials.rb', line 83

def load_from_file(files, profile = 'default')
  ret = {}

  files.each do |f|
    next unless f.exist?
    ret = load_profile(f, profile)
    ret[:file] = f
  end

  ret
end

#load_profile(file, profile = 'default') ⇒ Hash

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



103
104
105
106
107
# File 'lib/wavefront-sdk/credentials.rb', line 103

def load_profile(file, profile = 'default')
  IniFile.load(file)[profile].each_with_object({}) do |(k, v), memo|
    memo[k.to_sym] = v
  end
end

#populate(raw) ⇒ Object

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



58
59
60
61
62
63
64
65
# File 'lib/wavefront-sdk/credentials.rb', line 58

def populate(raw)
  @config = Map(raw)
  @creds  = Map(raw.select { |k, _v| i[endpoint token].include?(k) })
  @proxy  = Map(raw.select { |k, _v| i[proxy port].include?(k) })
  @all    = Map(raw.select do |k, _v|
    i[proxy port endpoint token].include?(k)
  end)
end