Re: Merge of data containers

"'Nick Anderson' via help-cfengine" <[email protected]>
Newsgroups gmane.comp.sysutils.cfengine.general
Message-ID <[email protected]>
Stefan Skoglund <[email protected]> writes:

Reading the docs for `mapdata()', I was a bit surprised that `mapdata(
"json", '{ "stats": $(this.v) }' , "ifs")' works.

,----
| bundle agent __main__
| {
|   vars:
|       "a"
|         data => parsejson('{ "ifname": "lo"}');
|       "b"
|         data => parsejson('{ "ifname": "enp1"}');
|
|       "ifs"
|         slist => { "a", "b"};
|
|       "a_b"
|         data => mapdata( "json", '{ "status": $(this.v) }' , "ifs");
| }
`----

,----
| Variable name                            Variable value                                               Meta tags                                Comment
| default:main.a                           {"ifname":"lo"}                                              source=promise
| default:main.a_b                         [{"status":{"ifname":"lo"}},{"status":{"ifname":"enp1"}}]    source=promise
| default:main.b                           {"ifname":"enp1"}                                            source=promise
| default:main.ifs                          {"a","b"}                                                   source=promise
`----


The last parameter to `mapdata()' is expected to be an array or data
container:

,----
| bundle agent __main__
| {
|   vars:
|       "a"
|         data => parsejson('{ "ifname": "lo"}');
|       "b"
|         data => parsejson('{ "ifname": "enp1"}');
|
|       "ifs"
|         data => '[ "a", "b" ]';
|
|       "a_b"
|         data => mapdata( "json", '{ "status": $(this.v) }' , "ifs");
| }
`----

,----
| Variable name                            Variable value                                               Meta tags                                Comment
| default:main.a                           {"ifname":"lo"}                                              source=promise
| default:main.a_b                         [{"status":{"ifname":"lo"}},{"status":{"ifname":"enp1"}}]    source=promise
| default:main.b                           {"ifname":"enp1"}                                            source=promise
| default:main.ifs                         ["a","b"]                                                    source=promise
`----


`$(this.v)' should expand to `a' and `b'.

But then I remembered that you can wrap naked variable names. You can
achieve similar result with `mergedata()' explicitly

,----
| bundle agent __main__
| {
|   vars:
|       "a"
|         data => parsejson('{ "ifname": "lo"}');
|       "b"
|         data => parsejson('{ "ifname": "enp1"}');
|
|       "a_b"
|         data => mergedata( '[{ "status": a }]' , '[{ "status": b }]' );
| }
`----

,----
| Variable name                            Variable value                                               Meta tags                                Comment
| default:main.a                           {"ifname":"lo"}                                              source=promise
| default:main.a_b                         [{"status":{"ifname":"lo"}},{"status":{"ifname":"enp1"}}]    source=promise
| default:main.b                           {"ifname":"enp1"}                                            source=promise
`----


      So how to load data from multiple datacontainers into a json
      array in a data container ?

If your trying to merge data containers, remember that objects will
overwrite. If you merge `{ "ifname": "lo"}' and `{ "ifname": "enp1"}'
whichever is merged last will win.

,----
| bundle agent __main__
| {
|   vars:
|       "a"
|         data => parsejson('{ "ifname": "lo"}');
|       "b"
|         data => parsejson('{ "ifname": "enp1"}');
|
|       # Here b will win since it's merged last
|       "a_b"
|         data => mergedata( a, b );
|
|       # Here a will win since it's merged last
|       "b_a"
|         data => mergedata( b, a );
| }
`----

,----
| Variable name                            Variable value                                               Meta tags                                Comment
| default:main.a                           {"ifname":"lo"}                                              source=promise
| default:main.a_b                         {"ifname":"enp1"}                                            source=promise
| default:main.b                           {"ifname":"enp1"}                                            source=promise
| default:main.b_a                         {"ifname":"lo"}                                              source=promise
`----


When you merge arrays, they will merge. This is the behavior you saw
when you used mapdata.

You could potentially use `variablesmatching_as_data()'.

,----
| bundle agent __main__
| {
|   vars:
|       "a"
|         data => parsejson('{ "ifname": "lo"}'),
|         meta => { "tag" };
|       "b"
|         data => parsejson('{ "ifname": "enp1"}'),
|         meta => { "tag" };
|
|       "tagged_with_tag"
|         data => variablesmatching_as_data( ".*", "tag" );
|
|       "tagged_with_tag_i" slist => getindices( a_b );
| }
`----

,----
| Variable name                            Variable value                                               Meta tags                                Comment
| default:main.a                           {"ifname":"lo"}                                              source=promise,tag
| default:main.b                           {"ifname":"enp1"}                                            source=promise,tag
| default:main.tagged_with_tag             {"default:main.a":{"ifname":"lo"},"default:main.b":{"ifname":"enp1"}} source=promise
| default:main.tagged_with_tag_i                                                                        source=promise
`----


You might be better off using a module to do all the json munging and
simply return that back to CFEngine.

-- Nick Anderson | Doer of Things | (+1) 785-550-1767 |
<https://northern.tech>

-- 
You received this message because you are subscribed to the Google Groups "help-cfengine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
To view this discussion on the web visit https://groups.google.com/d/msgid/help-cfengine/878rjtocpy.fsf%40precision-5570.home.cmdln.org.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.