Skip to content

Commit

Permalink
Fix passing params to template or script failed in watcher (#58559)
Browse files Browse the repository at this point in the history
The main changes are:
* Fix custom params are missing when using template or script in watcher's 
  logging action or jira action.
* Add yaml tests to test passing params to template or script successfully.

Relates to #57625
  • Loading branch information
gaobinlong authored Sep 2, 2020
1 parent 5065dbc commit 500e12f
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
---
setup:
- do:
cluster.health:
wait_for_status: yellow

---
teardown:
- do:
watcher.delete_watch:
id: "test_watch"
ignore: 404

---
"Test executing logging action using template with params":
- do:
put_script:
id: log-action
body: >
{
"script":
{
"lang": "mustache",
"source": "{{color}} alert"
}
}
- do:
watcher.put_watch:
id: "test_watch"
body: >
{
"trigger": {
"schedule" : { "interval": "1m" }
},
"input": {
"simple": {
}
},
"condition": {
"always": {}
},
"actions": {
"log" : {
"logging" : {
"text" : {
"id": "log-action",
"params": {
"color": "yellow"
}
}
}
}
}
}
- match: { _id: "test_watch" }
- match: { created: true }

- do:
watcher.execute_watch:
id: "test_watch"

- match: { watch_record.watch_id: "test_watch" }
- match: { watch_record.result.actions.0.id: "log" }
- match: { watch_record.result.actions.0.type: "logging" }
- match: { watch_record.result.actions.0.status: "success" }
- match: { watch_record.result.actions.0.logging.logged_text: "yellow alert" }
Original file line number Diff line number Diff line change
Expand Up @@ -246,3 +246,61 @@
- match: { "watch_record.result.actions.0.type" : "email" }
- match: { "watch_record.result.actions.0.email.message.subject" : "404 recently encountered" }

---
"Test executing logging action using scripts with params":
- do:
cluster.health:
wait_for_status: yellow

- do:
put_script:
id: log-action
body: >
{
"script":
{
"lang": "painless",
"source": "params.color + \" alert\""
}
}
- do:
watcher.put_watch:
id: "test_watch"
body: >
{
"trigger": {
"schedule" : { "interval": "1m" }
},
"input": {
"simple": {
}
},
"condition": {
"always": {}
},
"actions": {
"log" : {
"logging" : {
"text" : {
"id": "log-action",
"params": {
"color": "yellow"
}
}
}
}
}
}
- match: { _id: "test_watch" }
- match: { created: true }

- do:
watcher.execute_watch:
id: "test_watch"

- match: { watch_record.watch_id: "test_watch" }
- match: { watch_record.result.actions.0.id: "log" }
- match: { watch_record.result.actions.0.type: "logging" }
- match: { watch_record.result.actions.0.status: "success" }
- match: { watch_record.result.actions.0.logging.logged_text: "yellow alert" }
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public String render(TextTemplate textTemplate, Map<String, Object> model) {
Script script = new Script(textTemplate.getType(),
textTemplate.getType() == ScriptType.STORED ? null : "mustache", template, options, mergedModel);
TemplateScript.Factory compiledTemplate = service.compile(script, Watcher.SCRIPT_TEMPLATE_CONTEXT);
return compiledTemplate.newInstance(model).execute();
return compiledTemplate.newInstance(mergedModel).execute();
}

private String trimContentType(TextTemplate textTemplate) {
Expand Down

0 comments on commit 500e12f

Please sign in to comment.