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.
-
#populate(raw) ⇒ Object
Make the helper values.
- #raise_on_missing_profile(profile, profiles_found, conf_files_found) ⇒ Object
- #real_files(files) ⇒ Object
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
39 40 41 42 43 44 45 |
# File 'lib/wavefront-sdk/credentials.rb', line 39 def initialize( = {}) @raise_noprofile = [:raise_on_no_profile] || false raw = load_from_file(real_files(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 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( = {}) @raise_noprofile = [:raise_on_no_profile] || false raw = load_from_file(real_files(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) 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 |
#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 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( = {}) @raise_noprofile = [:raise_on_no_profile] || false raw = load_from_file(real_files(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) 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 |
#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 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( = {}) @raise_noprofile = [:raise_on_no_profile] || false raw = load_from_file(real_files(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) 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.
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.
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.
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.
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
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 |