Skip to content

Commit

Permalink
[System Actions] Complete API audit and update APIs to comply with Sy…
Browse files Browse the repository at this point in the history
…stem Actions (#172937)

## Summary

Closes #172168

This updates the legacy `rewriteActionsReq` and `rewriteRule` transforms
to be compatible with System Actions, and replaces all uses of legacy
`rewriteActionsRes` with `transformRuleActions`, which is already System
Actions-compliant.

Affected APIs are:

- `_clone`
- `_find`
- `_get`
- `_update`
  • Loading branch information
Zacqary committed Jan 3, 2024
1 parent f8a1a73 commit 21f0a78
Show file tree
Hide file tree
Showing 10 changed files with 156 additions and 190 deletions.
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 @@ -141,6 +141,7 @@ export interface RuleSystemAction {
actionTypeId: string;
params: RuleActionParams;
type: typeof RuleActionTypes.SYSTEM;
useAlertDataForTemplate?: boolean;
}

export type RuleAction = RuleDefaultAction | RuleSystemAction;
Expand Down
15 changes: 5 additions & 10 deletions x-pack/plugins/alerting/server/routes/clone_rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,21 @@
import { schema } from '@kbn/config-schema';
import { IRouter } from '@kbn/core/server';
import { ILicenseState, RuleTypeDisabledError } from '../lib';
import {
verifyAccessAndContext,
RewriteResponseCase,
handleDisabledApiKeysError,
rewriteRuleLastRun,
rewriteActionsRes,
} from './lib';
import { verifyAccessAndContext, handleDisabledApiKeysError, rewriteRuleLastRun } from './lib';
import {
RuleTypeParams,
AlertingRequestHandlerContext,
INTERNAL_BASE_ALERTING_API_PATH,
PartialRule,
} from '../types';
import { transformRuleActions } from './rule/transforms';

const paramSchema = schema.object({
id: schema.string(),
newId: schema.maybe(schema.string()),
});

const rewriteBodyRes: RewriteResponseCase<PartialRule<RuleTypeParams>> = ({
const rewriteBodyRes = ({
actions,
alertTypeId,
scheduledTaskId,
Expand All @@ -46,7 +41,7 @@ const rewriteBodyRes: RewriteResponseCase<PartialRule<RuleTypeParams>> = ({
lastRun,
nextRun,
...rest
}) => ({
}: PartialRule<RuleTypeParams>) => ({
...rest,
api_key_owner: apiKeyOwner,
created_by: createdBy,
Expand All @@ -71,7 +66,7 @@ const rewriteBodyRes: RewriteResponseCase<PartialRule<RuleTypeParams>> = ({
: {}),
...(actions
? {
actions: rewriteActionsRes(actions),
actions: transformRuleActions(actions),
}
: {}),
...(lastRun ? { last_run: rewriteRuleLastRun(lastRun) } : {}),
Expand Down
13 changes: 2 additions & 11 deletions x-pack/plugins/alerting/server/routes/find_rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,7 @@ import { UsageCounter } from '@kbn/usage-collection-plugin/server';
import { schema } from '@kbn/config-schema';
import { ILicenseState } from '../lib';
import { FindOptions, FindResult } from '../rules_client';
import {
RewriteRequestCase,
RewriteResponseCase,
verifyAccessAndContext,
rewriteRule,
} from './lib';
import { RewriteRequestCase, verifyAccessAndContext, rewriteRule } from './lib';
import {
RuleTypeParams,
AlertingRequestHandlerContext,
Expand Down Expand Up @@ -69,11 +64,7 @@ const rewriteQueryReq: RewriteRequestCase<FindOptions> = ({
...(hasReference ? { hasReference } : {}),
...(searchFields ? { searchFields } : {}),
});
const rewriteBodyRes: RewriteResponseCase<FindResult<RuleTypeParams>> = ({
perPage,
data,
...restOfResult
}) => {
const rewriteBodyRes = ({ perPage, data, ...restOfResult }: FindResult<RuleTypeParams>) => {
return {
...restOfResult,
per_page: perPage,
Expand Down
14 changes: 5 additions & 9 deletions x-pack/plugins/alerting/server/routes/get_rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,21 @@ import { omit } from 'lodash';
import { schema } from '@kbn/config-schema';
import { IRouter } from '@kbn/core/server';
import { ILicenseState } from '../lib';
import {
verifyAccessAndContext,
RewriteResponseCase,
rewriteRuleLastRun,
rewriteActionsRes,
} from './lib';
import { verifyAccessAndContext, rewriteRuleLastRun } from './lib';
import {
RuleTypeParams,
AlertingRequestHandlerContext,
BASE_ALERTING_API_PATH,
INTERNAL_BASE_ALERTING_API_PATH,
SanitizedRule,
} from '../types';
import { transformRuleActions } from './rule/transforms';

const paramSchema = schema.object({
id: schema.string(),
});

const rewriteBodyRes: RewriteResponseCase<SanitizedRule<RuleTypeParams>> = ({
const rewriteBodyRes = ({
alertTypeId,
createdBy,
updatedBy,
Expand All @@ -47,7 +43,7 @@ const rewriteBodyRes: RewriteResponseCase<SanitizedRule<RuleTypeParams>> = ({
nextRun,
viewInAppRelativeUrl,
...rest
}) => ({
}: SanitizedRule<RuleTypeParams>) => ({
...rest,
rule_type_id: alertTypeId,
created_by: createdBy,
Expand All @@ -66,7 +62,7 @@ const rewriteBodyRes: RewriteResponseCase<SanitizedRule<RuleTypeParams>> = ({
last_execution_date: executionStatus.lastExecutionDate,
last_duration: executionStatus.lastDuration,
},
actions: rewriteActionsRes(actions),
actions: transformRuleActions(actions),
...(lastRun ? { last_run: rewriteRuleLastRun(lastRun) } : {}),
...(nextRun ? { next_run: nextRun } : {}),
...(viewInAppRelativeUrl ? { view_in_app_relative_url: viewInAppRelativeUrl } : {}),
Expand Down
6 changes: 1 addition & 5 deletions x-pack/plugins/alerting/server/routes/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@ export type {
} from './rewrite_request_case';
export { verifyAccessAndContext } from './verify_access_and_context';
export { countUsageOfPredefinedIds } from './count_usage_of_predefined_ids';
export {
rewriteActionsReq,
rewriteActionsRes,
rewriteActionsReqWithSystemActions,
} from './rewrite_actions';
export { rewriteActionsReq } from './rewrite_actions';
export { actionsSchema } from './actions_schema';
export { rewriteRule, rewriteRuleLastRun } from './rewrite_rule';
export { rewriteNamespaces } from './rewrite_namespaces';
Expand Down
124 changes: 63 additions & 61 deletions x-pack/plugins/alerting/server/routes/lib/rewrite_actions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,25 @@
* 2.0.
*/

import { rewriteActionsReq, rewriteActionsRes } from './rewrite_actions';
import { RuleActionTypes } from '../../../common';
import { rewriteActionsReq } from './rewrite_actions';

describe('rewrite Actions', () => {
describe('rewriteActionsRes', () => {
it('rewrites the actions response correctly', () => {
expect(
rewriteActionsRes([
describe('rewriteActionsReq', () => {
it('should rewrite actions correctly', () => {
expect(
rewriteActionsReq(
[
{
uuid: '111',
group: 'default',
id: '1',
actionTypeId: '2',
params: { foo: 'bar' },
frequency: {
summary: true,
notifyWhen: 'onThrottleInterval',
notify_when: 'onThrottleInterval',
throttle: '1h',
},
alertsFilter: {
alerts_filter: {
query: {
kql: 'test:1s',
dsl: '{test:1}',
Expand All @@ -39,60 +39,12 @@ describe('rewrite Actions', () => {
},
},
},
])
).toEqual([
{
alerts_filter: {
query: { dsl: '{test:1}', kql: 'test:1s', filters: [] },
timeframe: {
days: [1, 2, 3],
hours: { end: '15:00', start: '00:00' },
timezone: 'UTC',
},
},
connector_type_id: '2',
frequency: { notify_when: 'onThrottleInterval', summary: true, throttle: '1h' },
group: 'default',
id: '1',
params: { foo: 'bar' },
uuid: '111',
},
]);
});
});

describe('rewriteActionsReq', () => {
expect(
rewriteActionsReq([
{
uuid: '111',
group: 'default',
id: '1',
params: { foo: 'bar' },
frequency: {
summary: true,
notify_when: 'onThrottleInterval',
throttle: '1h',
},
alerts_filter: {
query: {
kql: 'test:1s',
dsl: '{test:1}',
filters: [],
},
timeframe: {
days: [1, 2, 3],
timezone: 'UTC',
hours: {
start: '00:00',
end: '15:00',
},
},
},
},
])
],
() => false
)
).toEqual([
{
type: RuleActionTypes.DEFAULT,
uuid: '111',
group: 'default',
id: '1',
Expand Down Expand Up @@ -120,4 +72,54 @@ describe('rewrite Actions', () => {
},
]);
});
it('should rewrite system actions correctly', () => {
expect(
rewriteActionsReq(
[
{
uuid: '111',
group: 'default',
id: '1',
params: { foo: 'bar' },
frequency: {
summary: true,
notify_when: 'onThrottleInterval',
throttle: '1h',
},
},
{
uuid: '111',
group: 'default',
id: 'system-1',
params: { foo: 'bar' },
frequency: {
summary: true,
notify_when: 'onThrottleInterval',
throttle: '1h',
},
},
],
(id) => id.startsWith('system')
)
).toEqual([
{
type: RuleActionTypes.DEFAULT,
uuid: '111',
group: 'default',
id: '1',
params: { foo: 'bar' },
frequency: {
summary: true,
notifyWhen: 'onThrottleInterval',
throttle: '1h',
},
},
{
type: RuleActionTypes.SYSTEM,
uuid: '111',
id: 'system-1',
params: { foo: 'bar' },
},
]);
});
});
Loading

0 comments on commit 21f0a78

Please sign in to comment.