Puppet has a function named tree_each() that can be used to flatten and
filter a tree structure of data. Once filtered it is possible to again
create a Hash out of the result.
Documentation here:
https://puppet.com/docs/puppet/5.5/function.html#treeeach
Here are two examples (both from the documentation; the first from
tree_each(), and the second from Hash.new().
The first example shows the flattened filtered value.
To get the pruned hash in that example, do what is done in
Example 2 at the end - i.e. Hash($flattened_pruned_value, 'hash_tree').
It is really difficult to achieve the same with just reduce() and
filter() functions - you would have to more or less implement
the tree_each() function - but you don't have to since puppet has it :-)
Hope this helps you with what you were trying to do.
Also - note that it may be better for you (instead of filtering your
values and then give the resulting structure to create_reources()), to
iterate over the structure and the simply have conditional logic
around the declaration of resources. That is much less magic to read.
Best
- henrik
Encourage you to play with these examples:
#### EXAMPLE 1
# A tree of some complexity (here very simple for readability)
$tree = [
{ name => 'user1', status => 'inactive', id => '10'},
{ name => 'user2', status => 'active', id => '20'}
]
notice $tree.tree_each.filter |$v| {
$value = $v[1]
$value =~ Hash and $value[status] == active
}
#### EXAMPLE 2
####
# A hash tree with 'water' at different locations
$h = { a => { b => { x => 'water'}}, b => { y => 'water'} }
# a helper function that turns water into wine
function make_wine($x) { if $x == 'water' { 'wine' } else { $x } }
# create a flattened tree with water turned into wine
$flat_tree = $h.tree_each.map |$entry| { [$entry[0], make_wine($entry[1])] }
# create a new Hash and log it
notice Hash($flat_tree, 'hash_tree')
>
>
>
> --
> Albert SHIH
> DIO bâtiment 15
> Observatoire de Paris
> xmpp:
j...@obspm.fr
> Heure local/Local time:
> Wed Sep 19 14:50:21 CEST 2018
>
--
Visit my Blog "Puppet on the Edge"
http://puppet-on-the-edge.blogspot.se/