-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #10079: Add a generic method to merge two dict variables
- Loading branch information
Showing
2 changed files
with
128 additions
and
0 deletions.
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
tests/acceptance/30_generic_methods/variable_dict_merge.cf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
####################################################### | ||
# | ||
# Create a dict variable by merging two other variables | ||
# | ||
####################################################### | ||
|
||
bundle common acc_path | ||
{ | ||
vars: | ||
"root" string => getenv("NCF_TESTS_ACCEPTANCE", 1024); | ||
} | ||
|
||
body common control | ||
{ | ||
inputs => { "${acc_path.root}/default.cf.sub", "${acc_path.root}/default_ncf.cf.sub", "@{ncf_inputs.default_files}" }; | ||
bundlesequence => { configuration, default("${this.promise_filename}") }; | ||
version => "1.0"; | ||
} | ||
|
||
####################################################### | ||
|
||
bundle agent init | ||
{ | ||
vars: | ||
|
||
"dict1" data => parsejson('{ "key1": "value1", "key2": "value2", "key3": { "keyx": "valuex" }, "key4": [ "valuey" ] }'); | ||
"dict2" data => parsejson('{ "key1": "value1bis", "key4": "value4" }'); | ||
"ref" string => '{ | ||
"key1": "value1bis", | ||
"key2": "value2", | ||
"key3": { | ||
"keyx": "valuex" | ||
}, | ||
"key4": "value4" | ||
}'; | ||
|
||
} | ||
|
||
####################################################### | ||
|
||
bundle agent test | ||
{ | ||
methods: | ||
"ph3" usebundle => variable_dict_merge("prefix", "var3", "init.dict1", "init.dict2"); | ||
} | ||
|
||
####################################################### | ||
|
||
bundle agent check | ||
{ | ||
vars: | ||
"result" string => storejson("prefix.var3"); | ||
|
||
classes: | ||
|
||
"ok_class" expression => "variable_dict_merge_var3_kept.!variable_dict_merge_var3_repaired.!variable_dict_merge_var3_error"; | ||
"ok_value" expression => strcmp("${result}", "${init.ref}"); | ||
|
||
"ok" expression => "ok_class.ok_value"; | ||
|
||
reports: | ||
ok:: | ||
"$(this.promise_filename) Pass"; | ||
!ok:: | ||
"$(this.promise_filename) FAIL"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
##################################################################################### | ||
# Copyright 2017 Normation SAS | ||
##################################################################################### | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, Version 3. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
# | ||
##################################################################################### | ||
|
||
# @name Variable dict merge | ||
# @description Define a variable resulting of the merge of two other variables | ||
# @documentation To use the generated variable, you must use the form `${variable_prefix.variable_name[key]}` with each name replaced with the parameters of this method. | ||
# | ||
# Be careful that using a global variable can lead to unpredictable content in case of multiple definition, which is implicitly the case when a technique has more than one instance (directive). | ||
# Please note that only global variables are available within templates. | ||
# | ||
# @parameter variable_prefix The prefix of the variable name | ||
# @parameter variable_name The variable to define, the full name will be variable_prefix.variable_name | ||
# @parameter first_variable The first variable, that will be overriden if necessary (in the form variable_prefix.variable_name) | ||
# @parameter second_variable The second variable, that will override the first if necessary (in the form variable_prefix.variable_name) | ||
# | ||
# @agent_version >=3.6 | ||
# | ||
# @class_prefix variable_dict_merge | ||
# @class_parameter variable_name | ||
|
||
bundle agent variable_dict_merge(variable_prefix, variable_name, first_variable, second_variable) | ||
{ | ||
vars: | ||
"old_class_prefix" string => canonify("variable_dict_merge_${variable_name}"); | ||
"promisers" slist => { @{this.callers_promisers}, cf_null }, policy => "ifdefined"; | ||
"class_prefix" string => canonify(join("_", "promisers")); | ||
"args" slist => { "${variable_prefix}", "${variable_name}", "${first_variable}", "${second_variable}" }; | ||
|
||
# define the variable within the variable_prefix namespace | ||
"${variable_prefix}.${variable_name}" data => mergedata("${first_variable}", "${second_variable}"); | ||
|
||
classes: | ||
"variable_defined" expression => isvariable("${variable_prefix}.${variable_name}"); | ||
|
||
methods: | ||
!variable_defined:: | ||
"error" usebundle => _classes_failure("${old_class_prefix}"); | ||
"error" usebundle => _classes_failure("${class_prefix}"); | ||
|
||
variable_defined:: | ||
"success" usebundle => _classes_success("${old_class_prefix}"); | ||
"success" usebundle => _classes_success("${class_prefix}"); | ||
|
||
any:: | ||
"report" | ||
usebundle => _log("Set the dict ${variable_prefix}.${variable_name} to the content of the merge of ${first_variable} and ${second_variable}", "${old_class_prefix}", "${class_prefix}", @{args}); | ||
} |