Prototype: classfilterdata(data_container, data_structure, key_or_index)

Return type: data

Description: This function filters a data container (data_container) based on defined classes. Records within the data container containing a class expression located at a given key or index (key_or_index) are filtered out when they evaluate to false in the current context. The interpretation of the data container depends on the specified data structure (data_structure).

If the data_structure argument is specified to be:

  • "array_of_arrays", the data_container argument is interpreted as an array of arrays, and the key_or_index argument is interpreted as an index within the children arrays.
  • "array_of_objects", the data_container argument is interpreted as an array of objects, and the key_or_index argument is interpreted as a key within the children objects.
  • "auto", the interpretation is automatically detected based on the data structure.

Arguments:

  • data_container: string - CFEngine variable identifier or inline JSON - in the range: .*
  • data_structure: - Specify type of data structure - one of
    • array_of_arrays
    • array_of_objects
    • auto
  • key_or_index: string - Key or index of class expressions - in the range: .*

Example (with array of arrays):

Policy:

bundle agent __main__
{
  classes:
      "role_2";

  vars:
      "original"
        data => '[
            [ "role_1",  "alice",  32 ],
            [ "!role_1", "bob",    24 ],
            [ "role_2",  "malcom", 27 ]
          ]';

      "filtered"
        data => classfilterdata("original", "array_of_arrays", "0");

  reports:
      "Filtered data: $(with)"
        with => storejson("filtered");
}

Output:

R: Filtered data: [
  [
    "!role_1",
    "bob",
    24
  ],
  [
    "role_2",
    "malcom",
    27
  ]
]

Example (with array of objects):

Policy:

bundle agent __main__
{
  classes:
      "role_2";

  vars:
      "original"
        data => '[
            { "file": "/tmp/foo", "ifvarclass": "role_1"          },
            { "file": "/tmp/bar", "ifvarclass": "role_2"          },
            { "file": "/tmp/baz", "ifvarclass": "(role_1|role_2)" }
          ]';

      "filtered"
        data => classfilterdata("original", "array_of_objects", "ifvarclass");

  reports:
      "Filtered data: $(with)"
        with => storejson("filtered");
}

Output:

R: Filtered data: [
  {
    "file": "/tmp/bar",
    "ifvarclass": "role_2"
  },
  {
    "file": "/tmp/baz",
    "ifvarclass": "(role_1|role_2)"
  }
]

Notes:

This function can accept many types of data parameters (See collecting function ).

See also: [classfiltercsv() ](/reference/functions/classfiltercsv “classfiltercsv”), [data_expand() ](/reference/functions/data_expand “data_expand”), [classmatch() ](/reference/functions/classmatch “classmatch”)

History:

  • Introduced in CFEngine 3.27.0