1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
|
# frozen_string_literal: true
# @summary Deeply merge a "defaults" hash into a "resources" hash like the ones expected by `create_resources()`.
#
# Deeply merge a "defaults" hash into a "resources" hash like the ones expected by `create_resources()`.
#
# Internally calls the puppetlabs-stdlib function `deep_merge()`. In case of
# duplicate keys the `resources` hash keys win over the `defaults` hash keys.
#
# Example
# ```puppet
# $defaults_hash = {
# 'one' => '1',
# 'two' => '2',
# 'three' => '3',
# 'four' => {
# 'five' => '5',
# 'six' => '6',
# 'seven' => '7',
# }
# }
#
# $numbers_hash = {
# 'german' => {
# 'one' => 'eins',
# 'three' => 'drei',
# 'four' => {
# 'six' => 'sechs',
# },
# },
# 'french' => {
# 'one' => 'un',
# 'two' => 'deux',
# 'four' => {
# 'five' => 'cinq',
# 'seven' => 'sept',
# },
# }
# }
#
# $result_hash = resources_deep_merge($numbers_hash, $defaults_hash)
# ```
#
# The $result_hash then looks like this:
#
# ```puppet
# $result_hash = {
# 'german' => {
# 'one' => 'eins',
# 'two' => '2',
# 'three' => 'drei',
# 'four' => {
# 'five' => '5',
# 'six' => 'sechs',
# 'seven' => '7',
# }
# },
# 'french' => {
# 'one' => 'un',
# 'two' => 'deux',
# 'three' => '3',
# 'four' => {
# 'five' => 'cinq',
# 'six' => '6',
# 'seven' => 'sept',
# }
# }
# }
# ```
Puppet::Functions.create_function(:'extlib::resources_deep_merge') do
# Deep-merges defaults into a resources hash
# @param resources The hash of resources.
# @param defaults The hash of defaults to merge.
# @return Returns the merged hash.
dispatch :resources_deep_merge do
param 'Hash', :resources
param 'Hash', :defaults
return_type 'Hash'
end
def resources_deep_merge(resources, defaults)
deep_merged_resources = {}
resources.each do |title, params|
deep_merged_resources[title] = call_function('deep_merge', defaults, params)
end
deep_merged_resources
end
end
|