Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
merging containers to get a single list
body common control
{
bundlesequence => {
"main",
};
}
bundle agent main
{
methods:
"any" usebundle => test1;
"any" usebundle => test2;
"any" usebundle => test3;
}
bundle agent test1
{
vars:
"ports"
data => parsejson( '{ "tcp" : [ "100", "101", "102" ] }' ),
meta => { "firewall" };
}
bundle agent test2
{
vars:
"ports"
data => parsejson( '{ "tcp" : [ "200", "201", "202" ] }' ),
meta => { "firewall" };
}
bundle agent test3
{
vars:
"port_lists"
comment => "Get list of firewall port lists",
slist => variablesmatching( ".*ports", "firewall" );
"port_lists_tcp"
slist => maplist("$(this)[tcp]", @(port_lists));
methods:
"any" usebundle => cmerge( "all_fw_ports", @{port_lists_tcp} );
reports:
"tcp ports => ${cmerge.all_fw_ports}";
"string dump => ${cmerge.all_fw_ports_str}";
}
bundle agent cmerge( name, varlist )
{
vars:
"$(name)" data => parsejson('[]'), policy => "free";
"$(name)" data => mergedata($(name), $(varlist)), policy => "free"; # iterates!
"$(name)_str" string => format("%S", $(name)), policy => "free";
}
$ cf-agent -Kf ./ggmpd.cf
R: tcp ports => 100
R: tcp ports => 101
R: tcp ports => 102
R: tcp ports => 200
R: tcp ports => 201
R: tcp ports => 202
R: string dump => ["100","101","102","200","201","202"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment