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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
|
# Copyright 2021 Northern.tech AS
# This file is part of CFEngine 3 - written and maintained by Northern.tech AS.
# 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, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
# To the extent this program is licensed as part of the Enterprise
# versions of Cfengine, the applicable Commercial Open Source License
# (COSL) may apply to this file if you as a licensee so wish it. See
# included file COSL.txt.
#+begin_src cfengine3
body common control
{
bundlesequence => { "test", "test2", "test3" };
}
bundle agent test
{
vars:
"d1" data => parsejson('{ "a": [1,2,3], "b": [] }');
"d2" data => parsejson('{ "b": [4,5,6] }');
"d3" data => parsejson('[4,5,6]');
"list1" slist => { "element1", "element2" };
"array1[mykey]" slist => { "array_element1", "array_element2" };
"array2[otherkey]" string => "hello";
"merged_d1_d2" data => mergedata("d1", "d2");
"merged_d1_d3" data => mergedata("d1", "d3");
"merged_d3_list1" data => mergedata("d3", "list1");
"merged_d1_array1" data => mergedata("d1", "array1");
"merged_d2_array2" data => mergedata("d2", "array2");
"merged_d1_wrap_array_d2" data => mergedata("d1", "[ d2 ]");
"merged_d1_wrap_map_d2" data => mergedata("d1", '{ "newkey": d2 }');
"merged_d1_d2_str" string => format("merging %S with %S produced %S", d1, d2, merged_d1_d2);
"merged_d1_wrap_array_d2_str" string => format("merging %S with wrapped [ %S ] produced %S", d1, d2, merged_d1_wrap_array_d2);
"merged_d1_wrap_map_d2_str" string => format('merging %S with wrapped { "newkey": %S produced %S', d1, d2, merged_d1_wrap_map_d2);
"merged_d1_d3_str" string => format("merging %S with %S produced %S", d1, d3, merged_d1_d3);
"merged_d3_list1_str" string => format("merging %S with %S produced %S", d3, list1, merged_d3_list1);
"merged_d1_array1_str" string => format("merging %S with %s produced %S", d1, array1, merged_d1_array1);
"merged_d2_array2_str" string => format("merging %S with %s produced %S", d2, array2, merged_d2_array2);
reports:
"$(merged_d1_d2_str)";
"$(merged_d1_wrap_array_d2_str)";
"$(merged_d1_wrap_map_d2_str)";
"$(merged_d1_d3_str)";
"$(merged_d3_list1_str)";
"$(merged_d1_array1_str)";
"$(merged_d2_array2_str)";
}
bundle agent test2
{
vars:
"a" data => parsejson('{ "a": "1" }'), meta => { "mymerge" };
"b" data => parsejson('{ "b": "2" }'), meta => { "mymerge" };
"c" data => parsejson('{ "c": "3" }'), meta => { "mymerge" };
"d" data => parsejson('{ "d": "4" }'), meta => { "mymerge" };
"todo" slist => variablesmatching(".*", "mymerge");
methods:
"go" usebundle => cmerge(@(todo)); # a, b, c, d
reports:
"$(this.bundle): merged containers with cmerge = $(cmerge.all_str)";
}
# note this bundle is in the standard library, in lib/3.6/bundles.cf
bundle agent cmerge(varlist)
{
vars:
"all" data => parsejson('[]'), policy => "free";
"all" data => mergedata(all, $(varlist)), policy => "free";
"all_str" string => format("%S", all), policy => "free";
}
bundle agent test3
{
vars:
"dest_files" slist => { "/tmp/default.json", "/tmp/epel.json" };
"template_file" string => "repository.mustache";
"process_templates" data => mergedata('{ "$(template_file)" : dest_files }');
"process__templates_str" string => format("%S", "process_templates");
reports:
"$(this.bundle) $(process__templates_str)";
"$(this.bundle) $(process_templates[$(template_file)])";
}
#+end_src
###############################################################################
#+begin_src example_output
#@ ```
#@ R: merging {"a":[1,2,3],"b":[]} with {"b":[4,5,6]} produced {"a":[1,2,3],"b":[4,5,6]}
#@ R: merging {"a":[1,2,3],"b":[]} with wrapped [ {"b":[4,5,6]} ] produced {"0":{"b":[4,5,6]},"a":[1,2,3],"b":[]}
#@ R: merging {"a":[1,2,3],"b":[]} with wrapped { "newkey": {"b":[4,5,6]} produced {"a":[1,2,3],"b":[],"newkey":{"b":[4,5,6]}}
#@ R: merging {"a":[1,2,3],"b":[]} with [4,5,6] produced {"0":4,"1":5,"2":6,"a":[1,2,3],"b":[]}
#@ R: merging [4,5,6] with { "element1", "element2" } produced [4,5,6,"element1","element2"]
#@ R: merging {"a":[1,2,3],"b":[]} with array1 produced {"a":[1,2,3],"b":[],"mykey":["array_element1","array_element2"]}
#@ R: merging {"b":[4,5,6]} with array2 produced {"b":[4,5,6],"otherkey":"hello"}
#@ R: test2: merged containers with cmerge = {"a":"1","b":"2","c":"3","d":"4"}
#@ R: test3 {"repository.mustache":["/tmp/default.json","/tmp/epel.json"]}
#@ R: test3 /tmp/default.json
#@ R: test3 /tmp/epel.json
#@ ```
#+end_src
|