StackStorm / st2

StackStorm (aka "IFTTT for Ops") is event-driven automation for auto-remediation, incident responses, troubleshooting, deployments, and more for DevOps and SREs. Includes rules engine, workflow, 160 integration packs with 6000+ actions (see https://exchange.stackstorm.org) and ChatOps. Installer at https://docs.stackstorm.com/install/index.html
https://stackstorm.com/
Apache License 2.0
6.07k stars 749 forks source link

Mistral YAQL and Jinja renderers should support to_json_string, to_yaml_string notations. #3241

Closed lakshmi-kannan closed 4 years ago

lakshmi-kannan commented 7 years ago

We support custom filters in st2 land which works well in action chains. We need to port these filters to mistral https://docs.stackstorm.com/reference/jinja.html#applying-filters-with-jinja.

nzlosh commented 7 years ago

+1

May I urge you to implement from_json_string and from_yaml_string. After discussion in slack with @lakshmi-kannan it was explained that from_json_string shouldn't be necessary because stackstorm automatically detects json formatted results.

Despite the detection feature, the use case I'm confronted with is the following:

When fetching a key using the consul pack, the response is a JSON object in the following form:

{
  "LockIndex": 0,
  "ModifyIndex": 11431146,
  "Value": "[u'pkg-01.example.com', u'pkg-02.example.com', u'pkg-03.example.com', u'pkg-04.example.com']",
  "Flags": 0,
  "Key": "automation/pkg/hosts",
  "CreateIndex": 11430978
}

Consul's Value can only be a string data type. The above example was created using the following mistral+YAQL action:

            record_hosts:
                action: consul.put
                input:
                    key: "automation/<% $.role %>/hosts"
                    value: <% str($.hosts) %>
                on-success: record_hosts_in_progress

YAQL casts hosts (type: list) to string so the result conforms with consul.put's value parameter which expects a string data type. Having a to_json_string function would allow YAQL to serialise an object to a string in valid JSON format. Having from_json_string would allow the deserialisation of the same string back into a Python object.

I hope the use case is clear and compelling enough to implement to/from json string functions in YAQL.