Class: Hiera::Backend::Puppet_backend

Inherits:
Object
  • Object
show all
Defined in:
lib/hiera/backend/puppet_backend.rb

Instance Method Summary collapse

Constructor Details

#initializePuppet_backend

Returns a new instance of Puppet_backend.



4
5
6
# File 'lib/hiera/backend/puppet_backend.rb', line 4

def initialize
  Hiera.debug("Hiera Puppet backend starting")
end

Instance Method Details

#hierarchy(scope, override) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/hiera/backend/puppet_backend.rb', line 8

def hierarchy(scope, override)
  begin
    data_class = Config[:puppet][:datasource] || "data"
  rescue
    data_class = "data"
  end

  calling_class = scope.resource.name.to_s.downcase
  calling_module = calling_class.split("::").first

  hierarchy = Config[:hierarchy] || [calling_class, calling_module]

  hierarchy = [hierarchy].flatten.map do |klass|
    klass = Backend.parse_string(klass, scope,
      {
        "calling_class"  => calling_class,
        "calling_module" => calling_module
      }
    )

    next if klass == ""

    [data_class, klass].join("::")
  end.compact

  hierarchy << [calling_class, data_class].join("::")

  unless calling_module == calling_class
    hierarchy << [calling_module, data_class].join("::")
  end

  hierarchy.insert(0, [data_class, override].join("::")) if override

  hierarchy
end

#lookup(key, scope, order_override, resolution_type) ⇒ Object



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
# File 'lib/hiera/backend/puppet_backend.rb', line 44

def lookup(key, scope, order_override, resolution_type)
  answer = nil

  Hiera.debug("Looking up #{key} in Puppet backend")

  include_class = Puppet::Parser::Functions.function(:include)
  loaded_classes = scope.catalog.classes

  hierarchy(scope, order_override).each do |klass|
    Hiera.debug("Looking for data in #{klass}")

    varname = [klass, key].join("::")
    temp_answer = nil

    unless loaded_classes.include?(klass)
      begin
        if scope.respond_to?(:function_include)
          scope.function_include(klass)
        else
          scope.real.function_include(klass)
        end

        temp_answer = scope[varname]
        Hiera.debug("Found data in class #{klass}")
      rescue
      end
    else
      temp_answer = scope[varname]
    end

    next if temp_answer == :undefined

    if temp_answer
      # For array resolution we just append to the array whatever we
      # find, we then go onto the next file and keep adding to the array.
      #
      # For priority searches we break after the first found data item.
      case resolution_type
      when :array
        answer ||= []
        answer << Backend.parse_answer(temp_answer, scope)
      when :hash
        answer ||= {}
        answer = Backend.parse_answer(temp_answer, scope).merge answer
      else
        answer = Backend.parse_answer(temp_answer, scope)
        break
      end
    end
  end

  answer = nil if answer == :undefined

  answer
end