Class: HieraController

Inherits:
Object
  • Object
show all
Defined in:
lib/hiera_browser/hiera_controller.rb

Overview

HieraController gives us a reasonably limited interface for Hiera

Author:

  • David Gwilliam

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ void

Parameters:

  • args ({:hiera_yaml => String}) (defaults to: {})

    path to ‘hiera.yaml`



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_yamlObject (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

#configHash

Returns:

  • (Hash)


23
24
25
# File 'lib/hiera_browser/hiera_controller.rb', line 23

def config
  hiera.config
end

#datadirsArray

Returns:

  • (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

Parameters:

  • args ({:scope => Hash})

Returns:

  • (Hash)


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

Parameters:

  • args (Hash) (defaults to: {})

    arguments to pass to Hiera.new()

Returns:

  • (Hiera)


18
19
20
# File 'lib/hiera_browser/hiera_controller.rb', line 18

def hiera(args={})
  @hiera || Hiera.new(args)
end

#hierarchyArray

Returns:

  • (Array)


44
45
46
# File 'lib/hiera_browser/hiera_controller.rb', line 44

def hierarchy
  config[:hierarchy]
end

#hierarchy_variablesObject



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

#keysArray

Returns:

  • (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

Note:

needs to be moved to Node#lookup

Basically shadows the Hiera#lookup method

Parameters:

  • args ({:key => String, :scope => Hash, :resolution_type => Symbol}) (defaults to: {})

Returns:

  • (Hash)

Raises:

  • (ArgumentError)


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

Parameters:

  • args ({:key => String, :scope => Hash})

Returns:

  • (Hash)


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

Note:

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' }

Parameters:

  • args ({:scope => Hash})

Returns:

  • (Hash)


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