summaryrefslogtreecommitdiff
path: root/lib/puppet/util/trocla_helper.rb
blob: ce583f528c56eb0d29bafcb686f5c2728a1a54ef (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
module Puppet::Util::TroclaHelper
  def trocla(trocla_func,has_options,*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] || raise(Puppet::ParseError, "You need to pass at least a key as an argument!")
        format = args[1] || 'plain'
        options = args[2] || {}

        if options.is_a?(String)
          require 'yaml'
          options = YAML.load(options)
        end

        has_options ? store.send(trocla_func, key, format, options) : store.send(trocla_func, key, format)
  end
  module_function :trocla

  private

  def store
    @store ||= begin
      require 'trocla'
      configfile = File.join(File.dirname(Puppet.settings[:config]), "troclarc.yaml")

      raise(Puppet::ParseError, "Trocla config file #{configfile} is not readable") unless File.exist?(configfile)

      Trocla.new(configfile)
    end
  end
  module_function :store

end