Skip to content

Commit

Permalink
move mute alert to own folder
Browse files Browse the repository at this point in the history
  • Loading branch information
jcger committed Sep 7, 2023
1 parent 7473672 commit 39cd734
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
*/

import { Boom } from '@hapi/boom';
import { muteOptionsSchema } from '../../schemas/mute_options';
import { RuleMuteAlertOptions } from '../../types';
import { muteAlertParamsSchema } from './schemas';
import type { MuteAlertParams } from './types';
import { Rule } from '../../../../types';
import { WriteOperations, AlertingAuthorizationEntity } from '../../../../authorization';
import { retryIfConflicts } from '../../../../lib/retry_if_conflicts';
Expand All @@ -17,24 +17,24 @@ import { updateMeta } from '../../../../rules_client/lib';

export async function muteInstance(
context: RulesClientContext,
options: RuleMuteAlertOptions
params: MuteAlertParams
): Promise<void> {
try {
muteOptionsSchema.validate(options);
muteAlertParamsSchema.validate(params);
} catch (error) {
throw Boom.badRequest(`Failed to validate params: ${error.message}`);
}

return await retryIfConflicts(
context.logger,
`rulesClient.muteInstance('${options.alertId}')`,
async () => await muteInstanceWithOCC(context, options)
`rulesClient.muteInstance('${params.alertId}')`,
async () => await muteInstanceWithOCC(context, params)
);
}

async function muteInstanceWithOCC(
context: RulesClientContext,
{ alertId, alertInstanceId }: RuleMuteAlertOptions
{ alertId, alertInstanceId }: MuteAlertParams
) {
const { attributes, version } = await context.unsecuredSavedObjectsClient.get<Rule>(
'alert',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/*
* 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 { muteAlertParamsSchema } from './mute_alert_params_schema';
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/
import { schema } from '@kbn/config-schema';

export const muteOptionsSchema = schema.object({
export const muteAlertParamsSchema = schema.object({
alertId: schema.string(),
alertInstanceId: schema.string(),
});
Original file line number Diff line number Diff line change
@@ -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 type { MuteAlertParams } from './mute_alert_params';
Original file line number Diff line number Diff line change
@@ -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 { TypeOf } from '@kbn/config-schema';
import { muteAlertParamsSchema } from '../schemas';

export type MuteAlertParams = TypeOf<typeof muteAlertParamsSchema>;
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ export type {
RuleParams,
RuleNotifyWhen,
RuleSnoozeSchedule,
RuleMuteAlertOptions,
RuleMuteAlertParams,
} from './rule';
2 changes: 0 additions & 2 deletions x-pack/plugins/alerting/server/application/rule/types/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import {
actionSchema,
ruleSchema,
ruleDomainSchema,
muteOptionsSchema,
} from '../schemas';

export type RuleNotifyWhen = typeof ruleNotifyWhen[keyof typeof ruleNotifyWhen];
Expand All @@ -42,7 +41,6 @@ export type Monitoring = TypeOf<typeof monitoringSchema>;
export type Action = TypeOf<typeof actionSchema>;
type RuleSchemaType = TypeOf<typeof ruleSchema>;
type RuleDomainSchemaType = TypeOf<typeof ruleDomainSchema>;
export type RuleMuteAlertOptions = TypeOf<typeof muteOptionsSchema>;

type RuleExecutionStatusWithDateString = TypeOf<typeof ruleExecutionStatusSchema>;
export interface RuleExecutionStatus {
Expand Down

0 comments on commit 39cd734

Please sign in to comment.