Skip to content

Commit

Permalink
Merge branch 'main' into serverless-role-selector
Browse files Browse the repository at this point in the history
  • Loading branch information
thomheymann authored Nov 14, 2023
2 parents 11eb3c9 + 11b47c4 commit d8b0b01
Show file tree
Hide file tree
Showing 52 changed files with 771 additions and 261 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export const actionSchema = schema.object({
params: schema.recordOf(schema.string(), schema.maybe(schema.any()), { defaultValue: {} }),
frequency: schema.maybe(actionFrequencySchema),
alerts_filter: schema.maybe(actionAlertsFilterSchema),
use_alert_data_for_template: schema.maybe(schema.boolean()),
});

export const createBodySchema = schema.object({
Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/alerting/common/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ export interface RuleAction {
params: RuleActionParams;
frequency?: RuleActionFrequency;
alertsFilter?: AlertsFilter;
useAlertDataForTemplate?: boolean;
}

export interface RuleLastRun {
Expand Down
19 changes: 17 additions & 2 deletions x-pack/plugins/alerting/server/alert/alert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import { v4 as uuidV4 } from 'uuid';
import { AADAlert } from '@kbn/alerts-as-data-utils';
import { get, isEmpty } from 'lodash';
import { MutableAlertInstanceMeta } from '@kbn/alerting-state-types';
import { ALERT_UUID } from '@kbn/rule-data-utils';
Expand Down Expand Up @@ -36,7 +37,7 @@ export type PublicAlert<
Context extends AlertInstanceContext = AlertInstanceContext,
ActionGroupIds extends string = DefaultActionGroupId
> = Pick<
Alert<State, Context, ActionGroupIds>,
Alert<State, Context, ActionGroupIds, AADAlert>,
| 'getContext'
| 'getState'
| 'getUuid'
Expand All @@ -50,13 +51,15 @@ export type PublicAlert<
export class Alert<
State extends AlertInstanceState = AlertInstanceState,
Context extends AlertInstanceContext = AlertInstanceContext,
ActionGroupIds extends string = never
ActionGroupIds extends string = never,
AlertAsData extends AADAlert = AADAlert
> {
private scheduledExecutionOptions?: ScheduledExecutionOptions<State, Context, ActionGroupIds>;
private meta: MutableAlertInstanceMeta;
private state: State;
private context: Context;
private readonly id: string;
private alertAsData: AlertAsData | undefined;

constructor(id: string, { state, meta = {} }: RawAlertInstance = {}) {
this.id = id;
Expand All @@ -78,6 +81,18 @@ export class Alert<
return this.meta.uuid!;
}

isAlertAsData() {
return this.alertAsData !== undefined;
}

setAlertAsData(alertAsData: AlertAsData) {
this.alertAsData = alertAsData;
}

getAlertAsData() {
return this.alertAsData;
}

getStart(): string | null {
return this.state.start ? `${this.state.start}` : null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export const createRuleDataSchema = schema.object({
),
uuid: schema.maybe(schema.string()),
alertsFilter: schema.maybe(actionAlertsFilterSchema),
useAlertDataForTemplate: schema.maybe(schema.boolean()),
}),
{ defaultValue: [] }
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export const actionDomainSchema = schema.object({
params: actionParamsSchema,
frequency: schema.maybe(actionFrequencySchema),
alertsFilter: schema.maybe(actionDomainAlertsFilterSchema),
useAlertDataAsTemplate: schema.maybe(schema.boolean()),
});

/**
Expand All @@ -89,4 +90,5 @@ export const actionSchema = schema.object({
params: actionParamsSchema,
frequency: schema.maybe(actionFrequencySchema),
alertsFilter: schema.maybe(actionAlertsFilterSchema),
useAlertDataForTemplate: schema.maybe(schema.boolean()),
});
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export const actionsSchema = schema.arrayOf(
),
})
),
use_alert_data_for_template: schema.maybe(schema.boolean()),
}),
{ defaultValue: [] }
);
59 changes: 35 additions & 24 deletions x-pack/plugins/alerting/server/routes/lib/rewrite_actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,28 @@ export const rewriteActionsReq = (
): NormalizedAlertAction[] => {
if (!actions) return [];

return actions.map(({ frequency, alerts_filter: alertsFilter, ...action }) => {
return {
...action,
...(frequency
? {
frequency: {
...omit(frequency, 'notify_when'),
notifyWhen: frequency.notify_when,
},
}
: {}),
...(alertsFilter ? { alertsFilter } : {}),
};
});
return actions.map(
({
frequency,
alerts_filter: alertsFilter,
use_alert_data_for_template: useAlertDataForTemplate,
...action
}) => {
return {
...action,
useAlertDataForTemplate,
...(frequency
? {
frequency: {
...omit(frequency, 'notify_when'),
notifyWhen: frequency.notify_when,
},
}
: {}),
...(alertsFilter ? { alertsFilter } : {}),
};
}
);
};

export const rewriteActionsRes = (actions?: RuleAction[]) => {
Expand All @@ -37,14 +45,17 @@ export const rewriteActionsRes = (actions?: RuleAction[]) => {
notify_when: notifyWhen,
});
if (!actions) return [];
return actions.map(({ actionTypeId, frequency, alertsFilter, ...action }) => ({
...action,
connector_type_id: actionTypeId,
...(frequency ? { frequency: rewriteFrequency(frequency) } : {}),
...(alertsFilter
? {
alerts_filter: alertsFilter,
}
: {}),
}));
return actions.map(
({ actionTypeId, frequency, alertsFilter, useAlertDataForTemplate, ...action }) => ({
...action,
connector_type_id: actionTypeId,
use_alert_data_for_template: useAlertDataForTemplate,
...(frequency ? { frequency: rewriteFrequency(frequency) } : {}),
...(alertsFilter
? {
alerts_filter: alertsFilter,
}
: {}),
})
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ describe('bulkEditRulesRoute', () => {
foo: true,
},
uuid: '123-456',
use_alert_data_for_template: false,
},
],
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ describe('createRuleRoute', () => {
},
connector_type_id: 'test',
uuid: '123-456',
use_alert_data_for_template: false,
},
],
};
Expand Down Expand Up @@ -198,6 +199,7 @@ describe('createRuleRoute', () => {
"params": Object {
"foo": true,
},
"useAlertDataForTemplate": undefined,
},
],
"alertTypeId": "1",
Expand Down Expand Up @@ -314,6 +316,7 @@ describe('createRuleRoute', () => {
"params": Object {
"foo": true,
},
"useAlertDataForTemplate": undefined,
},
],
"alertTypeId": "1",
Expand Down Expand Up @@ -431,6 +434,7 @@ describe('createRuleRoute', () => {
"params": Object {
"foo": true,
},
"useAlertDataForTemplate": undefined,
},
],
"alertTypeId": "1",
Expand Down Expand Up @@ -548,6 +552,7 @@ describe('createRuleRoute', () => {
"params": Object {
"foo": true,
},
"useAlertDataForTemplate": undefined,
},
],
"alertTypeId": "1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,33 @@ import type { RuleParams } from '../../../../../../application/rule/types';
const transformCreateBodyActions = (actions: CreateRuleActionV1[]): CreateRuleData['actions'] => {
if (!actions) return [];

return actions.map(({ frequency, alerts_filter: alertsFilter, ...action }) => {
return {
group: action.group,
id: action.id,
params: action.params,
actionTypeId: action.actionTypeId,
...(action.uuid ? { uuid: action.uuid } : {}),
...(frequency
? {
frequency: {
summary: frequency.summary,
throttle: frequency.throttle,
notifyWhen: frequency.notify_when,
},
}
: {}),
...(alertsFilter ? { alertsFilter } : {}),
};
});
return actions.map(
({
frequency,
alerts_filter: alertsFilter,
use_alert_data_for_template: useAlertDataForTemplate,
...action
}) => {
return {
group: action.group,
id: action.id,
params: action.params,
actionTypeId: action.actionTypeId,
useAlertDataForTemplate,
...(action.uuid ? { uuid: action.uuid } : {}),
...(frequency
? {
frequency: {
summary: frequency.summary,
throttle: frequency.throttle,
notifyWhen: frequency.notify_when,
},
}
: {}),
...(alertsFilter ? { alertsFilter } : {}),
};
}
);
};

export const transformCreateBody = <Params extends RuleParams = never>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ describe('resolveRuleRoute', () => {
foo: true,
},
uuid: '123-456',
useAlertDataForTemplate: false,
},
],
consumer: 'bar',
Expand Down Expand Up @@ -101,6 +102,7 @@ describe('resolveRuleRoute', () => {
params: mockedRule.actions[0].params,
connector_type_id: mockedRule.actions[0].actionTypeId,
uuid: mockedRule.actions[0].uuid,
use_alert_data_for_template: mockedRule.actions[0].useAlertDataForTemplate,
},
],
outcome: 'aliasMatch',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,21 @@ export const transformRuleToRuleResponse = <Params extends RuleParams = never>(
consumer: rule.consumer,
schedule: rule.schedule,
actions: rule.actions.map(
({ group, id, actionTypeId, params, frequency, uuid, alertsFilter }) => ({
({
group,
id,
actionTypeId,
params,
frequency,
uuid,
alertsFilter,
useAlertDataForTemplate,
}) => ({
group,
id,
params,
connector_type_id: actionTypeId,
use_alert_data_for_template: useAlertDataForTemplate ?? false,
...(frequency
? {
frequency: {
Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/alerting/server/routes/update_rule.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ describe('updateRuleRoute', () => {
"params": Object {
"baz": true,
},
"useAlertDataForTemplate": undefined,
"uuid": "1234-5678",
},
],
Expand Down
Loading

0 comments on commit d8b0b01

Please sign in to comment.