OK, below is a working example (the JSON is inline, otherwise same idea
as yours, with longer bundle and variable names).
With the latest master, arrays seem to be cleared on bundle entry, so
you can't just use the "ary" array (this also seems to break the
standard library's `cmerge()` utility bundle). It works in 3.6.2 though,
so I think it's due to the resolution of
https://dev.cfengine.com/issues/6369
Instead you have to use `variablesmatching()` which is not as clean.
Perhaps that issue will be addressed before the next release. I updated
https://dev.cfengine.com/issues/6369 with the example and my notes.
Ted
#+begin_src cfengine3
body common control
{
bundlesequence => { "legacy" };
inputs => { "$(sys.libdir)/
stdlib.cf" };
}
bundle agent legacy
{
vars:
"groups" data => parsejson('{
"groupa": {
"gid": "1000",
"members": [ "usera", "userb" ]
},
"groupb": {
"gid": "1001",
"members": [ "userc", "userd" ]
}
}');
"unsorted_keys" slist => getindices("groups");
"keys" slist => sort(unsorted_keys, "lex");
methods:
"addgroup"
usebundle => add_legacy_group(@(groups), $(keys));
"report"
usebundle => report_legacy_groups();
}
bundle agent add_legacy_group(dg, group)
{
vars:
"members" slist => getvalues("dg[$(group)][members]");
"gid" string => "$(dg[$(group)][gid])";
"strmembers" string => join(",", members);
"created_$(group)"
meta => { "groupcreation" },
string => format("%s:x:%s:%s", $(group), $(gid), $(strmembers));
"ary[$(group)]" string => "$(created_$(group))";
reports:
"$(this.bundle): collecting data for $(group): members $(strmembers), GID $(gid)";
"$(this.bundle): collected data for $(group): $(created_$(group))";
}
bundle agent report_legacy_groups()
{
vars:
"created" slist => variablesmatching(".*", "groupcreation");
"collected[$(created)]" string => "$($(created))";
"todo" data => mergedata(collected);
"todo_s" string => format("%S", todo);
"ary_import" data => mergedata("add_legacy_group.ary");
"ary_import_s" string => format("%S", ary_import);
reports:
"$(this.bundle): $(created) = $(collected[$(created)])";
"$(this.bundle): as JSON: $(todo_s)";
"$(this.bundle): broken ary import as JSON: $(ary_import_s)";
}
#+end_src
Output with latest master:
#+begin_src text
R: add_legacy_group: collecting data for groupa: members usera,userb, GID 1000
R: add_legacy_group: collected data for groupa: groupa:x:1000:usera,userb
R: add_legacy_group: collecting data for groupb: members userc,userd, GID 1001
R: add_legacy_group: collected data for groupb: groupb:x:1001:userc,userd
R: report_legacy_groups: default:add_legacy_group.created_groupb = groupb:x:1001:userc,userd
R: report_legacy_groups: default:add_legacy_group.created_groupa = groupa:x:1000:usera,userb
R: report_legacy_groups: as JSON: {"default:add_legacy_group.created_groupa":"groupa:x:1000:usera,userb","default:add_legacy_group.created_groupb":"groupb:x:1001:userc,userd"}
R: report_legacy_groups: broken ary import as JSON: {"groupb":"groupb:x:1001:userc,userd"}
#+end_src