From 7855b812209ed494413034bb2a41aeba0347dc97 Mon Sep 17 00:00:00 2001 From: Julian Gernun <17549662+jcger@users.noreply.github.com> Date: Wed, 9 Oct 2024 13:01:56 +0200 Subject: [PATCH] add application types and schemas --- .../common/routes/rule/apis/mute_all/index.ts | 8 +++--- .../rule/methods/mute_all/index.ts | 9 +++++++ .../rule/methods/mute_all/mute_all.ts | 27 ++++++++++++++----- .../rule/methods/mute_all/schemas/index.ts | 8 ++++++ .../mute_all/schemas/mute_all_rule_schemas.ts | 12 +++++++++ .../rule/methods/mute_all/types/index.ts | 8 ++++++ .../mute_all/types/mute_all_rule_types.ts | 11 ++++++++ .../server/rules_client/rules_client.ts | 2 +- 8 files changed, 73 insertions(+), 12 deletions(-) create mode 100644 x-pack/plugins/alerting/server/application/rule/methods/mute_all/index.ts create mode 100644 x-pack/plugins/alerting/server/application/rule/methods/mute_all/schemas/index.ts create mode 100644 x-pack/plugins/alerting/server/application/rule/methods/mute_all/schemas/mute_all_rule_schemas.ts create mode 100644 x-pack/plugins/alerting/server/application/rule/methods/mute_all/types/index.ts create mode 100644 x-pack/plugins/alerting/server/application/rule/methods/mute_all/types/mute_all_rule_types.ts diff --git a/x-pack/plugins/alerting/common/routes/rule/apis/mute_all/index.ts b/x-pack/plugins/alerting/common/routes/rule/apis/mute_all/index.ts index c6fee1fa688915..ba1dd568aeeb2d 100644 --- a/x-pack/plugins/alerting/common/routes/rule/apis/mute_all/index.ts +++ b/x-pack/plugins/alerting/common/routes/rule/apis/mute_all/index.ts @@ -5,8 +5,8 @@ * 2.0. */ -export { enableRuleRequestParamsSchema } from './schemas/latest'; -export type { EnableRuleRequestParams } from './types/latest'; +export { muteAllRuleRequestParamsSchema } from './schemas/latest'; +export type { MuteAllRuleRequestParams } from './types/latest'; -export { enableRuleRequestParamsSchema as enableRuleRequestParamsSchemaV1 } from './schemas/v1'; -export type { EnableRuleRequestParams as EnableRuleRequestParamsV1 } from './types/v1'; +export { muteAllRuleRequestParamsSchema as muteAllRuleRequestParamsSchemaV1 } from './schemas/v1'; +export type { MuteAllRuleRequestParams as MuteAllRuleRequestParamsV1 } from './types/v1'; diff --git a/x-pack/plugins/alerting/server/application/rule/methods/mute_all/index.ts b/x-pack/plugins/alerting/server/application/rule/methods/mute_all/index.ts new file mode 100644 index 00000000000000..c8b85c149314e7 --- /dev/null +++ b/x-pack/plugins/alerting/server/application/rule/methods/mute_all/index.ts @@ -0,0 +1,9 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +export type { MuteAllRuleParams } from './types'; +export { muteAll } from './mute_all'; diff --git a/x-pack/plugins/alerting/server/application/rule/methods/mute_all/mute_all.ts b/x-pack/plugins/alerting/server/application/rule/methods/mute_all/mute_all.ts index b4f1696fb75aa7..65f45ea142342f 100644 --- a/x-pack/plugins/alerting/server/application/rule/methods/mute_all/mute_all.ts +++ b/x-pack/plugins/alerting/server/application/rule/methods/mute_all/mute_all.ts @@ -5,17 +5,23 @@ * 2.0. */ +import Boom from '@hapi/boom'; import { RawRule } from '../../../../types'; import { WriteOperations, AlertingAuthorizationEntity } from '../../../../authorization'; import { retryIfConflicts } from '../../../../lib/retry_if_conflicts'; import { partiallyUpdateRule, RULE_SAVED_OBJECT_TYPE } from '../../../../saved_objects'; -import { ruleAuditEvent, RuleAuditAction } from '../../../common/audit_events'; -import { RulesClientContext } from '../types'; -import { updateMetaAttributes } from '../lib'; -import { clearUnscheduledSnoozeAttributes } from '../common'; -import { RuleAttributes } from '../../data/rule/types'; +import { ruleAuditEvent, RuleAuditAction } from '../../../../rules_client/common/audit_events'; +import { RulesClientContext } from '../../../../rules_client/types'; +import { updateMetaAttributes } from '../../../../rules_client/lib'; +import { clearUnscheduledSnoozeAttributes } from '../../../../rules_client/common'; +import { RuleAttributes } from '../../../../data/rule/types'; +import { MuteAllRuleParams } from './types'; +import { muteAllRuleParamsSchema } from './schemas'; -export async function muteAll(context: RulesClientContext, { id }: { id: string }): Promise { +export async function muteAll( + context: RulesClientContext, + { id }: MuteAllRuleParams +): Promise { return await retryIfConflicts( context.logger, `rulesClient.muteAll('${id}')`, @@ -23,7 +29,14 @@ export async function muteAll(context: RulesClientContext, { id }: { id: string ); } -async function muteAllWithOCC(context: RulesClientContext, { id }: { id: string }) { +async function muteAllWithOCC(context: RulesClientContext, params: MuteAllRuleParams) { + try { + muteAllRuleParamsSchema.validate(params); + } catch (error) { + throw Boom.badRequest(`Error validating enable rule parameters - ${error.message}`); + } + + const { id } = params; const { attributes, version } = await context.unsecuredSavedObjectsClient.get( RULE_SAVED_OBJECT_TYPE, id diff --git a/x-pack/plugins/alerting/server/application/rule/methods/mute_all/schemas/index.ts b/x-pack/plugins/alerting/server/application/rule/methods/mute_all/schemas/index.ts new file mode 100644 index 00000000000000..b6c6729ac50293 --- /dev/null +++ b/x-pack/plugins/alerting/server/application/rule/methods/mute_all/schemas/index.ts @@ -0,0 +1,8 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +export * from './mute_all_rule_schemas'; diff --git a/x-pack/plugins/alerting/server/application/rule/methods/mute_all/schemas/mute_all_rule_schemas.ts b/x-pack/plugins/alerting/server/application/rule/methods/mute_all/schemas/mute_all_rule_schemas.ts new file mode 100644 index 00000000000000..0d0ae33394e72a --- /dev/null +++ b/x-pack/plugins/alerting/server/application/rule/methods/mute_all/schemas/mute_all_rule_schemas.ts @@ -0,0 +1,12 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import { schema } from '@kbn/config-schema'; + +export const muteAllRuleParamsSchema = schema.object({ + id: schema.string(), +}); diff --git a/x-pack/plugins/alerting/server/application/rule/methods/mute_all/types/index.ts b/x-pack/plugins/alerting/server/application/rule/methods/mute_all/types/index.ts new file mode 100644 index 00000000000000..c2d2f7401b3505 --- /dev/null +++ b/x-pack/plugins/alerting/server/application/rule/methods/mute_all/types/index.ts @@ -0,0 +1,8 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +export * from './mute_all_rule_types'; diff --git a/x-pack/plugins/alerting/server/application/rule/methods/mute_all/types/mute_all_rule_types.ts b/x-pack/plugins/alerting/server/application/rule/methods/mute_all/types/mute_all_rule_types.ts new file mode 100644 index 00000000000000..4f4ad36dbc23ab --- /dev/null +++ b/x-pack/plugins/alerting/server/application/rule/methods/mute_all/types/mute_all_rule_types.ts @@ -0,0 +1,11 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import type { TypeOf } from '@kbn/config-schema'; +import { muteAllRuleParamsSchema } from '../schemas'; + +export type MuteAllRuleParams = TypeOf; diff --git a/x-pack/plugins/alerting/server/rules_client/rules_client.ts b/x-pack/plugins/alerting/server/rules_client/rules_client.ts index 80f9b82733a9df..163df75cc0e6b9 100644 --- a/x-pack/plugins/alerting/server/rules_client/rules_client.ts +++ b/x-pack/plugins/alerting/server/rules_client/rules_client.ts @@ -58,7 +58,7 @@ import { enableRule } from '../application/rule/methods/enable_rule/enable_rule' import { updateRuleApiKey } from '../application/rule/methods/update_api_key/update_rule_api_key'; import { disableRule } from '../application/rule/methods/disable/disable_rule'; import { muteInstance } from '../application/rule/methods/mute_alert/mute_instance'; -import { muteAll } from './methods/mute_all'; +import { muteAll } from '../application/rule/methods/mute_all'; import { unmuteAll } from './methods/unmute_all'; import { unmuteInstance } from '../application/rule/methods/unmute_alert/unmute_instance'; import { runSoon } from './methods/run_soon';