Please review pull request #50: (#14460) Add Puppet parser functions opened by (kelseyhightower)
Description:
This patch bundles the following Puppet parser functions from
hiera-puppet:
- hiera
- hiera_array
- hiera_hash
- Opened: Mon May 14 20:03:29 UTC 2012
- Based on: puppetlabs:master (c02b85c2b539b3faa336f6baef91c349bfbe9d5e)
- Requested merge: kelseyhightower:ticket/master/14460_add_puppet_parser_functions (540d2d6e7311c26ca7560a8ac4edcd082f3d0ac9)
Diff follows:
diff --git a/lib/puppet/parser/functions/hiera.rb b/lib/puppet/parser/functions/hiera.rb
new file mode 100644
index 0000000..c8d7b64
--- /dev/null
+++ b/lib/puppet/parser/functions/hiera.rb
@@ -0,0 +1,45 @@
+module 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]
+
+ configfile = File.join([File.dirname(Puppet.settings[:config]), "hiera.yaml"])
+
+ raise(Puppet::ParseError, "Hiera config file #{configfile} not readable") unless File.exist?(configfile)
+
+ require 'hiera'
+ require 'hiera/scope'
+
+ config = YAML.load_file(configfile)
+ config[:logger] = "puppet"
+
+ hiera = Hiera.new(:config => config)
+
+ 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
+
diff --git a/lib/puppet/parser/functions/hiera_array.rb b/lib/puppet/parser/functions/hiera_array.rb
new file mode 100644
index 0000000..03e5410
--- /dev/null
+++ b/lib/puppet/parser/functions/hiera_array.rb
@@ -0,0 +1,35 @@
+module 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]
+
+ configfile = File.join([File.dirname(Puppet.settings[:config]), "hiera.yaml"])
+
+ raise(Puppet::ParseError, "Hiera config file #{configfile} not readable") unless File.exist?(configfile)
+
+ require 'hiera'
+ require 'hiera/scope'
+
+ config = YAML.load_file(configfile)
+ config[:logger] = "puppet"
+
+ hiera = Hiera.new(:config => config)
+
+ 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
diff --git a/lib/puppet/parser/functions/hiera_hash.rb b/lib/puppet/parser/functions/hiera_hash.rb
new file mode 100644
index 0000000..282df11
--- /dev/null
+++ b/lib/puppet/parser/functions/hiera_hash.rb
@@ -0,0 +1,37 @@
+module 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]
+
+ configfile = File.join([File.dirname(Puppet.settings[:config]), "hiera.yaml"])
+
+ raise(Puppet::ParseError, "Hiera config file #{configfile} not readable") unless File.exist?(configfile)
+
+ require 'hiera'
+ require 'hiera/scope'
+
+ config = YAML.load_file(configfile)
+ config[:logger] = "puppet"
+
+ hiera = Hiera.new(:config => config)
+
+ 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
diff --git a/spec/unit/parser/functions/hiera_hash_spec.rb b/spec/unit/parser/functions/hiera_hash_spec.rb
new file mode 100644
index 0000000..368df49
--- /dev/null
+++ b/spec/unit/parser/functions/hiera_hash_spec.rb
@@ -0,0 +1,11 @@
+require 'puppet'
+require 'hiera'
+require 'spec_helper'
+
+describe 'Puppet::Parser::Functions#hiera_hash' do
+ it 'should require a key argument' do
+ Puppet::Parser::Functions.function(:hiera_hash)
+ @scope = Puppet::Parser::Scope.new
+ expect { @scope.function_hiera_hash([]) }.should raise_error(Puppet::ParseError)
+ end
+end
-- You received this message because you are subscribed to the Google Groups "Puppet Developers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to [email protected].
For more options, visit this group at http://groups.google.com/group/puppet-dev?hl=en.
