Class: HieraController
- Inherits:
-
Object
- Object
- HieraController
- Defined in:
- lib/hiera_browser/hiera_controller.rb
Overview
HieraController gives us a reasonably limited interface for Hiera
Instance Attribute Summary collapse
-
#hiera_yaml ⇒ Object
readonly
Returns the value of attribute hiera_yaml.
Instance Method Summary collapse
- #config ⇒ Hash
- #datadirs ⇒ Array
-
#get_all(args) ⇒ Hash
Retrieve all node values for all known hiera keys.
- #hiera(args = {}) ⇒ Hiera
- #hierarchy ⇒ Array
- #hierarchy_variables ⇒ Object
- #initialize(args = {}) ⇒ void constructor
- #keys ⇒ Array
-
#lookup(args = {}) ⇒ Hash
Basically shadows the Hiera#lookup method.
-
#lookup_additive(args) ⇒ Hash
Check return value of priority lookup in order to determine which “additive” resolution type to use, then repeat the lookup with the correct resolution type.
-
#top_scopify(args) ⇒ Hash
Return the scope but with the addition of fully qualified variable keys for any level of the hierarchy that’s formatted that way, e.g.: { ‘datacenter’ => ‘pdx’, ‘::datacenter’ => ‘pdx’ }.
Constructor Details
#initialize(args = {}) ⇒ void
11 12 13 14 |
# File 'lib/hiera_browser/hiera_controller.rb', line 11 def initialize(args={}) @hiera_yaml = args[:hiera_yaml] || ENV['HIERA_YAML'] @hiera = hiera(:config => @hiera_yaml) end |
Instance Attribute Details
#hiera_yaml ⇒ Object (readonly)
Returns the value of attribute hiera_yaml.
7 8 9 |
# File 'lib/hiera_browser/hiera_controller.rb', line 7 def hiera_yaml @hiera_yaml end |
Instance Method Details
#config ⇒ Hash
23 24 25 |
# File 'lib/hiera_browser/hiera_controller.rb', line 23 def config hiera.config end |
#datadirs ⇒ Array
28 29 30 31 32 33 34 35 36 |
# File 'lib/hiera_browser/hiera_controller.rb', line 28 def datadirs config[:backends].map{|b| path = config[b.to_sym][:datadir] DataDir.new( :hiera => self, :path => path, ) } end |
#get_all(args) ⇒ Hash
Retrieve all node values for all known hiera keys
91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/hiera_browser/hiera_controller.rb', line 91 def get_all(args) scope = top_scopify(:scope => args[:scope]) values = keys.inject({}){|a, k| a.merge({k => lookup(:key => k, :scope => scope)}) } if args[:additive_keys] additive_values = args[:additive_keys].inject({}){|a,k| a.merge({k => lookup_additive(:key => k, :scope => scope)}) } values = values.delete_if {|k,v| additive_values.has_key?(k)}.merge!(additive_values) end values end |
#hiera(args = {}) ⇒ Hiera
18 19 20 |
# File 'lib/hiera_browser/hiera_controller.rb', line 18 def hiera(args={}) @hiera || Hiera.new(args) end |
#hierarchy ⇒ Array
44 45 46 |
# File 'lib/hiera_browser/hiera_controller.rb', line 44 def hierarchy config[:hierarchy] end |
#hierarchy_variables ⇒ Object
48 49 50 51 52 53 54 55 56 |
# File 'lib/hiera_browser/hiera_controller.rb', line 48 def hierarchy_variables hierarchy.map{|datasource| begin datasource.match(/\%\{([\:a-z_]+)\}/)[1] rescue NoMethodError datasource end } end |
#keys ⇒ Array
39 40 41 |
# File 'lib/hiera_browser/hiera_controller.rb', line 39 def keys datadirs.map{|d| d.keys}.flatten.uniq.sort end |
#lookup(args = {}) ⇒ Hash
needs to be moved to Node#lookup
Basically shadows the Hiera#lookup method
79 80 81 82 83 84 85 |
# File 'lib/hiera_browser/hiera_controller.rb', line 79 def lookup(args = {}) raise ArgumentError, 'HieraController#lookup requires both :key and :scope args' unless args[:key] and args[:scope] key = args[:key] scope = top_scopify(:scope => args[:scope]) resolution_type = args[:resolution_type] || :priority Hash[*[key,hiera.lookup(key, nil, scope, nil, resolution_type)]] end |
#lookup_additive(args) ⇒ Hash
Check return value of priority lookup in order to determine which “additive” resolution type to use, then repeat the lookup with the correct resolution type
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/hiera_browser/hiera_controller.rb', line 108 def lookup_additive(args) key = args[:key] scope = top_scopify(:scope => args[:scope]) value = lookup(:key => key, :scope => scope) lookup_type = case value.values.pop when Hash :hash when TrueClass, FalseClass :priority else :array end lookup(:key => key, :scope => scope, :resolution_type => lookup_type) end |
#top_scopify(args) ⇒ Hash
needs to be moved to Node
Return the scope but with the addition of fully qualified
variable keys for any level of the hierarchy that's formatted that way, e.g.:
{ 'datacenter' => 'pdx', '::datacenter' => 'pdx' }
65 66 67 68 69 70 71 72 |
# File 'lib/hiera_browser/hiera_controller.rb', line 65 def top_scopify(args) scope = args[:scope] fix_keys = hierarchy_variables.select{|datasource| datasource.start_with?(Parameter.top_scope)} scope.inject({}){|a,fact| a[Parameter.top_scope(fact.first)] = fact.last if fix_keys.include?(Parameter.top_scope(fact.first)) a[fact.first] = fact.last a } end |