diff options
author | kwadronaut <kwadronaut@leap.se> | 2016-07-25 00:44:22 +0200 |
---|---|---|
committer | kwadronaut <kwadronaut@leap.se> | 2016-07-25 00:44:22 +0200 |
commit | 30bc1e889dd0042132c4da21b94780c5a530b67c (patch) | |
tree | e430d45f553364c232626961df1647052166169d /lib/puppet/util | |
parent | c0d2832dff7fb14e056a49b28860087b2f201619 (diff) | |
parent | 8c1aac4f23d245cda54994737c72a868d112db87 (diff) |
Diffstat (limited to 'lib/puppet/util')
-rw-r--r-- | lib/puppet/util/trocla_helper.rb | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/lib/puppet/util/trocla_helper.rb b/lib/puppet/util/trocla_helper.rb new file mode 100644 index 0000000..ce583f5 --- /dev/null +++ b/lib/puppet/util/trocla_helper.rb @@ -0,0 +1,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 |