summaryrefslogtreecommitdiff
path: root/lib/hiera/backend/puppet_backend.rb
blob: a227ce240ee305bd8af49d20632b4f7e8b06af16 (plain)
1
2
3
4
5
6
7
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
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
require 'hiera/backend'

class Hiera
  module Backend
    class Puppet_backend
      def initialize
        Hiera.debug("Hiera Puppet backend starting")
      end

      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

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

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

        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

          # Note that temp_answer might be define but false.
          if temp_answer.nil?
            next
          else
            # 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
      end
    end
  end
end