Class: CMDB::Interface
- Inherits:
-
Object
- Object
- CMDB::Interface
- Defined in:
- lib/cmdb/interface.rb
Instance Method Summary collapse
-
#each_pair {|key, value| ... } ⇒ Interface
Enumerate all of the keys in the CMDB.
-
#get(key) ⇒ Object?
Retrieve the value of a CMDB key, searching all sources in the order they were initialized.
-
#get!(key) ⇒ Object?
Retrieve the value of a CMDB key; raise an exception if the key is not found.
-
#initialize(settings = {}) ⇒ Interface
constructor
Create a new instance of the CMDB interface.
-
#to_h ⇒ Object
Transform the entire CMDB into a flat Hash that can be merged into ENV.
Constructor Details
#initialize(settings = {}) ⇒ Interface
Create a new instance of the CMDB interface.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/cmdb/interface.rb', line 8 def initialize(settings = {}) @root = settings[:root] if settings namespaces = {} load_file_sources(namespaces) check_overlap(namespaces) @sources = [] # Load from consul source first if one is available. unless ConsulSource.url.nil? if ConsulSource.prefixes.nil? || ConsulSource.prefixes.empty? @sources << ConsulSource.new('') else ConsulSource.prefixes.each do |prefix| @sources << ConsulSource.new(prefix) end end end # Register valid sources with CMDB namespaces.each do |_, v| @sources << v.first end end |
Instance Method Details
#each_pair {|key, value| ... } ⇒ Interface
Enumerate all of the keys in the CMDB.
66 67 68 69 70 71 72 |
# File 'lib/cmdb/interface.rb', line 66 def each_pair(&block) @sources.each do |s| s.each_pair(&block) end self end |
#get(key) ⇒ Object?
Retrieve the value of a CMDB key, searching all sources in the order they were initialized.
38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/cmdb/interface.rb', line 38 def get(key) raise BadKey.new(key) unless key =~ VALID_KEY value = nil @sources.each do |s| value = s.get(key) break unless value.nil? end value end |
#get!(key) ⇒ Object?
Retrieve the value of a CMDB key; raise an exception if the key is not found.
56 57 58 |
# File 'lib/cmdb/interface.rb', line 56 def get!(key) get(key) || raise(MissingKey.new(key)) end |
#to_h ⇒ Object
Transform the entire CMDB into a flat Hash that can be merged into ENV. Key names are transformed into underscore-separated, uppercase strings; all runs of non-alphanumeric, non-underscore characters are tranformed into a single underscore.
The transformation rules make it possible for key names to conflict, e.g. “apple.orange.pear” and “apple.orange_pear” cannot exist in the same flat hash. This method checks for such conflicts and raises rather than returning bad data.
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/cmdb/interface.rb', line 85 def to_h values = {} sources = {} each_pair do |key, value| env_key = key_to_env(key) value = JSON.dump(value) unless value.is_a?(String) if sources.key?(env_key) raise NameConflict.new(env_key, [sources[env_key], key]) else sources[env_key] = key values[env_key] = value_to_env(value) end end values end |