-
Notifications
You must be signed in to change notification settings - Fork 8.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Response Ops][Rules] Version Mute All Rule API #195572
Changes from all commits
2faf873
e6ca996
ee4d610
8420c02
7855b81
185f37d
2d92357
0a6abc9
2ea66f3
eebfd6f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. | ||
*/ | ||
|
||
export { muteAllRuleRequestParamsSchema } from './schemas/latest'; | ||
export type { MuteAllRuleRequestParams } from './types/latest'; | ||
|
||
export { muteAllRuleRequestParamsSchema as muteAllRuleRequestParamsSchemaV1 } from './schemas/v1'; | ||
export type { MuteAllRuleRequestParams as MuteAllRuleRequestParamsV1 } from './types/v1'; |
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 * from './v1'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/* | ||
* 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 muteAllRuleRequestParamsSchema = schema.object({ | ||
id: schema.string({ | ||
meta: { | ||
description: 'The identifier for the rule.', | ||
}, | ||
}), | ||
}); |
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 * from './v1'; |
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 type { TypeOf } from '@kbn/config-schema'; | ||
import { muteAllRuleRequestParamsSchemaV1 } from '..'; | ||
|
||
export type MuteAllRuleRequestParams = TypeOf<typeof muteAllRuleRequestParamsSchemaV1>; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* 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 { RulesClientContext } from '../../../../rules_client'; | ||
import { muteAll } from './mute_all'; | ||
import { savedObjectsRepositoryMock } from '@kbn/core-saved-objects-api-server-mocks'; | ||
|
||
jest.mock('../../../../lib/retry_if_conflicts', () => ({ | ||
retryIfConflicts: (_: unknown, id: unknown, asyncFn: () => Promise<unknown>) => { | ||
return asyncFn(); | ||
}, | ||
})); | ||
|
||
jest.mock('../../../../rules_client/lib', () => ({ | ||
updateMetaAttributes: () => {}, | ||
})); | ||
|
||
jest.mock('../../../../saved_objects', () => ({ | ||
partiallyUpdateRule: async () => {}, | ||
})); | ||
|
||
const loggerErrorMock = jest.fn(); | ||
const getBulkMock = jest.fn(); | ||
|
||
const savedObjectsMock = savedObjectsRepositoryMock.create(); | ||
savedObjectsMock.get = jest.fn().mockReturnValue({ | ||
attributes: { | ||
actions: [], | ||
}, | ||
version: '9.0.0', | ||
}); | ||
|
||
const context = { | ||
logger: { error: loggerErrorMock }, | ||
getActionsClient: () => { | ||
return { | ||
getBulk: getBulkMock, | ||
}; | ||
}, | ||
unsecuredSavedObjectsClient: savedObjectsMock, | ||
authorization: { ensureAuthorized: async () => {} }, | ||
ruleTypeRegistry: { | ||
ensureRuleTypeEnabled: () => {}, | ||
}, | ||
getUserName: async () => {}, | ||
} as unknown as RulesClientContext; | ||
|
||
describe('validateMuteAllParams', () => { | ||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should not throw an error for valid params', () => { | ||
const validParams = { | ||
id: 'ble', | ||
}; | ||
|
||
expect(() => muteAll(context, validParams)).not.toThrow(); | ||
expect(savedObjectsMock.get).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should throw Boom.badRequest for invalid params', async () => { | ||
const invalidParams = { | ||
id: 22 as unknown as string, // type workaround to send wrong data validation | ||
}; | ||
|
||
await expect(muteAll(context, invalidParams)).rejects.toThrowErrorMatchingInlineSnapshot( | ||
`"Error validating mute all parameters - [id]: expected value of type [string] but got [number]"` | ||
); | ||
}); | ||
}); |
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 * from './mute_all_rule_schemas'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(), | ||
}); |
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 * from './mute_all_rule_types'; |
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 type { TypeOf } from '@kbn/config-schema'; | ||
import { muteAllRuleParamsSchema } from '../schemas'; | ||
|
||
export type MuteAllRuleParams = TypeOf<typeof muteAllRuleParamsSchema>; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,20 +6,15 @@ | |
*/ | ||
|
||
import { IRouter } from '@kbn/core/server'; | ||
import { schema } from '@kbn/config-schema'; | ||
import { UsageCounter } from '@kbn/usage-collection-plugin/server'; | ||
import { ILicenseState, RuleTypeDisabledError } from '../lib'; | ||
import { verifyAccessAndContext } from './lib'; | ||
import { AlertingRequestHandlerContext, BASE_ALERTING_API_PATH } from '../types'; | ||
import { trackDeprecatedRouteUsage } from '../lib/track_deprecated_route_usage'; | ||
|
||
const paramSchema = schema.object({ | ||
id: schema.string({ | ||
meta: { | ||
description: 'The identifier for the rule.', | ||
}, | ||
}), | ||
}); | ||
import { ILicenseState, RuleTypeDisabledError } from '../../../../lib'; | ||
import { verifyAccessAndContext } from '../../../lib'; | ||
import { AlertingRequestHandlerContext, BASE_ALERTING_API_PATH } from '../../../../types'; | ||
import { trackDeprecatedRouteUsage } from '../../../../lib/track_deprecated_route_usage'; | ||
import { | ||
muteAllRuleRequestParamsSchemaV1, | ||
MuteAllRuleRequestParamsV1, | ||
} from '../../../../../common/routes/rule/apis/mute_all'; | ||
|
||
export const muteAllRuleRoute = ( | ||
router: IRouter<AlertingRequestHandlerContext>, | ||
|
@@ -36,7 +31,7 @@ export const muteAllRuleRoute = ( | |
}, | ||
validate: { | ||
request: { | ||
params: paramSchema, | ||
params: muteAllRuleRequestParamsSchemaV1, | ||
}, | ||
response: { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Previously, when we did this versioning we ended up also updating the documentation. I don't know if we will do another passing but I noticed 403 is missing from the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If I'm not mistaken, there is an extra issue to do it afterwards (which should be also be ready for next ff) #195182. Are we talking about the same? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ah, I didn't realize there was a separate issue. Nevermind then! |
||
204: { | ||
|
@@ -48,10 +43,10 @@ export const muteAllRuleRoute = ( | |
router.handleLegacyErrors( | ||
verifyAccessAndContext(licenseState, async function (context, req, res) { | ||
const rulesClient = (await context.alerting).getRulesClient(); | ||
const { id } = req.params; | ||
const params: MuteAllRuleRequestParamsV1 = req.params; | ||
trackDeprecatedRouteUsage('muteAll', usageCounter); | ||
try { | ||
await rulesClient.muteAll({ id }); | ||
await rulesClient.muteAll(params); | ||
return res.noContent(); | ||
} catch (e) { | ||
if (e instanceof RuleTypeDisabledError) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please add a unit test for this new change?