Class: Wavefront::Credentials
- Inherits:
-
Object
- Object
- Wavefront::Credentials
- Defined in:
- lib/wavefront-sdk/credentials.rb
Overview
Helper methods to get Wavefront credentials.
Instance Attribute Summary collapse
-
#all ⇒ Object
readonly
Returns the value of attribute all.
-
#config ⇒ Map
readonly
The entire loaded config.
-
#creds ⇒ Map
readonly
Credentials for speaking to the Wavefront API.
-
#opts ⇒ Object
readonly
Returns the value of attribute opts.
-
#proxy ⇒ Map
readonly
Information for speaking to a Wavefront proxy.
Instance Method Summary collapse
-
#cred_files(opts = {}) ⇒ Array
A list of possible credential files.
-
#env_override(raw) ⇒ Hash
If the user has set certain environment variables, their values will override values from the config file or command-line.
-
#initialize(options = {}) ⇒ Credentials
constructor
Gives you an object of credentials and options for speaking to Wavefront.
-
#load_from_file(files, profile = 'default') ⇒ Hash
The given profile from the given list of files.
-
#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.
-
#populate(raw) ⇒ Object
Make the helper values.
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
36 37 38 39 40 |
# File 'lib/wavefront-sdk/credentials.rb', line 36 def initialize( = {}) raw = load_from_file(cred_files(), [:profile] || 'default') populate(env_override(raw)) end |
Instance Attribute Details
#all ⇒ Object (readonly)
Returns the value of attribute all.
21 22 23 |
# File 'lib/wavefront-sdk/credentials.rb', line 21 def all @all end |
#config ⇒ Map (readonly)
Returns 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 |
# 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' # def initialize( = {}) raw = load_from_file(cred_files(), [: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.conf', 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? && f.file? 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 rescue StandardError raise Wavefront::Exception::InvalidConfigFile, file end end |
#creds ⇒ Map (readonly)
Returns 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 |
# 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' # def initialize( = {}) raw = load_from_file(cred_files(), [: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.conf', 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? && f.file? 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 rescue StandardError raise Wavefront::Exception::InvalidConfigFile, file end end |
#opts ⇒ Object (readonly)
Returns the value of attribute opts.
21 22 23 |
# File 'lib/wavefront-sdk/credentials.rb', line 21 def opts @opts end |
#proxy ⇒ Map (readonly)
Returns 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 |
# 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' # def initialize( = {}) raw = load_from_file(cred_files(), [: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.conf', 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? && f.file? 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 rescue StandardError raise Wavefront::Exception::InvalidConfigFile, file end end |
Instance Method Details
#cred_files(opts = {}) ⇒ Array
Returns a list of possible credential files.
73 74 75 76 77 78 79 80 81 |
# File 'lib/wavefront-sdk/credentials.rb', line 73 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) ⇒ Hash
If the user has set certain environment variables, their values will override values from the config file or command-line.
48 49 50 51 52 53 |
# File 'lib/wavefront-sdk/credentials.rb', line 48 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.
88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/wavefront-sdk/credentials.rb', line 88 def load_from_file(files, profile = 'default') ret = {} files.each do |f| next unless f.exist? && f.file? 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.
109 110 111 112 113 114 115 |
# File 'lib/wavefront-sdk/credentials.rb', line 109 def load_profile(file, profile = 'default') IniFile.load(file)[profile].each_with_object({}) do |(k, v), memo| memo[k.to_sym] = v end 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
62 63 64 65 66 67 68 69 |
# File 'lib/wavefront-sdk/credentials.rb', line 62 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 |