Uploaded image for project: 'CFEngine Community'
  1. CFEngine Community
  2. CFE-2839

Define custom functions in policy

    XMLWordPrintable

    Details

    • Type: Task
    • Status: Open
    • Priority: Low
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: cf-agent
    • Labels:
      None

      Description

      This is just an idea I had, no plans to implement currently.

      One of the hardest things to do in CFEngine is data manipulation, especially for people new to the language. I often find that it's really hard if the policy language doesn't have the specific function I need. If I could easily implement the function myself that would be great. I think the easiest way to achieve this is to do the evaluation in another language, like python.

      Suggestion:

      function agent string_replace(inp, find, replace)
      {
          language => "python3";
          type => "string";
          impl => "return inp.replace(find, replace)";
      }
      
      bundle agent __main__
      {
          vars:
              "inp" string => "Hello, world";
              "out" string => string_replace("$(inp)", "world", "universe");
          reports:
              "Input: $(inp)";
              "Output: $(out)";
      }
      

      (Of course, a downside of this would be that these functions would be very slow compared to builtin functions)

      Functions should be pure, deterministic and have no side effects. If they are written with this in mind, we can at least cache results and skip evaluation.

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                olehermanse Ole Herman Schumacher Elgesem
                Reporter:
                olehermanse Ole Herman Schumacher Elgesem
              • Votes:
                0 Vote for this issue
                Watchers:
                3 Start watching this issue

                Dates

                • Created:
                  Updated:

                  Summary Panel