Class: Puppet::Parser::Compiler

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec-hiera-puppet/puppet.rb

Instance Method Summary collapse

Instance Method Details

#compileObject



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/rspec-hiera-puppet/puppet.rb', line 6

def compile
  spec = Thread.current[:spec]

  if spec
    register_function_hiera(spec)
    register_function_hiera_array(spec)
    register_function_hiera_hash(spec)
    register_function_hiera_include(spec)
  end

  compile_unadorned
end

#compile_unadornedObject



4
# File 'lib/rspec-hiera-puppet/puppet.rb', line 4

alias_method :compile_unadorned, :compile

#register_function_hiera(spec) ⇒ Object



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
# File 'lib/rspec-hiera-puppet/puppet.rb', line 19

def register_function_hiera(spec)
  Puppet::Parser::Functions.newfunction(:hiera, :type => :rvalue) do |*args|
    # Functions called from puppet manifests that look like this:
    #   lookup("foo", "bar")
    # internally in puppet are invoked:  func(["foo", "bar"])
    #
    # where as calling from templates should work like this:
    #   scope.function_lookup("foo", "bar")
    #
    #  Therefore, declare this function with args '*args' to accept any number
    #  of arguments and deal with puppet's special calling mechanism now:
    if args[0].is_a?(Array)
      args = args[0]
    end

    key = args[0]
    default = args[1]
    override = args[2]

    require 'hiera'
    require 'hiera/scope'

    hiera = Hiera.new(:config => spec.hiera_config.merge(:logger => 'puppet'))

    if self.respond_to?("[]")
      hiera_scope = self
    else
      hiera_scope = Hiera::Scope.new(self)
    end

    answer = hiera.lookup(key, default, hiera_scope, override, :priority)

    raise(Puppet::ParseError, "Could not find data item #{key} in any Hiera data file and no default supplied") if answer.nil?

    return answer
  end
end

#register_function_hiera_array(spec) ⇒ Object



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
# File 'lib/rspec-hiera-puppet/puppet.rb', line 57

def register_function_hiera_array(spec)
  Puppet::Parser::Functions.newfunction(:hiera_array, :type => :rvalue) do |*args|
    if args[0].is_a?(Array)
      args = args[0]
    end

    key = args[0]
    default = args[1]
    override = args[2]

    require 'hiera'
    require 'hiera/scope'

    hiera = Hiera.new(:config => spec.hiera_config.merge(:logger => 'puppet'))

    if self.respond_to?("[]")
      hiera_scope = self
    else
      hiera_scope = Hiera::Scope.new(self)
    end

    answer = hiera.lookup(key, default, hiera_scope, override, :array)

    raise(Puppet::ParseError, "Could not find data item #{key} in any Hiera data file and no default supplied") if answer.empty?

    answer
  end
end

#register_function_hiera_hash(spec) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/rspec-hiera-puppet/puppet.rb', line 86

def register_function_hiera_hash(spec)
  Puppet::Parser::Functions.newfunction(:hiera_hash, :type => :rvalue) do |*args|
    if args[0].is_a?(Array)
      args = args[0]
    end

    raise(Puppet::ParseError, "Please supply a parameter to perform a Hiera lookup") if args.empty?

    key = args[0]
    default = args[1]
    override = args[2]

    require 'hiera'
    require 'hiera/scope'

    hiera = Hiera.new(:config => spec.hiera_config.merge(:logger => 'puppet'))

    if self.respond_to?("{}")
      hiera_scope = self
    else
      hiera_scope = Hiera::Scope.new(self)
    end

    answer = hiera.lookup(key, default, hiera_scope, override, :hash)

    raise(Puppet::ParseError, "Could not find data item #{key} in any Hiera data file and no default supplied") if answer.empty?

    answer
  end
end

#register_function_hiera_include(spec) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/rspec-hiera-puppet/puppet.rb', line 117

def register_function_hiera_include(spec)
  Puppet::Parser::Functions.newfunction(:hiera_include) do |*args|
    if args[0].is_a?(Array)
      args = args[0]
    end

    key = args[0]
    default = args[1]
    override = args[2]

    require 'hiera'
    require 'hiera/scope'

    hiera = Hiera.new(:config => spec.hiera_config.merge(:logger => 'puppet'))

    if self.respond_to?("[]")
      hiera_scope = self
    else
      hiera_scope = Hiera::Scope.new(self)
    end

    answer = hiera.lookup(key, default, hiera_scope, override, :array)

    raise(Puppet::ParseError, "Could not find data item #{key} in any Hiera data file and no default supplied") if answer.empty?

    method = Puppet::Parser::Functions.function(:include)
    send(method, answer)
  end
end