From 544c19af333c6bee42c16c45bd75e7cc30e4e5a9 Mon Sep 17 00:00:00 2001 From: Ramesh Sencha Date: Mon, 29 May 2023 22:02:36 +0530 Subject: [PATCH 1/2] (CONT-1023) - Enhancing deferrable_epp to support nested hash --- functions/deferrable_epp.pp | 2 +- lib/puppet/functions/hash_values.rb | 26 ++++++++++++++++++++++++++ spec/functions/hash_values_spec.rb | 14 ++++++++++++++ 3 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 lib/puppet/functions/hash_values.rb create mode 100644 spec/functions/hash_values_spec.rb diff --git a/functions/deferrable_epp.pp b/functions/deferrable_epp.pp index b94b6510f..1a05e132f 100644 --- a/functions/deferrable_epp.pp +++ b/functions/deferrable_epp.pp @@ -6,7 +6,7 @@ # have to explicitly pass the entire scope to the client. # function stdlib::deferrable_epp(String $template, Hash $variables) >> Variant[String, Sensitive[String], Deferred] { - if $variables.any |$key, $value| { $value.is_a(Deferred) } { + if $variables.hash_values.any |$value| { $value.is_a(Deferred) } { Deferred( 'inline_epp', [find_template($template).file, $variables], diff --git a/lib/puppet/functions/hash_values.rb b/lib/puppet/functions/hash_values.rb new file mode 100644 index 000000000..6f18a79dc --- /dev/null +++ b/lib/puppet/functions/hash_values.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true + +# This function will return list of Hash values, the return value will be Array +# NOTE : This function is expecting only Hash and return value will be Array +# +# @example : +# $hash = { +# "key1" => "value1", +# "key2" => { "key2.1" => "value2.1"} +# } +# $hash.hash_value +# +# Output : ["value1", "value2.1"] +# +Puppet::Functions.create_function(:hash_values) do + dispatch :hash_values do + param 'Hash', :options + return_type 'Array' + end + + def hash_values(options) + options.each_with_object([]) do |(_k, v), values| + v.is_a?(Hash) ? values.concat(hash_values(v)) : (values << v) + end + end +end diff --git a/spec/functions/hash_values_spec.rb b/spec/functions/hash_values_spec.rb new file mode 100644 index 000000000..d45c371df --- /dev/null +++ b/spec/functions/hash_values_spec.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe 'hash_values' do + # please note that these tests are examples only + # you will need to replace the params and return value + # with your expectations + it { is_expected.to run.with_params({}).and_return([]) } + it { is_expected.to run.with_params({ 'key' => 'value' }).and_return(['value']) } + it { is_expected.to run.with_params({ 'key' => { 'key1' => 'value1', 'key2' => 'value2' } }).and_return(['value1', 'value2']) } + it { is_expected.to run.with_params(2).and_raise_error(StandardError) } + it { is_expected.to run.with_params(nil).and_raise_error(StandardError) } +end From 40bcc4ac03c5948712c1ede006d98a5931577423 Mon Sep 17 00:00:00 2001 From: Ramesh Sencha Date: Tue, 30 May 2023 23:11:06 +0530 Subject: [PATCH 2/2] Addressing review comments --- functions/deferrable_epp.pp | 2 +- .../functions/{hash_values.rb => nested_values.rb} | 10 +++++----- .../{hash_values_spec.rb => nested_values_spec.rb} | 3 ++- 3 files changed, 8 insertions(+), 7 deletions(-) rename lib/puppet/functions/{hash_values.rb => nested_values.rb} (68%) rename spec/functions/{hash_values_spec.rb => nested_values_spec.rb} (73%) diff --git a/functions/deferrable_epp.pp b/functions/deferrable_epp.pp index 1a05e132f..b46bb3614 100644 --- a/functions/deferrable_epp.pp +++ b/functions/deferrable_epp.pp @@ -6,7 +6,7 @@ # have to explicitly pass the entire scope to the client. # function stdlib::deferrable_epp(String $template, Hash $variables) >> Variant[String, Sensitive[String], Deferred] { - if $variables.hash_values.any |$value| { $value.is_a(Deferred) } { + if $variables.nested_values.any |$value| { $value.is_a(Deferred) } { Deferred( 'inline_epp', [find_template($template).file, $variables], diff --git a/lib/puppet/functions/hash_values.rb b/lib/puppet/functions/nested_values.rb similarity index 68% rename from lib/puppet/functions/hash_values.rb rename to lib/puppet/functions/nested_values.rb index 6f18a79dc..5fecf7eca 100644 --- a/lib/puppet/functions/hash_values.rb +++ b/lib/puppet/functions/nested_values.rb @@ -8,19 +8,19 @@ # "key1" => "value1", # "key2" => { "key2.1" => "value2.1"} # } -# $hash.hash_value +# $hash.nested_values # # Output : ["value1", "value2.1"] # -Puppet::Functions.create_function(:hash_values) do - dispatch :hash_values do +Puppet::Functions.create_function(:nested_values) do + dispatch :nested_values do param 'Hash', :options return_type 'Array' end - def hash_values(options) + def nested_values(options) options.each_with_object([]) do |(_k, v), values| - v.is_a?(Hash) ? values.concat(hash_values(v)) : (values << v) + v.is_a?(Hash) ? values.concat(nested_values(v)) : (values << v) end end end diff --git a/spec/functions/hash_values_spec.rb b/spec/functions/nested_values_spec.rb similarity index 73% rename from spec/functions/hash_values_spec.rb rename to spec/functions/nested_values_spec.rb index d45c371df..2fd163b91 100644 --- a/spec/functions/hash_values_spec.rb +++ b/spec/functions/nested_values_spec.rb @@ -2,13 +2,14 @@ require 'spec_helper' -describe 'hash_values' do +describe 'nested_values' do # please note that these tests are examples only # you will need to replace the params and return value # with your expectations it { is_expected.to run.with_params({}).and_return([]) } it { is_expected.to run.with_params({ 'key' => 'value' }).and_return(['value']) } it { is_expected.to run.with_params({ 'key' => { 'key1' => 'value1', 'key2' => 'value2' } }).and_return(['value1', 'value2']) } + it { is_expected.to run.with_params({ 'key1' => 'value1', 'key2' => { 'key1' => 'value21', 'key2' => 'value22' }, 'key3' => 'value3' }).and_return(['value1', 'value21', 'value22', 'value3']) } it { is_expected.to run.with_params(2).and_raise_error(StandardError) } it { is_expected.to run.with_params(nil).and_raise_error(StandardError) } end