Add lookup_hiera_hash function
authorGiulio Fidente <gfidente@redhat.com>
Mon, 23 May 2016 19:09:21 +0000 (21:09 +0200)
committerGiulio Fidente <gfidente@redhat.com>
Wed, 25 May 2016 21:55:44 +0000 (23:55 +0200)
The lookup_hiera_hash function is meant to lookup for the value
of a given key from a given Hiera hash. In the manifests this is
possible by saving the value of the hash in a variable first but
when driving lookups from the Heat templates we can't do it.

Change-Id: Ie31bb70314db44a0a18e86090cc74aa4df5de169

lib/puppet/parser/functions/lookup_hiera_hash.rb [new file with mode: 0644]
spec/fixtures/hiera.yaml [new file with mode: 0644]
spec/fixtures/hieradata/default.yaml [new file with mode: 0644]
spec/functions/lookup_hiera_hash_spec.rb [new file with mode: 0644]
spec/spec_helper.rb

diff --git a/lib/puppet/parser/functions/lookup_hiera_hash.rb b/lib/puppet/parser/functions/lookup_hiera_hash.rb
new file mode 100644 (file)
index 0000000..d96d65f
--- /dev/null
@@ -0,0 +1,22 @@
+module Puppet::Parser::Functions
+  newfunction(:lookup_hiera_hash, :arity => 2, :type => :rvalue,
+              :doc => "Lookup a key->value from a Hiera hash") do |args|
+    hash_name = args[0]
+    key_name = args[1]
+    unless hash_name.is_a?(String) and key_name.is_a?(String)
+      raise Puppet::ParseError, "The hash name and the key name must be given as strings."
+    end
+    if defined? call_function
+      hash = call_function('hiera', [hash_name])
+    else
+      hash = function_hiera([hash_name])
+    end
+    unless hash.is_a?(Hash)
+      raise Puppet::ParseError, "The value Hiera returned for #{hash_name} is not a Hash."
+    end
+    unless hash.key?(key_name)
+      raise Puppet::ParseError, "The Hiera hash #{hash_name} does not contain key #{key_name}."
+    end
+    return hash[key_name]
+  end
+end
diff --git a/spec/fixtures/hiera.yaml b/spec/fixtures/hiera.yaml
new file mode 100644 (file)
index 0000000..1dc3360
--- /dev/null
@@ -0,0 +1,7 @@
+---
+:backends:
+  - yaml
+:yaml:
+  :datadir: './spec/fixtures/hieradata'
+:hierarchy:
+  - default
diff --git a/spec/fixtures/hieradata/default.yaml b/spec/fixtures/hieradata/default.yaml
new file mode 100644 (file)
index 0000000..0d0c944
--- /dev/null
@@ -0,0 +1,3 @@
+my_hash:
+  network: '127.0.0.1'
+not_hash: string
diff --git a/spec/functions/lookup_hiera_hash_spec.rb b/spec/functions/lookup_hiera_hash_spec.rb
new file mode 100644 (file)
index 0000000..ffaf8b5
--- /dev/null
@@ -0,0 +1,20 @@
+require 'spec_helper'
+require 'puppet'
+
+# puppet 4.0 call_function() has no visibility of 3.x functions and will fail anyway
+unless Puppet.version =~ /^4\.0/
+  describe 'lookup_hiera_hash' do
+    # working version
+    it { should run.with_params('my_hash', 'network').and_return('127.0.0.1') }
+    # raise if key does not exist
+    it { should run.with_params('my_hash', 'not_network').and_raise_error(Puppet::ParseError) }
+    # raise if hash value returned by hiera is not a hash
+    it { should run.with_params('not_hash', 'key').and_raise_error(Puppet::ParseError) }
+    # raise if arguments are not two
+    it { should run.with_params('hash', 'key', 'unexpected').and_raise_error(ArgumentError) }
+    it { should run.with_params('hash').and_raise_error(ArgumentError) }
+    # raise if arguments are not strings
+    it { should run.with_params({}, 'key').and_raise_error(Puppet::ParseError) }
+    it { should run.with_params('hash', true).and_raise_error(Puppet::ParseError) }
+  end
+end
index 15d5eab..251160e 100644 (file)
@@ -6,6 +6,7 @@ fixture_path = File.expand_path(File.join(__FILE__, '..', 'fixtures'))
 RSpec.configure do |c|
   c.alias_it_should_behave_like_to :it_configures, 'configures'
   c.alias_it_should_behave_like_to :it_raises, 'raises'
+  c.hiera_config = File.join(fixture_path, 'hiera.yaml')
   c.module_path = File.join(fixture_path, 'modules')
   c.manifest_dir = File.join(fixture_path, 'manifests')