forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added getAlertState on AlertClient and exposed on api
- Loading branch information
Showing
15 changed files
with
614 additions
and
24 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
73 changes: 73 additions & 0 deletions
73
x-pack/legacy/plugins/alerting/server/routes/get_alert_state.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,73 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { createMockServer } from './_mock_server'; | ||
import { getAlertStateRoute } from './getAlertState'; | ||
import { SavedObjectsErrorHelpers } from 'src/core/server'; | ||
|
||
const { server, alertsClient } = createMockServer(); | ||
server.route(getAlertStateRoute); | ||
|
||
const mockedAlertState = { | ||
alertTypeState: { | ||
some: 'value', | ||
}, | ||
alertInstances: { | ||
first_instance: { | ||
state: {}, | ||
meta: { | ||
lastScheduledActions: { | ||
group: 'first_group', | ||
date: new Date(), | ||
}, | ||
}, | ||
}, | ||
second_instance: {}, | ||
}, | ||
}; | ||
|
||
beforeEach(() => jest.resetAllMocks()); | ||
|
||
test('gets alert state', async () => { | ||
const request = { | ||
method: 'GET', | ||
url: '/api/alert/1/state', | ||
}; | ||
|
||
alertsClient.getAlertState.mockResolvedValueOnce(mockedAlertState); | ||
|
||
const { statusCode } = await server.inject(request); | ||
expect(statusCode).toBe(200); | ||
expect(alertsClient.getAlertState).toHaveBeenCalledWith({ id: '1' }); | ||
}); | ||
|
||
test('returns NO-CONTENT when alert exists but has no task state yet', async () => { | ||
const request = { | ||
method: 'GET', | ||
url: '/api/alert/1/state', | ||
}; | ||
|
||
alertsClient.getAlertState.mockResolvedValueOnce(undefined); | ||
|
||
const { statusCode } = await server.inject(request); | ||
expect(statusCode).toBe(204); | ||
expect(alertsClient.getAlertState).toHaveBeenCalledWith({ id: '1' }); | ||
}); | ||
|
||
test('returns NOT-FOUND when alert is not found', async () => { | ||
const request = { | ||
method: 'GET', | ||
url: '/api/alert/1/state', | ||
}; | ||
|
||
alertsClient.getAlertState.mockRejectedValue( | ||
SavedObjectsErrorHelpers.createGenericNotFoundError('alert', '1') | ||
); | ||
|
||
const { statusCode } = await server.inject(request); | ||
expect(statusCode).toBe(404); | ||
expect(alertsClient.getAlertState).toHaveBeenCalledWith({ id: '1' }); | ||
}); |
35 changes: 35 additions & 0 deletions
35
x-pack/legacy/plugins/alerting/server/routes/get_alert_state.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,35 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import Joi from 'joi'; | ||
import Hapi from 'hapi'; | ||
|
||
interface GetAlertStateRequest extends Hapi.Request { | ||
params: { | ||
id: string; | ||
}; | ||
} | ||
|
||
export const getAlertStateRoute = { | ||
method: 'GET', | ||
path: '/api/alert/{id}/state', | ||
options: { | ||
tags: ['access:alerting-read'], | ||
validate: { | ||
params: Joi.object() | ||
.keys({ | ||
id: Joi.string().required(), | ||
}) | ||
.required(), | ||
}, | ||
}, | ||
async handler(request: GetAlertStateRequest, h: Hapi.ResponseToolkit) { | ||
const { id } = request.params; | ||
const alertsClient = request.getAlertsClient!(); | ||
const state = await alertsClient.getAlertState({ id }); | ||
return state ? state : h.response().code(204); | ||
}, | ||
}; |
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.