Skip to content

Commit

Permalink
[Alerting] Unifies the use of a single constant for Api urls in alert…
Browse files Browse the repository at this point in the history
…ing & actions (#60430) (#60786)

Unifies the use of a single constant for Api urls in alerting & actions

Co-authored-by: Elastic Machine <[email protected]>
  • Loading branch information
gmmorris and elasticmachine authored Mar 23, 2020
1 parent 42857d5 commit 78a963f
Show file tree
Hide file tree
Showing 25 changed files with 63 additions and 35 deletions.
10 changes: 7 additions & 3 deletions examples/alerting_example/public/components/view_alert.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ import {
import { withRouter, RouteComponentProps } from 'react-router-dom';
import { CoreStart } from 'kibana/public';
import { isEmpty } from 'lodash';
import { Alert, AlertTaskState } from '../../../../x-pack/plugins/alerting/common';
import {
Alert,
AlertTaskState,
BASE_ALERT_API_PATH,
} from '../../../../x-pack/plugins/alerting/common';
import { ALERTING_EXAMPLE_APP_ID } from '../../common/constants';

type Props = RouteComponentProps & {
Expand All @@ -45,10 +49,10 @@ export const ViewAlertPage = withRouter(({ http, id }: Props) => {

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ import {
import { withRouter, RouteComponentProps } from 'react-router-dom';
import { CoreStart } from 'kibana/public';
import { isEmpty } from 'lodash';
import { Alert, AlertTaskState } from '../../../../x-pack/plugins/alerting/common';
import {
Alert,
AlertTaskState,
BASE_ALERT_API_PATH,
} from '../../../../x-pack/plugins/alerting/common';
import { ALERTING_EXAMPLE_APP_ID } from '../../common/constants';

type Props = RouteComponentProps & {
Expand All @@ -51,10 +55,10 @@ export const ViewPeopleInSpaceAlertPage = withRouter(({ http, id }: Props) => {

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
import { kfetch } from 'ui/kfetch';
import { omit, pick } from 'lodash';
import { i18n } from '@kbn/i18n';
import { ActionResult } from '../../../../../../../plugins/actions/common';
import { ActionResult, BASE_ACTION_API_PATH } from '../../../../../../../plugins/actions/common';
import { ManageEmailAction, EmailActionData } from '../manage_email_action';
import { ALERT_ACTION_TYPE_EMAIL } from '../../../../common/constants';
import { NEW_ACTION_ID } from './configuration';
Expand All @@ -44,7 +44,7 @@ export const Step1: React.FC<GetStep1Props> = (props: GetStep1Props) => {
if (props.editAction) {
await kfetch({
method: 'PUT',
pathname: `/api/action/${props.editAction.id}`,
pathname: `${BASE_ACTION_API_PATH}/${props.editAction.id}`,
body: JSON.stringify({
name: props.editAction.name,
config: omit(data, ['user', 'password']),
Expand All @@ -55,7 +55,7 @@ export const Step1: React.FC<GetStep1Props> = (props: GetStep1Props) => {
} else {
await kfetch({
method: 'POST',
pathname: '/api/action',
pathname: BASE_ACTION_API_PATH,
body: JSON.stringify({
name: i18n.translate('xpack.monitoring.alerts.configuration.emailAction.name', {
defaultMessage: 'Email action for Stack Monitoring alerts',
Expand All @@ -75,7 +75,7 @@ export const Step1: React.FC<GetStep1Props> = (props: GetStep1Props) => {

await kfetch({
method: 'DELETE',
pathname: `/api/action/${id}`,
pathname: `${BASE_ACTION_API_PATH}/${id}`,
});

if (props.editAction && props.editAction.id === id) {
Expand All @@ -101,7 +101,7 @@ export const Step1: React.FC<GetStep1Props> = (props: GetStep1Props) => {

const result = await kfetch({
method: 'POST',
pathname: `/api/action/${props.selectedEmailActionId}/_execute`,
pathname: `${BASE_ACTION_API_PATH}/${props.selectedEmailActionId}/_execute`,
body: JSON.stringify({ params }),
});
if (result.status === 'ok') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
import { i18n } from '@kbn/i18n';
import { FormattedMessage } from '@kbn/i18n/react';
import { ELASTIC_WEBSITE_URL, DOC_LINK_VERSION } from 'ui/documentation_links';
import { Alert } from '../../../../../../plugins/alerting/common';
import { Alert, BASE_ALERT_API_PATH } from '../../../../../../plugins/alerting/common';
import { getSetupModeState, addSetupModeCallback, toggleSetupMode } from '../../lib/setup_mode';
import { NUMBER_OF_MIGRATED_ALERTS, ALERT_TYPE_PREFIX } from '../../../common/constants';
import { AlertsConfiguration } from './configuration';
Expand All @@ -39,7 +39,7 @@ export const AlertsStatus: React.FC<AlertsStatusProps> = (props: AlertsStatusPro

React.useEffect(() => {
async function fetchAlertsStatus() {
const alerts = await kfetch({ method: 'GET', pathname: `/api/alert/_find` });
const alerts = await kfetch({ method: 'GET', pathname: `${BASE_ALERT_API_PATH}/_find` });
const monitoringAlerts = alerts.data.filter((alert: Alert) =>
alert.alertTypeId.startsWith(ALERT_TYPE_PREFIX)
);
Expand Down
3 changes: 2 additions & 1 deletion x-pack/plugins/actions/server/routes/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
} from 'kibana/server';
import { ActionResult } from '../types';
import { ILicenseState, verifyApiAccess, isErrorThatHandlesItsOwnResponse } from '../lib';
import { BASE_ACTION_API_PATH } from '../../common';

export const bodySchema = schema.object({
name: schema.string(),
Expand All @@ -25,7 +26,7 @@ export const bodySchema = schema.object({
export const createActionRoute = (router: IRouter, licenseState: ILicenseState) => {
router.post(
{
path: `/api/action`,
path: BASE_ACTION_API_PATH,
validate: {
body: bodySchema,
},
Expand Down
3 changes: 2 additions & 1 deletion x-pack/plugins/actions/server/routes/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
KibanaResponseFactory,
} from 'kibana/server';
import { ILicenseState, verifyApiAccess } from '../lib';
import { BASE_ACTION_API_PATH } from '../../common';

const paramSchema = schema.object({
id: schema.string(),
Expand All @@ -26,7 +27,7 @@ const paramSchema = schema.object({
export const deleteActionRoute = (router: IRouter, licenseState: ILicenseState) => {
router.delete(
{
path: `/api/action/{id}`,
path: `${BASE_ACTION_API_PATH}/{id}`,
validate: {
params: paramSchema,
},
Expand Down
3 changes: 2 additions & 1 deletion x-pack/plugins/actions/server/routes/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { ILicenseState, verifyApiAccess, isErrorThatHandlesItsOwnResponse } from

import { ActionExecutorContract } from '../lib';
import { ActionTypeExecutorResult } from '../types';
import { BASE_ACTION_API_PATH } from '../../common';

const paramSchema = schema.object({
id: schema.string(),
Expand All @@ -31,7 +32,7 @@ export const executeActionRoute = (
) => {
router.post(
{
path: '/api/action/{id}/_execute',
path: `${BASE_ACTION_API_PATH}/{id}/_execute`,
validate: {
body: bodySchema,
params: paramSchema,
Expand Down
3 changes: 2 additions & 1 deletion x-pack/plugins/actions/server/routes/find.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
} from 'kibana/server';
import { FindOptions } from '../../../alerting/server';
import { ILicenseState, verifyApiAccess } from '../lib';
import { BASE_ACTION_API_PATH } from '../../common';

// config definition
const querySchema = schema.object({
Expand Down Expand Up @@ -43,7 +44,7 @@ const querySchema = schema.object({
export const findActionRoute = (router: IRouter, licenseState: ILicenseState) => {
router.get(
{
path: `/api/action/_find`,
path: `${BASE_ACTION_API_PATH}/_find`,
validate: {
query: querySchema,
},
Expand Down
3 changes: 2 additions & 1 deletion x-pack/plugins/actions/server/routes/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
KibanaResponseFactory,
} from 'kibana/server';
import { ILicenseState, verifyApiAccess } from '../lib';
import { BASE_ACTION_API_PATH } from '../../common';

const paramSchema = schema.object({
id: schema.string(),
Expand All @@ -21,7 +22,7 @@ const paramSchema = schema.object({
export const getActionRoute = (router: IRouter, licenseState: ILicenseState) => {
router.get(
{
path: `/api/action/{id}`,
path: `${BASE_ACTION_API_PATH}/{id}`,
validate: {
params: paramSchema,
},
Expand Down
3 changes: 2 additions & 1 deletion x-pack/plugins/actions/server/routes/list_action_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ import {
KibanaResponseFactory,
} from 'kibana/server';
import { ILicenseState, verifyApiAccess } from '../lib';
import { BASE_ACTION_API_PATH } from '../../common';

export const listActionTypesRoute = (router: IRouter, licenseState: ILicenseState) => {
router.get(
{
path: `/api/action/types`,
path: `${BASE_ACTION_API_PATH}/types`,
validate: {},
options: {
tags: ['access:actions-read'],
Expand Down
3 changes: 2 additions & 1 deletion x-pack/plugins/actions/server/routes/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
KibanaResponseFactory,
} from 'kibana/server';
import { ILicenseState, verifyApiAccess, isErrorThatHandlesItsOwnResponse } from '../lib';
import { BASE_ACTION_API_PATH } from '../../common';

const paramSchema = schema.object({
id: schema.string(),
Expand All @@ -27,7 +28,7 @@ const bodySchema = schema.object({
export const updateActionRoute = (router: IRouter, licenseState: ILicenseState) => {
router.put(
{
path: `/api/action/{id}`,
path: `${BASE_ACTION_API_PATH}/{id}`,
validate: {
body: bodySchema,
params: paramSchema,
Expand Down
4 changes: 2 additions & 2 deletions x-pack/plugins/alerting/server/routes/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
import { LicenseState } from '../lib/license_state';
import { verifyApiAccess } from '../lib/license_api_access';
import { validateDurationSchema } from '../lib';
import { Alert } from '../types';
import { Alert, BASE_ALERT_API_PATH } from '../types';

export const bodySchema = schema.object({
name: schema.string(),
Expand All @@ -42,7 +42,7 @@ export const bodySchema = schema.object({
export const createAlertRoute = (router: IRouter, licenseState: LicenseState) => {
router.post(
{
path: '/api/alert',
path: BASE_ALERT_API_PATH,
validate: {
body: bodySchema,
},
Expand Down
3 changes: 2 additions & 1 deletion x-pack/plugins/alerting/server/routes/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
} from 'kibana/server';
import { LicenseState } from '../lib/license_state';
import { verifyApiAccess } from '../lib/license_api_access';
import { BASE_ALERT_API_PATH } from '../../common';

const paramSchema = schema.object({
id: schema.string(),
Expand All @@ -22,7 +23,7 @@ const paramSchema = schema.object({
export const deleteAlertRoute = (router: IRouter, licenseState: LicenseState) => {
router.delete(
{
path: '/api/alert/{id}',
path: `${BASE_ALERT_API_PATH}/{id}`,
validate: {
params: paramSchema,
},
Expand Down
3 changes: 2 additions & 1 deletion x-pack/plugins/alerting/server/routes/disable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
} from 'kibana/server';
import { LicenseState } from '../lib/license_state';
import { verifyApiAccess } from '../lib/license_api_access';
import { BASE_ALERT_API_PATH } from '../../common';

const paramSchema = schema.object({
id: schema.string(),
Expand All @@ -22,7 +23,7 @@ const paramSchema = schema.object({
export const disableAlertRoute = (router: IRouter, licenseState: LicenseState) => {
router.post(
{
path: '/api/alert/{id}/_disable',
path: `${BASE_ALERT_API_PATH}/{id}/_disable`,
validate: {
params: paramSchema,
},
Expand Down
3 changes: 2 additions & 1 deletion x-pack/plugins/alerting/server/routes/enable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
} from 'kibana/server';
import { LicenseState } from '../lib/license_state';
import { verifyApiAccess } from '../lib/license_api_access';
import { BASE_ALERT_API_PATH } from '../../common';

const paramSchema = schema.object({
id: schema.string(),
Expand All @@ -22,7 +23,7 @@ const paramSchema = schema.object({
export const enableAlertRoute = (router: IRouter, licenseState: LicenseState) => {
router.post(
{
path: '/api/alert/{id}/_enable',
path: `${BASE_ALERT_API_PATH}/{id}/_enable`,
validate: {
params: paramSchema,
},
Expand Down
3 changes: 2 additions & 1 deletion x-pack/plugins/alerting/server/routes/find.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
import { FindOptions } from '../../../alerting/server';
import { LicenseState } from '../lib/license_state';
import { verifyApiAccess } from '../lib/license_api_access';
import { BASE_ALERT_API_PATH } from '../../common';

// config definition
const querySchema = schema.object({
Expand Down Expand Up @@ -44,7 +45,7 @@ const querySchema = schema.object({
export const findAlertRoute = (router: IRouter, licenseState: LicenseState) => {
router.get(
{
path: '/api/alert/_find',
path: `${BASE_ALERT_API_PATH}/_find`,
validate: {
query: querySchema,
},
Expand Down
3 changes: 2 additions & 1 deletion x-pack/plugins/alerting/server/routes/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
} from 'kibana/server';
import { LicenseState } from '../lib/license_state';
import { verifyApiAccess } from '../lib/license_api_access';
import { BASE_ALERT_API_PATH } from '../../common';

const paramSchema = schema.object({
id: schema.string(),
Expand All @@ -22,7 +23,7 @@ const paramSchema = schema.object({
export const getAlertRoute = (router: IRouter, licenseState: LicenseState) => {
router.get(
{
path: `/api/alert/{id}`,
path: `${BASE_ALERT_API_PATH}/{id}`,
validate: {
params: paramSchema,
},
Expand Down
3 changes: 2 additions & 1 deletion x-pack/plugins/alerting/server/routes/get_alert_state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
} from 'kibana/server';
import { LicenseState } from '../lib/license_state';
import { verifyApiAccess } from '../lib/license_api_access';
import { BASE_ALERT_API_PATH } from '../../common';

const paramSchema = schema.object({
id: schema.string(),
Expand All @@ -22,7 +23,7 @@ const paramSchema = schema.object({
export const getAlertStateRoute = (router: IRouter, licenseState: LicenseState) => {
router.get(
{
path: '/api/alert/{id}/state',
path: `${BASE_ALERT_API_PATH}/{id}/state`,
validate: {
params: paramSchema,
},
Expand Down
3 changes: 2 additions & 1 deletion x-pack/plugins/alerting/server/routes/list_alert_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ import {
} from 'kibana/server';
import { LicenseState } from '../lib/license_state';
import { verifyApiAccess } from '../lib/license_api_access';
import { BASE_ALERT_API_PATH } from '../../common';

export const listAlertTypesRoute = (router: IRouter, licenseState: LicenseState) => {
router.get(
{
path: `/api/alert/types`,
path: `${BASE_ALERT_API_PATH}/types`,
validate: {},
options: {
tags: ['access:alerting-read'],
Expand Down
3 changes: 2 additions & 1 deletion x-pack/plugins/alerting/server/routes/mute_all.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
} from 'kibana/server';
import { LicenseState } from '../lib/license_state';
import { verifyApiAccess } from '../lib/license_api_access';
import { BASE_ALERT_API_PATH } from '../../common';

const paramSchema = schema.object({
id: schema.string(),
Expand All @@ -22,7 +23,7 @@ const paramSchema = schema.object({
export const muteAllAlertRoute = (router: IRouter, licenseState: LicenseState) => {
router.post(
{
path: '/api/alert/{id}/_mute_all',
path: `${BASE_ALERT_API_PATH}/{id}/_mute_all`,
validate: {
params: paramSchema,
},
Expand Down
3 changes: 2 additions & 1 deletion x-pack/plugins/alerting/server/routes/mute_instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
} from 'kibana/server';
import { LicenseState } from '../lib/license_state';
import { verifyApiAccess } from '../lib/license_api_access';
import { BASE_ALERT_API_PATH } from '../../common';

const paramSchema = schema.object({
alertId: schema.string(),
Expand All @@ -23,7 +24,7 @@ const paramSchema = schema.object({
export const muteAlertInstanceRoute = (router: IRouter, licenseState: LicenseState) => {
router.post(
{
path: '/api/alert/{alertId}/alert_instance/{alertInstanceId}/_mute',
path: `${BASE_ALERT_API_PATH}/{alertId}/alert_instance/{alertInstanceId}/_mute`,
validate: {
params: paramSchema,
},
Expand Down
Loading

0 comments on commit 78a963f

Please sign in to comment.