Skip to content

Commit

Permalink
[alerts] adds action group and date to mustache template variables fo…
Browse files Browse the repository at this point in the history
…r actions (#83195)

resolves: #67389

Adds new variables to the existing set of variables that can be used in mustache templates to be used in action parameters when creating alerts.

- `alertActionGroup` - the action group associated with the alert scheduling actions
- `date` - the current date, in ISO format
  • Loading branch information
pmuellr authored Nov 19, 2020
1 parent 5211dfe commit f83e06f
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ export function createExecutionHandler({
spaceId,
tags,
alertInstanceId,
alertActionGroup: actionGroup,
context,
actionParams: action.params,
state,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ test('skips non string parameters', () => {
tags: ['tag-A', 'tag-B'],
spaceId: 'spaceId-A',
alertInstanceId: '2',
alertActionGroup: 'action-group',
alertParams: {
foo: 'test',
},
Expand Down Expand Up @@ -54,6 +55,7 @@ test('missing parameters get emptied out', () => {
tags: ['tag-A', 'tag-B'],
spaceId: 'spaceId-A',
alertInstanceId: '2',
alertActionGroup: 'action-group',
alertParams: {},
});
expect(result).toMatchInlineSnapshot(`
Expand All @@ -77,6 +79,7 @@ test('context parameters are passed to templates', () => {
tags: ['tag-A', 'tag-B'],
spaceId: 'spaceId-A',
alertInstanceId: '2',
alertActionGroup: 'action-group',
alertParams: {},
});
expect(result).toMatchInlineSnapshot(`
Expand All @@ -99,6 +102,7 @@ test('state parameters are passed to templates', () => {
tags: ['tag-A', 'tag-B'],
spaceId: 'spaceId-A',
alertInstanceId: '2',
alertActionGroup: 'action-group',
alertParams: {},
});
expect(result).toMatchInlineSnapshot(`
Expand All @@ -121,6 +125,7 @@ test('alertId is passed to templates', () => {
tags: ['tag-A', 'tag-B'],
spaceId: 'spaceId-A',
alertInstanceId: '2',
alertActionGroup: 'action-group',
alertParams: {},
});
expect(result).toMatchInlineSnapshot(`
Expand All @@ -143,6 +148,7 @@ test('alertName is passed to templates', () => {
tags: ['tag-A', 'tag-B'],
spaceId: 'spaceId-A',
alertInstanceId: '2',
alertActionGroup: 'action-group',
alertParams: {},
});
expect(result).toMatchInlineSnapshot(`
Expand All @@ -165,6 +171,7 @@ test('tags is passed to templates', () => {
tags: ['tag-A', 'tag-B'],
spaceId: 'spaceId-A',
alertInstanceId: '2',
alertActionGroup: 'action-group',
alertParams: {},
});
expect(result).toMatchInlineSnapshot(`
Expand All @@ -186,6 +193,7 @@ test('undefined tags is passed to templates', () => {
alertName: 'alert-name',
spaceId: 'spaceId-A',
alertInstanceId: '2',
alertActionGroup: 'action-group',
alertParams: {},
});
expect(result).toMatchInlineSnapshot(`
Expand All @@ -208,6 +216,7 @@ test('empty tags is passed to templates', () => {
tags: [],
spaceId: 'spaceId-A',
alertInstanceId: '2',
alertActionGroup: 'action-group',
alertParams: {},
});
expect(result).toMatchInlineSnapshot(`
Expand All @@ -230,6 +239,7 @@ test('spaceId is passed to templates', () => {
tags: ['tag-A', 'tag-B'],
spaceId: 'spaceId-A',
alertInstanceId: '2',
alertActionGroup: 'action-group',
alertParams: {},
});
expect(result).toMatchInlineSnapshot(`
Expand All @@ -252,6 +262,7 @@ test('alertInstanceId is passed to templates', () => {
tags: ['tag-A', 'tag-B'],
spaceId: 'spaceId-A',
alertInstanceId: '2',
alertActionGroup: 'action-group',
alertParams: {},
});
expect(result).toMatchInlineSnapshot(`
Expand All @@ -261,6 +272,53 @@ test('alertInstanceId is passed to templates', () => {
`);
});

test('alertActionGroup is passed to templates', () => {
const actionParams = {
message: 'Value "{{alertActionGroup}}" exists',
};
const result = transformActionParams({
actionParams,
state: {},
context: {},
alertId: '1',
alertName: 'alert-name',
tags: ['tag-A', 'tag-B'],
spaceId: 'spaceId-A',
alertInstanceId: '2',
alertActionGroup: 'action-group',
alertParams: {},
});
expect(result).toMatchInlineSnapshot(`
Object {
"message": "Value \\"action-group\\" exists",
}
`);
});

test('date is passed to templates', () => {
const actionParams = {
message: '{{date}}',
};
const dateBefore = Date.now();
const result = transformActionParams({
actionParams,
state: {},
context: {},
alertId: '1',
alertName: 'alert-name',
tags: ['tag-A', 'tag-B'],
spaceId: 'spaceId-A',
alertInstanceId: '2',
alertActionGroup: 'action-group',
alertParams: {},
});
const dateAfter = Date.now();
const dateVariable = new Date(`${result.message}`).valueOf();

expect(dateVariable).toBeGreaterThanOrEqual(dateBefore);
expect(dateVariable).toBeLessThanOrEqual(dateAfter);
});

test('works recursively', () => {
const actionParams = {
body: {
Expand All @@ -276,6 +334,7 @@ test('works recursively', () => {
tags: ['tag-A', 'tag-B'],
spaceId: 'spaceId-A',
alertInstanceId: '2',
alertActionGroup: 'action-group',
alertParams: {},
});
expect(result).toMatchInlineSnapshot(`
Expand All @@ -302,6 +361,7 @@ test('works recursively with arrays', () => {
tags: ['tag-A', 'tag-B'],
spaceId: 'spaceId-A',
alertInstanceId: '2',
alertActionGroup: 'action-group',
alertParams: {},
});
expect(result).toMatchInlineSnapshot(`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ interface TransformActionParamsOptions {
spaceId: string;
tags?: string[];
alertInstanceId: string;
alertActionGroup: string;
actionParams: AlertActionParams;
alertParams: AlertTypeParams;
state: AlertInstanceState;
Expand All @@ -31,6 +32,7 @@ export function transformActionParams({
spaceId,
tags,
alertInstanceId,
alertActionGroup,
context,
actionParams,
state,
Expand All @@ -48,7 +50,9 @@ export function transformActionParams({
spaceId,
tags,
alertInstanceId,
alertActionGroup,
context,
date: new Date().toISOString(),
state,
params: alertParams,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,18 @@ describe('transformActionVariables', () => {
"description": "The tags of the alert.",
"name": "tags",
},
Object {
"description": "The date the alert scheduled the action.",
"name": "date",
},
Object {
"description": "The alert instance id that scheduled actions for the alert.",
"name": "alertInstanceId",
},
Object {
"description": "The alert action group that was used to scheduled actions for the alert.",
"name": "alertActionGroup",
},
]
`);
});
Expand Down Expand Up @@ -66,10 +74,18 @@ describe('transformActionVariables', () => {
"description": "The tags of the alert.",
"name": "tags",
},
Object {
"description": "The date the alert scheduled the action.",
"name": "date",
},
Object {
"description": "The alert instance id that scheduled actions for the alert.",
"name": "alertInstanceId",
},
Object {
"description": "The alert action group that was used to scheduled actions for the alert.",
"name": "alertActionGroup",
},
Object {
"description": "foo-description",
"name": "context.foo",
Expand Down Expand Up @@ -109,10 +125,18 @@ describe('transformActionVariables', () => {
"description": "The tags of the alert.",
"name": "tags",
},
Object {
"description": "The date the alert scheduled the action.",
"name": "date",
},
Object {
"description": "The alert instance id that scheduled actions for the alert.",
"name": "alertInstanceId",
},
Object {
"description": "The alert action group that was used to scheduled actions for the alert.",
"name": "alertActionGroup",
},
Object {
"description": "foo-description",
"name": "state.foo",
Expand Down Expand Up @@ -155,10 +179,18 @@ describe('transformActionVariables', () => {
"description": "The tags of the alert.",
"name": "tags",
},
Object {
"description": "The date the alert scheduled the action.",
"name": "date",
},
Object {
"description": "The alert instance id that scheduled actions for the alert.",
"name": "alertInstanceId",
},
Object {
"description": "The alert action group that was used to scheduled actions for the alert.",
"name": "alertActionGroup",
},
Object {
"description": "fooC-description",
"name": "context.fooC",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,26 @@ function getAlwaysProvidedActionVariables(): ActionVariable[] {
}),
});

result.push({
name: 'date',
description: i18n.translate('xpack.triggersActionsUI.actionVariables.dateLabel', {
defaultMessage: 'The date the alert scheduled the action.',
}),
});

result.push({
name: 'alertInstanceId',
description: i18n.translate('xpack.triggersActionsUI.actionVariables.alertInstanceIdLabel', {
defaultMessage: 'The alert instance id that scheduled actions for the alert.',
}),
});

result.push({
name: 'alertActionGroup',
description: i18n.translate('xpack.triggersActionsUI.actionVariables.alertActionGroupLabel', {
defaultMessage: 'The alert action group that was used to scheduled actions for the alert.',
}),
});

return result;
}
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ alertName: {{alertName}},
spaceId: {{spaceId}},
tags: {{tags}},
alertInstanceId: {{alertInstanceId}},
alertActionGroup: {{alertActionGroup}},
instanceContextValue: {{context.instanceContextValue}},
instanceStateValue: {{state.instanceStateValue}}
`.trim();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ alertName: abc,
spaceId: ${space.id},
tags: tag-A,tag-B,
alertInstanceId: 1,
alertActionGroup: default,
instanceContextValue: true,
instanceStateValue: true
`.trim(),
Expand Down Expand Up @@ -282,6 +283,7 @@ alertName: abc,
spaceId: ${space.id},
tags: tag-A,tag-B,
alertInstanceId: 1,
alertActionGroup: default,
instanceContextValue: true,
instanceStateValue: true
`.trim(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ alertName: abc,
spaceId: ${space.id},
tags: tag-A,tag-B,
alertInstanceId: 1,
alertActionGroup: default,
instanceContextValue: true,
instanceStateValue: true
`.trim(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,16 @@ export default ({ getPageObjects, getService }: FtrProviderContext) => {
await testSubjects.setValue('messageTextArea', 'test message ');
await testSubjects.click('messageAddVariableButton');
await testSubjects.click('variableMenuButton-0');
expect(await messageTextArea.getAttribute('value')).to.eql('test message {{alertId}}');
expect(await messageTextArea.getAttribute('value')).to.eql(
'test message {{alertActionGroup}}'
);
await messageTextArea.type(' some additional text ');

await testSubjects.click('messageAddVariableButton');
await testSubjects.click('variableMenuButton-1');

expect(await messageTextArea.getAttribute('value')).to.eql(
'test message {{alertId}} some additional text {{alertInstanceId}}'
'test message {{alertActionGroup}} some additional text {{alertId}}'
);

await testSubjects.click('saveAlertButton');
Expand Down

0 comments on commit f83e06f

Please sign in to comment.