diff options
author | Brice Figureau <brice-puppet@daysofwonder.com> | 2009-11-10 16:43:37 +0100 |
---|---|---|
committer | test branch <puppet-dev@googlegroups.com> | 2010-02-17 06:50:53 -0800 |
commit | 75c32f910ea124a938a7035b3352c11a11b57d0c (patch) | |
tree | 744d30ad3c4234f57a92c417484a3b33685af99b /test | |
parent | 9122ac51284086a050d61df8fe060616aaf83d3c (diff) | |
download | puppet-75c32f910ea124a938a7035b3352c11a11b57d0c.tar.gz puppet-75c32f910ea124a938a7035b3352c11a11b57d0c.tar.xz puppet-75c32f910ea124a938a7035b3352c11a11b57d0c.zip |
Fix #2389 - Enhance Puppet DSL with Hashes
This bring a new container syntax to the Puppet DSL: hashes.
Hashes are defined like Ruby Hash:
{ key1 => val1, ... }
Hash keys are strings, but hash values can be any possible right
values admitted in Puppet DSL (ie function call, variables access...)
Currently it is possible:
1) to assign hashes to variable
$myhash = { key1 => "myval", key2 => $b }
2) to access hash members (recursively) from a variable containing
a hash (works for array too):
$myhash = { key => { subkey => "b" }}
notice($myhash[key][subjey]]
3) to use hash member access as resource title
4) to use hash in default definition parameter or resource parameter if
the type supports it (known for the moment).
It is not possible to string interpolate an hash access. If it proves
to be an issue it can be added or work-arounded with a string concatenation
operator easily.
It is not possible to use an hash as a resource title. This might be
possible once we support compound resource title.
Unlike the proposed syntax in the ticket it is not possible to assign
individual hash member (mostly to respect write once nature of variable
in puppet).
Signed-off-by: Brice Figureau <brice-puppet@daysofwonder.com>
Diffstat (limited to 'test')
-rw-r--r-- | test/data/snippets/hash.pp | 33 | ||||
-rwxr-xr-x | test/language/snippets.rb | 7 |
2 files changed, 40 insertions, 0 deletions
diff --git a/test/data/snippets/hash.pp b/test/data/snippets/hash.pp new file mode 100644 index 000000000..d33249872 --- /dev/null +++ b/test/data/snippets/hash.pp @@ -0,0 +1,33 @@ + +$hash = { "file" => "/tmp/myhashfile1" } + +file { + $hash["file"]: + ensure => file, content => "content"; +} + +$hash2 = { "a" => { key => "/tmp/myhashfile2" }} + +file { + $hash2["a"][key]: + ensure => file, content => "content"; +} + +define test($a = { "b" => "c" }) { + file { + $a["b"]: + ensure => file, content => "content" + } +} + +test { + "test": + a => { "b" => "/tmp/myhashfile3" } +} + +$hash3 = { mykey => "/tmp/myhashfile4" } +$key = "mykey" + +file { + $hash3[$key]: ensure => file, content => "content" +} diff --git a/test/language/snippets.rb b/test/language/snippets.rb index 5c7805c0a..bfd0e539c 100755 --- a/test/language/snippets.rb +++ b/test/language/snippets.rb @@ -486,6 +486,13 @@ class TestSnippets < Test::Unit::TestCase assert_file("/tmp/testiftest","if test"); end + def snippet_hash + assert_file("/tmp/myhashfile1","hash test 1"); + assert_file("/tmp/myhashfile2","hash test 2"); + assert_file("/tmp/myhashfile3","hash test 3"); + assert_file("/tmp/myhashfile4","hash test 4"); + end + # Iterate across each of the snippets and create a test. Dir.entries(snippetdir).sort.each { |file| next if file =~ /^\./ |