From 4ba8ab9b588f4d26970a6e98d56f021b6f08d734 Mon Sep 17 00:00:00 2001 From: Dominique Clarke Date: Sun, 8 Aug 2021 22:48:18 -0400 Subject: [PATCH] uptime - migrate legacy alert apis --- .../triggers_actions_ui/public/index.ts | 1 + .../triggers_actions_ui/public/types.ts | 2 + .../uptime/common/constants/rest_api.ts | 10 +-- .../public/state/api/alert_actions.test.ts | 4 -- .../uptime/public/state/api/alert_actions.ts | 10 +-- .../plugins/uptime/public/state/api/alerts.ts | 63 +++++++++++++++---- 6 files changed, 63 insertions(+), 27 deletions(-) diff --git a/x-pack/plugins/triggers_actions_ui/public/index.ts b/x-pack/plugins/triggers_actions_ui/public/index.ts index eedc5566ba1d5..b50a7efc9d907 100644 --- a/x-pack/plugins/triggers_actions_ui/public/index.ts +++ b/x-pack/plugins/triggers_actions_ui/public/index.ts @@ -21,6 +21,7 @@ export type { IErrorObject, AlertFlyoutCloseReason, AlertTypeParams, + AsApiContract, } from './types'; export { diff --git a/x-pack/plugins/triggers_actions_ui/public/types.ts b/x-pack/plugins/triggers_actions_ui/public/types.ts index b17d0493c4494..f01967592ea8c 100644 --- a/x-pack/plugins/triggers_actions_ui/public/types.ts +++ b/x-pack/plugins/triggers_actions_ui/public/types.ts @@ -17,6 +17,7 @@ import { AlertHistoryDocumentTemplate, ALERT_HISTORY_PREFIX, AlertHistoryDefaultIndexName, + AsApiContract, } from '../../actions/common'; import { TypeRegistry } from './application/type_registry'; import { @@ -58,6 +59,7 @@ export { AlertHistoryDocumentTemplate, AlertHistoryDefaultIndexName, ALERT_HISTORY_PREFIX, + AsApiContract, }; export type ActionTypeIndex = Record; diff --git a/x-pack/plugins/uptime/common/constants/rest_api.ts b/x-pack/plugins/uptime/common/constants/rest_api.ts index 6a75686266533..b49ccc3222a07 100644 --- a/x-pack/plugins/uptime/common/constants/rest_api.ts +++ b/x-pack/plugins/uptime/common/constants/rest_api.ts @@ -26,9 +26,9 @@ export enum API_URLS { ML_CAPABILITIES = '/api/ml/ml_capabilities', ML_ANOMALIES_RESULT = `/api/ml/results/anomalies_table_data`, - ALERT_ACTIONS = '/api/actions', - CREATE_ALERT = '/api/alerts/alert', - ALERT = '/api/alerts/alert/', - ALERTS_FIND = '/api/alerts/_find', - ACTION_TYPES = '/api/actions/list_action_types', + RULE_CONNECTORS = '/api/actions/connectors', + CREATE_RULE = '/api/alerting/rule', + DELETE_RULE = '/api/alerting/rule/', + RULES_FIND = '/api/alerting/rules/_find', + CONNECTOR_TYPES = '/api/actions/connector_types', } diff --git a/x-pack/plugins/uptime/public/state/api/alert_actions.test.ts b/x-pack/plugins/uptime/public/state/api/alert_actions.test.ts index 15fb95f5d4a4d..96c581f7fab98 100644 --- a/x-pack/plugins/uptime/public/state/api/alert_actions.test.ts +++ b/x-pack/plugins/uptime/public/state/api/alert_actions.test.ts @@ -58,7 +58,6 @@ describe('Alert Actions factory', () => { }); expect(resp).toEqual([ { - actionTypeId: '.pagerduty', group: 'recovered', id: 'f2a3b195-ed76-499a-805d-82d24d4eeba9', params: { @@ -69,7 +68,6 @@ describe('Alert Actions factory', () => { }, }, { - actionTypeId: '.pagerduty', group: 'xpack.uptime.alerts.actionGroups.monitorStatus', id: 'f2a3b195-ed76-499a-805d-82d24d4eeba9', params: { @@ -103,7 +101,6 @@ describe('Alert Actions factory', () => { }); expect(resp).toEqual([ { - actionTypeId: '.pagerduty', group: 'recovered', id: 'f2a3b195-ed76-499a-805d-82d24d4eeba9', params: { @@ -114,7 +111,6 @@ describe('Alert Actions factory', () => { }, }, { - actionTypeId: '.pagerduty', group: 'xpack.uptime.alerts.actionGroups.monitorStatus', id: 'f2a3b195-ed76-499a-805d-82d24d4eeba9', params: { diff --git a/x-pack/plugins/uptime/public/state/api/alert_actions.ts b/x-pack/plugins/uptime/public/state/api/alert_actions.ts index fff5fa9a67804..b0f5f3ea490e5 100644 --- a/x-pack/plugins/uptime/public/state/api/alert_actions.ts +++ b/x-pack/plugins/uptime/public/state/api/alert_actions.ts @@ -33,6 +33,8 @@ export const WEBHOOK_ACTION_ID: ActionTypeId = '.webhook'; const { MONITOR_STATUS } = ACTION_GROUP_DEFINITIONS; +export type RuleAction = Omit; + const getRecoveryMessage = (selectedMonitor: Ping) => { return i18n.translate('xpack.uptime.alerts.monitorStatus.recoveryMessage', { defaultMessage: 'Monitor {monitor} with url {url} has recovered with status Up', @@ -44,18 +46,16 @@ const getRecoveryMessage = (selectedMonitor: Ping) => { }; export function populateAlertActions({ defaultActions, selectedMonitor }: NewAlertParams) { - const actions: AlertAction[] = []; + const actions: RuleAction[] = []; defaultActions.forEach((aId) => { - const action: AlertAction = { + const action: RuleAction = { id: aId.id, - actionTypeId: aId.actionTypeId, group: MONITOR_STATUS.id, params: {}, }; - const recoveredAction: AlertAction = { + const recoveredAction: RuleAction = { id: aId.id, - actionTypeId: aId.actionTypeId, group: 'recovered', params: { message: getRecoveryMessage(selectedMonitor), diff --git a/x-pack/plugins/uptime/public/state/api/alerts.ts b/x-pack/plugins/uptime/public/state/api/alerts.ts index 5931936c48163..dc49556ed3956 100644 --- a/x-pack/plugins/uptime/public/state/api/alerts.ts +++ b/x-pack/plugins/uptime/public/state/api/alerts.ts @@ -10,18 +10,31 @@ import { apiService } from './utils'; import { ActionConnector } from '../alerts/alerts'; import { AlertsResult, MonitorIdParam } from '../actions/types'; -import { ActionType, AlertAction } from '../../../../triggers_actions_ui/public'; +import { ActionType, AsApiContract } from '../../../../triggers_actions_ui/public'; import { API_URLS } from '../../../common/constants'; import { Alert, AlertTypeParams } from '../../../../alerting/common'; import { AtomicStatusCheckParams } from '../../../common/runtime_types/alerts'; -import { populateAlertActions } from './alert_actions'; +import { populateAlertActions, RuleAction } from './alert_actions'; import { Ping } from '../../../common/runtime_types/ping'; const UPTIME_AUTO_ALERT = 'UPTIME_AUTO'; -export const fetchConnectors = async () => { - return await apiService.get(API_URLS.ALERT_ACTIONS); +export const fetchConnectors = async (): Promise => { + const response = (await apiService.get(API_URLS.RULE_CONNECTORS)) as Array< + AsApiContract + >; + /* eslint-disable @typescript-eslint/naming-convention */ + return response.map( + ({ connector_type_id, referenced_by_count, is_preconfigured, is_missing_secrets, ...res }) => ({ + ...res, + actionTypeId: connector_type_id, + referencedByCount: referenced_by_count, + isPreconfigured: is_preconfigured, + isMissingSecrets: is_missing_secrets, + }) + ); + /* eslint-enable @typescript-eslint/naming-convention */ }; export interface NewAlertParams extends AlertTypeParams { @@ -41,14 +54,21 @@ type NewMonitorStatusAlert = Omit< | 'muteAll' | 'mutedInstanceIds' | 'executionStatus' ->; + | 'alertTypeId' + | 'notifyWhen' + | 'actions' +> & { + rule_type_id: Alert['alertTypeId']; + notify_when: Alert['notifyWhen']; + actions: RuleAction[]; +}; export const createAlert = async ({ defaultActions, monitorId, selectedMonitor, }: NewAlertParams): Promise => { - const actions: AlertAction[] = populateAlertActions({ + const actions: RuleAction[] = populateAlertActions({ defaultActions, selectedMonitor, }); @@ -66,16 +86,16 @@ export const createAlert = async ({ filters: { 'url.port': [], 'observer.geo.name': [], 'monitor.type': [], tags: [] }, }, consumer: 'uptime', - alertTypeId: CLIENT_ALERT_TYPES.MONITOR_STATUS, + rule_type_id: CLIENT_ALERT_TYPES.MONITOR_STATUS, schedule: { interval: '1m' }, - notifyWhen: 'onActionGroupChange', + notify_when: 'onActionGroupChange', tags: [UPTIME_AUTO_ALERT], name: `${selectedMonitor?.monitor.name || selectedMonitor?.url?.full}(Simple status alert)`, enabled: true, throttle: null, }; - return await apiService.post(API_URLS.CREATE_ALERT, data); + return await apiService.post(API_URLS.CREATE_RULE, data); }; export const fetchMonitorAlertRecords = async (): Promise => { @@ -89,7 +109,7 @@ export const fetchMonitorAlertRecords = async (): Promise => { search_fields: ['name', 'tags'], search: 'UPTIME_AUTO', }; - return await apiService.get(API_URLS.ALERTS_FIND, data); + return await apiService.get(API_URLS.RULES_FIND, data); }; export const fetchAlertRecords = async ({ @@ -103,14 +123,31 @@ export const fetchAlertRecords = async ({ sort_field: 'name.keyword', sort_order: 'asc', }; - const alerts = await apiService.get(API_URLS.ALERTS_FIND, data); + const alerts = await apiService.get(API_URLS.RULES_FIND, data); return alerts.data.find((alert: Alert) => alert.params.monitorId === monitorId); }; export const disableAlertById = async ({ alertId }: { alertId: string }) => { - return await apiService.delete(API_URLS.ALERT + alertId); + return await apiService.delete(API_URLS.DELETE_RULE + alertId); }; export const fetchActionTypes = async (): Promise => { - return await apiService.get(API_URLS.ACTION_TYPES); + const response = (await apiService.get(API_URLS.CONNECTOR_TYPES)) as Array< + AsApiContract + >; + /* eslint-disable @typescript-eslint/naming-convention */ + return response.map( + ({ + enabled_in_config, + enabled_in_license, + minimum_license_required, + ...res + }: AsApiContract) => ({ + ...res, + enabledInConfig: enabled_in_config, + enabledInLicense: enabled_in_license, + minimumLicenseRequired: minimum_license_required, + }) + ); + /* eslint-enable @typescript-eslint/naming-convention */ };