Webhook action in watcher to use environment variables

Hi,

This is the webhook action I am using:

"actions" : {
"web-post" : {
"webhook": {
"method" : "POST",
"host": "x.x.x.x",
"port": 8090,
"path": ":/notification",
"headers": {
"Content-Type": "application/json"
},
"body": "{{#toJson}}ctx.payload.hits{{/toJson}}"
}
}
}

While the same code needs to be used in different environments, how can I make the host and port parameterized and use environment variables. something like " host => [${HOST}] " in logstash.

Thanks
Vinod

If you want to re-use the same code in many differents watch you have to use stored sripts fields

    POST _scripts/dat_super_script
        {
          "script": {
            "lang": "painless",
            "code": """def my_data = ["DATA1","DATA2","DATA3"]; ctx.vars.my_data=my_data;
     --- do whatever you want there---
        """
            }
        }

This script will create an array and add this array to the execution context and you can use it as condition validator or transform or just logging action , to just add data to the execution context

  "email_administrator": {
  "condition": {
    "script": {
      "stored": "dat_super_script"
    }
  },

you can access to this data by this variable : ctx.vars.my_data
for example :

"actions": {
    "web-post": {
        "webhook": {
            "method": "POST",
            "host": {{ctx.vars.my_data.0}},
            "port": 8090,
            "path": ":/notification",
            "headers": {
                "Content-Type": "application/json"
            },
            "body": "{{#toJson}}ctx.payload.hits{{/toJson}}"
        }
    }
}
1 Like

Thank you Guillaume, Let me try your suggestion.

Thanks
VInod

1 Like

Hey,

another possibility is to reuse the same watches (same input, same action ,same condition), but change the metadata field and access those. This way you could have scripts creating the watches on your deployment mechanism.

--Alex

2 Likes

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.