-
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.
Rename BASE_ALERT_API_PATH to LEGACY_BASE_ALERT_API_PATH
- Loading branch information
Showing
40 changed files
with
940 additions
and
86 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
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
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 | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { schema } from '@kbn/config-schema'; | ||
import type { AlertingRouter } from '../types'; | ||
import { ILicenseState } from '../lib/license_state'; | ||
import { verifyApiAccess } from '../lib/license_api_access'; | ||
import { LEGACY_BASE_ALERT_API_PATH } from '../../common'; | ||
import { renameKeys } from './lib/rename_keys'; | ||
import { FindOptions } from '../alerts_client'; | ||
|
||
// config definition | ||
const querySchema = schema.object({ | ||
search: schema.maybe(schema.string()), | ||
default_search_operator: schema.oneOf([schema.literal('OR'), schema.literal('AND')], { | ||
defaultValue: 'OR', | ||
}), | ||
search_fields: schema.maybe(schema.oneOf([schema.arrayOf(schema.string()), schema.string()])), | ||
has_reference: schema.maybe( | ||
// use nullable as maybe is currently broken | ||
// in config-schema | ||
schema.nullable( | ||
schema.object({ | ||
type: schema.string(), | ||
id: schema.string(), | ||
}) | ||
) | ||
), | ||
filter: schema.maybe(schema.string()), | ||
}); | ||
|
||
export const aggregateAlertRoute = (router: AlertingRouter, licenseState: ILicenseState) => { | ||
router.get( | ||
{ | ||
path: `${LEGACY_BASE_ALERT_API_PATH}/_aggregate`, | ||
validate: { | ||
query: querySchema, | ||
}, | ||
}, | ||
router.handleLegacyErrors(async function (context, req, res) { | ||
verifyApiAccess(licenseState); | ||
if (!context.alerting) { | ||
return res.badRequest({ body: 'RouteHandlerContext is not registered for alerting' }); | ||
} | ||
const alertsClient = context.alerting.getAlertsClient(); | ||
|
||
const query = req.query; | ||
const renameMap = { | ||
default_search_operator: 'defaultSearchOperator', | ||
has_reference: 'hasReference', | ||
search: 'search', | ||
filter: 'filter', | ||
}; | ||
|
||
const options = renameKeys<FindOptions, Record<string, unknown>>(renameMap, query); | ||
|
||
if (query.search_fields) { | ||
options.searchFields = Array.isArray(query.search_fields) | ||
? query.search_fields | ||
: [query.search_fields]; | ||
} | ||
|
||
const aggregateResult = await alertsClient.aggregate({ options }); | ||
return res.ok({ | ||
body: aggregateResult, | ||
}); | ||
}) | ||
); | ||
}; |
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,87 @@ | ||
/* | ||
* 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'; | ||
import type { AlertingRouter } from '../types'; | ||
import { ILicenseState } from '../lib/license_state'; | ||
import { verifyApiAccess } from '../lib/license_api_access'; | ||
import { validateDurationSchema } from '../lib'; | ||
import { handleDisabledApiKeysError } from './lib/error_handler'; | ||
import { | ||
Alert, | ||
AlertNotifyWhenType, | ||
AlertTypeParams, | ||
LEGACY_BASE_ALERT_API_PATH, | ||
validateNotifyWhenType, | ||
} from '../types'; | ||
import { AlertTypeDisabledError } from '../lib/errors/alert_type_disabled'; | ||
|
||
export const bodySchema = schema.object({ | ||
name: schema.string(), | ||
alertTypeId: schema.string(), | ||
enabled: schema.boolean({ defaultValue: true }), | ||
consumer: schema.string(), | ||
tags: schema.arrayOf(schema.string(), { defaultValue: [] }), | ||
throttle: schema.nullable(schema.string({ validate: validateDurationSchema })), | ||
params: schema.recordOf(schema.string(), schema.any(), { defaultValue: {} }), | ||
schedule: schema.object({ | ||
interval: schema.string({ validate: validateDurationSchema }), | ||
}), | ||
actions: schema.arrayOf( | ||
schema.object({ | ||
group: schema.string(), | ||
id: schema.string(), | ||
actionTypeId: schema.maybe(schema.string()), | ||
params: schema.recordOf(schema.string(), schema.any(), { defaultValue: {} }), | ||
}), | ||
{ defaultValue: [] } | ||
), | ||
notifyWhen: schema.nullable(schema.string({ validate: validateNotifyWhenType })), | ||
}); | ||
|
||
export const createAlertRoute = (router: AlertingRouter, licenseState: ILicenseState) => { | ||
router.post( | ||
{ | ||
path: `${LEGACY_BASE_ALERT_API_PATH}/alert/{id?}`, | ||
validate: { | ||
params: schema.maybe( | ||
schema.object({ | ||
id: schema.maybe(schema.string()), | ||
}) | ||
), | ||
body: bodySchema, | ||
}, | ||
}, | ||
handleDisabledApiKeysError( | ||
router.handleLegacyErrors(async function (context, req, res) { | ||
verifyApiAccess(licenseState); | ||
|
||
if (!context.alerting) { | ||
return res.badRequest({ body: 'RouteHandlerContext is not registered for alerting' }); | ||
} | ||
const alertsClient = context.alerting.getAlertsClient(); | ||
const alert = req.body; | ||
const params = req.params; | ||
const notifyWhen = alert?.notifyWhen ? (alert.notifyWhen as AlertNotifyWhenType) : null; | ||
try { | ||
const alertRes: Alert<AlertTypeParams> = await alertsClient.create<AlertTypeParams>({ | ||
data: { ...alert, notifyWhen }, | ||
options: { id: params?.id }, | ||
}); | ||
return res.ok({ | ||
body: alertRes, | ||
}); | ||
} catch (e) { | ||
if (e instanceof AlertTypeDisabledError) { | ||
return e.sendResponse(res); | ||
} | ||
throw e; | ||
} | ||
}) | ||
) | ||
); | ||
}; |
Oops, something went wrong.