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



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

def initialize(options = {})
  raw = load_from_file(options)
  raw = env_override(raw)
  populate(raw)
end

Instance Attribute Details

#configMap (readonly)



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

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

  # 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(options)
    raw = env_override(raw)
    populate(raw)
  end

  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

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

  def load_from_file(opts)
    ret = {}

    profile = opts[:profile] || 'default'

    c_file = if opts.key?(:file)
               Array(Pathname.new(opts[:file]))
             else
               [Pathname.new('/etc/wavefront/credentials'),
                Pathname.new(ENV['HOME']) + '.wavefront']
             end

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

    ret
  end

  # Load in configuration (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)



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

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

  # 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(options)
    raw = env_override(raw)
    populate(raw)
  end

  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

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

  def load_from_file(opts)
    ret = {}

    profile = opts[:profile] || 'default'

    c_file = if opts.key?(:file)
               Array(Pathname.new(opts[:file]))
             else
               [Pathname.new('/etc/wavefront/credentials'),
                Pathname.new(ENV['HOME']) + '.wavefront']
             end

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

    ret
  end

  # Load in configuration (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.



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

def opts
  @opts
end

#proxyMap (readonly)



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

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

  # 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(options)
    raw = env_override(raw)
    populate(raw)
  end

  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

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

  def load_from_file(opts)
    ret = {}

    profile = opts[:profile] || 'default'

    c_file = if opts.key?(:file)
               Array(Pathname.new(opts[:file]))
             else
               [Pathname.new('/etc/wavefront/credentials'),
                Pathname.new(ENV['HOME']) + '.wavefront']
             end

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

    ret
  end

  # Load in configuration (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

#env_override(raw) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/wavefront-sdk/credentials.rb', line 39

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(opts) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/wavefront-sdk/credentials.rb', line 53

def load_from_file(opts)
  ret = {}

  profile = opts[:profile] || 'default'

  c_file = if opts.key?(:file)
             Array(Pathname.new(opts[:file]))
           else
             [Pathname.new('/etc/wavefront/credentials'),
              Pathname.new(ENV['HOME']) + '.wavefront']
           end

  c_file.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 configuration (optionally) given section of an ini-style configuration file not there, we don’t consider that an error.



83
84
85
86
87
# File 'lib/wavefront-sdk/credentials.rb', line 83

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



47
48
49
50
51
# File 'lib/wavefront-sdk/credentials.rb', line 47

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