Skip to content

Commit

Permalink
Rename BASE_ALERT_API_PATH to LEGACY_BASE_ALERT_API_PATH
Browse files Browse the repository at this point in the history
  • Loading branch information
mikecote committed Mar 8, 2021
1 parent d66cf3d commit d692495
Show file tree
Hide file tree
Showing 40 changed files with 940 additions and 86 deletions.
40 changes: 20 additions & 20 deletions api_docs/alerting.json
Original file line number Diff line number Diff line change
Expand Up @@ -955,21 +955,21 @@
"description": [],
"source": {
"path": "x-pack/plugins/alerting/server/plugin.ts",
"lineNumber": 112
"lineNumber": 94
}
}
],
"tags": [],
"returnComment": [],
"source": {
"path": "x-pack/plugins/alerting/server/plugin.ts",
"lineNumber": 104
"lineNumber": 86
}
}
],
"source": {
"path": "x-pack/plugins/alerting/server/plugin.ts",
"lineNumber": 103
"lineNumber": 85
},
"initialIsOpen": false
},
Expand All @@ -988,7 +988,7 @@
"description": [],
"source": {
"path": "x-pack/plugins/alerting/server/plugin.ts",
"lineNumber": 124
"lineNumber": 106
},
"signature": [
"() => Set<",
Expand Down Expand Up @@ -1044,15 +1044,15 @@
"description": [],
"source": {
"path": "x-pack/plugins/alerting/server/plugin.ts",
"lineNumber": 125
"lineNumber": 107
}
}
],
"tags": [],
"returnComment": [],
"source": {
"path": "x-pack/plugins/alerting/server/plugin.ts",
"lineNumber": 125
"lineNumber": 107
}
},
{
Expand All @@ -1063,7 +1063,7 @@
"description": [],
"source": {
"path": "x-pack/plugins/alerting/server/plugin.ts",
"lineNumber": 126
"lineNumber": 108
},
"signature": [
"() => Promise<",
Expand All @@ -1080,7 +1080,7 @@
],
"source": {
"path": "x-pack/plugins/alerting/server/plugin.ts",
"lineNumber": 123
"lineNumber": 105
},
"initialIsOpen": false
}
Expand Down Expand Up @@ -2991,32 +2991,32 @@
"initialIsOpen": false
},
{
"id": "def-common.DefaultActionGroupId",
"type": "Type",
"label": "DefaultActionGroupId",
"tags": [],
"id": "def-common.BASE_ALERT_API_PATH",
"type": "string",
"label": "BASE_ALERT_API_PATH",
"description": [],
"source": {
"path": "x-pack/plugins/alerting/common/index.ts",
"lineNumber": 27
"path": "x-pack/plugins/alerting/common/builtin_action_groups.ts",
"lineNumber": 11
},
"signature": [
"\"/api/alerts\""
"\"default\""
],
"initialIsOpen": false
},
{
"id": "def-common.DefaultActionGroupId",
"type": "Type",
"label": "DefaultActionGroupId",
"tags": [],
"id": "def-common.LEGACY_BASE_ALERT_API_PATH",
"type": "string",
"label": "LEGACY_BASE_ALERT_API_PATH",
"description": [],
"source": {
"path": "x-pack/plugins/alerting/common/builtin_action_groups.ts",
"lineNumber": 11
"path": "x-pack/plugins/alerting/common/index.ts",
"lineNumber": 27
},
"signature": [
"\"default\""
"\"/api/alerts\""
],
"initialIsOpen": false
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,12 @@ import {
import { withRouter, RouteComponentProps } from 'react-router-dom';
import { CoreStart } from 'kibana/public';
import { isEmpty } from 'lodash';
import { Alert, AlertTaskState, BASE_ALERT_API_PATH } from '../../../../plugins/alerting/common';
import { ALERTING_EXAMPLE_APP_ID } from '../../common/constants';
import {
Alert,
AlertTaskState,
LEGACY_BASE_ALERT_API_PATH,
} from '../../../../plugins/alerting/common';

type Props = RouteComponentProps & {
http: CoreStart['http'];
Expand All @@ -34,10 +38,10 @@ export const ViewAlertPage = withRouter(({ http, id }: Props) => {

useEffect(() => {
if (!alert) {
http.get(`${BASE_ALERT_API_PATH}/alert/${id}`).then(setAlert);
http.get(`${LEGACY_BASE_ALERT_API_PATH}/alert/${id}`).then(setAlert);
}
if (!alertState) {
http.get(`${BASE_ALERT_API_PATH}/alert/${id}/state`).then(setAlertState);
http.get(`${LEGACY_BASE_ALERT_API_PATH}/alert/${id}/state`).then(setAlertState);
}
}, [alert, alertState, http, id]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@ import {
import { withRouter, RouteComponentProps } from 'react-router-dom';
import { CoreStart } from 'kibana/public';
import { isEmpty } from 'lodash';
import { Alert, AlertTaskState, BASE_ALERT_API_PATH } from '../../../../plugins/alerting/common';
import { ALERTING_EXAMPLE_APP_ID, AlwaysFiringParams } from '../../common/constants';
import {
Alert,
AlertTaskState,
LEGACY_BASE_ALERT_API_PATH,
} from '../../../../plugins/alerting/common';

type Props = RouteComponentProps & {
http: CoreStart['http'];
Expand All @@ -40,10 +44,10 @@ export const ViewPeopleInSpaceAlertPage = withRouter(({ http, id }: Props) => {

useEffect(() => {
if (!alert) {
http.get(`${BASE_ALERT_API_PATH}/alert/${id}`).then(setAlert);
http.get(`${LEGACY_BASE_ALERT_API_PATH}/alert/${id}`).then(setAlert);
}
if (!alertState) {
http.get(`${BASE_ALERT_API_PATH}/alert/${id}/state`).then(setAlertState);
http.get(`${LEGACY_BASE_ALERT_API_PATH}/alert/${id}/state`).then(setAlertState);
}
}, [alert, alertState, http, id]);

Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/alerting/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ export interface AlertingFrameworkHealth {
alertingFrameworkHeath: AlertsHealth;
}

export const BASE_ALERT_API_PATH = '/api/alerts';
export const LEGACY_BASE_ALERT_API_PATH = '/api/alerts';
export const ALERTS_FEATURE_ID = 'alerts';
8 changes: 4 additions & 4 deletions x-pack/plugins/alerting/public/alert_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@

import { HttpSetup } from 'kibana/public';
import { i18n } from '@kbn/i18n';
import { BASE_ALERT_API_PATH } from '../common';
import { LEGACY_BASE_ALERT_API_PATH } from '../common';
import type { Alert, AlertType } from '../common';

export async function loadAlertTypes({ http }: { http: HttpSetup }): Promise<AlertType[]> {
return await http.get(`${BASE_ALERT_API_PATH}/list_alert_types`);
return await http.get(`${LEGACY_BASE_ALERT_API_PATH}/list_alert_types`);
}

export async function loadAlertType({
Expand All @@ -22,7 +22,7 @@ export async function loadAlertType({
id: AlertType['id'];
}): Promise<AlertType> {
const maybeAlertType = ((await http.get(
`${BASE_ALERT_API_PATH}/list_alert_types`
`${LEGACY_BASE_ALERT_API_PATH}/list_alert_types`
)) as AlertType[]).find((type) => type.id === id);
if (!maybeAlertType) {
throw new Error(
Expand All @@ -44,5 +44,5 @@ export async function loadAlert({
http: HttpSetup;
alertId: string;
}): Promise<Alert> {
return await http.get(`${BASE_ALERT_API_PATH}/alert/${alertId}`);
return await http.get(`${LEGACY_BASE_ALERT_API_PATH}/alert/${alertId}`);
}
73 changes: 73 additions & 0 deletions x-pack/plugins/alerting/server/routes/aggregate.ts
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,
});
})
);
};
87 changes: 87 additions & 0 deletions x-pack/plugins/alerting/server/routes/create.ts
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;
}
})
)
);
};
Loading

0 comments on commit d692495

Please sign in to comment.