-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[RAM] Add Snooze UI and Unsnooze API (#128214)
* Add Snooze UI and Unsnooze API * Add unsnooze writeoperation * Add unsnooze API tests * Add UI tests * Add tooltip and enable canceling snooze when clicking Enabled * Fix rulesClient mock Co-authored-by: Kibana Machine <[email protected]>
- Loading branch information
1 parent
51e0845
commit d102213
Showing
31 changed files
with
1,422 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
x-pack/plugins/alerting/server/routes/unsnooze_rule.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* 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 { unsnoozeRuleRoute } from './unsnooze_rule'; | ||
import { httpServiceMock } from 'src/core/server/mocks'; | ||
import { licenseStateMock } from '../lib/license_state.mock'; | ||
import { mockHandlerArguments } from './_mock_handler_arguments'; | ||
import { rulesClientMock } from '../rules_client.mock'; | ||
import { AlertTypeDisabledError } from '../lib/errors/alert_type_disabled'; | ||
|
||
const rulesClient = rulesClientMock.create(); | ||
jest.mock('../lib/license_api_access.ts', () => ({ | ||
verifyApiAccess: jest.fn(), | ||
})); | ||
|
||
beforeEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
describe('unsnoozeAlertRoute', () => { | ||
it('unsnoozes an alert', async () => { | ||
const licenseState = licenseStateMock.create(); | ||
const router = httpServiceMock.createRouter(); | ||
|
||
unsnoozeRuleRoute(router, licenseState); | ||
|
||
const [config, handler] = router.post.mock.calls[0]; | ||
|
||
expect(config.path).toMatchInlineSnapshot(`"/internal/alerting/rule/{id}/_unsnooze"`); | ||
|
||
rulesClient.unsnooze.mockResolvedValueOnce(); | ||
|
||
const [context, req, res] = mockHandlerArguments( | ||
{ rulesClient }, | ||
{ | ||
params: { | ||
id: '1', | ||
}, | ||
}, | ||
['noContent'] | ||
); | ||
|
||
expect(await handler(context, req, res)).toEqual(undefined); | ||
|
||
expect(rulesClient.unsnooze).toHaveBeenCalledTimes(1); | ||
expect(rulesClient.unsnooze.mock.calls[0]).toMatchInlineSnapshot(` | ||
Array [ | ||
Object { | ||
"id": "1", | ||
}, | ||
] | ||
`); | ||
|
||
expect(res.noContent).toHaveBeenCalled(); | ||
}); | ||
|
||
it('ensures the rule type gets validated for the license', async () => { | ||
const licenseState = licenseStateMock.create(); | ||
const router = httpServiceMock.createRouter(); | ||
|
||
unsnoozeRuleRoute(router, licenseState); | ||
|
||
const [, handler] = router.post.mock.calls[0]; | ||
|
||
rulesClient.unsnooze.mockRejectedValue(new AlertTypeDisabledError('Fail', 'license_invalid')); | ||
|
||
const [context, req, res] = mockHandlerArguments({ rulesClient }, { params: {}, body: {} }, [ | ||
'ok', | ||
'forbidden', | ||
]); | ||
|
||
await handler(context, req, res); | ||
|
||
expect(res.forbidden).toHaveBeenCalledWith({ body: { message: 'Fail' } }); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* 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 { IRouter } from 'kibana/server'; | ||
import { schema } from '@kbn/config-schema'; | ||
import { ILicenseState, RuleMutedError } from '../lib'; | ||
import { verifyAccessAndContext } from './lib'; | ||
import { AlertingRequestHandlerContext, INTERNAL_BASE_ALERTING_API_PATH } from '../types'; | ||
|
||
const paramSchema = schema.object({ | ||
id: schema.string(), | ||
}); | ||
|
||
export const unsnoozeRuleRoute = ( | ||
router: IRouter<AlertingRequestHandlerContext>, | ||
licenseState: ILicenseState | ||
) => { | ||
router.post( | ||
{ | ||
path: `${INTERNAL_BASE_ALERTING_API_PATH}/rule/{id}/_unsnooze`, | ||
validate: { | ||
params: paramSchema, | ||
}, | ||
}, | ||
router.handleLegacyErrors( | ||
verifyAccessAndContext(licenseState, async function (context, req, res) { | ||
const rulesClient = context.alerting.getRulesClient(); | ||
const params = req.params; | ||
try { | ||
await rulesClient.unsnooze({ ...params }); | ||
return res.noContent(); | ||
} catch (e) { | ||
if (e instanceof RuleMutedError) { | ||
return e.sendResponse(res); | ||
} | ||
throw e; | ||
} | ||
}) | ||
) | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
x-pack/plugins/triggers_actions_ui/common/parse_interval.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* 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 dateMath from '@elastic/datemath'; | ||
import { i18n } from '@kbn/i18n'; | ||
export const INTERVAL_STRING_RE = new RegExp(`^([\\d\\.]+)\\s*(${dateMath.units.join('|')})$`); | ||
|
||
export const parseInterval = (intervalString: string) => { | ||
if (intervalString) { | ||
const matches = intervalString.match(INTERVAL_STRING_RE); | ||
if (matches) { | ||
const value = Number(matches[1]); | ||
const unit = matches[2]; | ||
return { value, unit }; | ||
} | ||
} | ||
throw new Error( | ||
i18n.translate('xpack.triggersActionsUI.parseInterval.errorMessage', { | ||
defaultMessage: '{value} is not an interval string', | ||
values: { | ||
value: intervalString, | ||
}, | ||
}) | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.