From b826313e3c987284fc318b0294410e7e6c952fea Mon Sep 17 00:00:00 2001 From: Gidi Meir Morris Date: Thu, 21 May 2020 08:57:59 +0100 Subject: [PATCH] split alerts file into two - for actions and alerts --- .../plugins/alerts/server/action_types.ts | 200 ++++++++++++++++++ .../plugins/alerts/server/alert_types.ts | 187 +--------------- .../fixtures/plugins/alerts/server/plugin.ts | 4 +- 3 files changed, 204 insertions(+), 187 deletions(-) create mode 100644 x-pack/test/alerting_api_integration/common/fixtures/plugins/alerts/server/action_types.ts diff --git a/x-pack/test/alerting_api_integration/common/fixtures/plugins/alerts/server/action_types.ts b/x-pack/test/alerting_api_integration/common/fixtures/plugins/alerts/server/action_types.ts new file mode 100644 index 00000000000000..a921dac7d43a15 --- /dev/null +++ b/x-pack/test/alerting_api_integration/common/fixtures/plugins/alerts/server/action_types.ts @@ -0,0 +1,200 @@ +/* + * 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 { CoreSetup } from 'kibana/server'; +import { schema } from '@kbn/config-schema'; +import { FixtureStartDeps, FixtureSetupDeps } from './plugin'; +import { ActionType, ActionTypeExecutorOptions } from '../../../../../../../plugins/actions/server'; + +export function defineActionTypes( + core: CoreSetup, + { actions }: Pick +) { + const clusterClient = core.elasticsearch.adminClient; + + // Action types + const noopActionType: ActionType = { + id: 'test.noop', + name: 'Test: Noop', + minimumLicenseRequired: 'gold', + async executor() { + return { status: 'ok', actionId: '' }; + }, + }; + const indexRecordActionType: ActionType = { + id: 'test.index-record', + name: 'Test: Index Record', + minimumLicenseRequired: 'gold', + validate: { + params: schema.object({ + index: schema.string(), + reference: schema.string(), + message: schema.string(), + }), + config: schema.object({ + unencrypted: schema.string(), + }), + secrets: schema.object({ + encrypted: schema.string(), + }), + }, + async executor({ config, secrets, params, services, actionId }: ActionTypeExecutorOptions) { + await services.callCluster('index', { + index: params.index, + refresh: 'wait_for', + body: { + params, + config, + secrets, + reference: params.reference, + source: 'action:test.index-record', + }, + }); + return { status: 'ok', actionId }; + }, + }; + const failingActionType: ActionType = { + id: 'test.failing', + name: 'Test: Failing', + minimumLicenseRequired: 'gold', + validate: { + params: schema.object({ + index: schema.string(), + reference: schema.string(), + }), + }, + async executor({ config, secrets, params, services }: ActionTypeExecutorOptions) { + await services.callCluster('index', { + index: params.index, + refresh: 'wait_for', + body: { + params, + config, + secrets, + reference: params.reference, + source: 'action:test.failing', + }, + }); + throw new Error(`expected failure for ${params.index} ${params.reference}`); + }, + }; + const rateLimitedActionType: ActionType = { + id: 'test.rate-limit', + name: 'Test: Rate Limit', + minimumLicenseRequired: 'gold', + maxAttempts: 2, + validate: { + params: schema.object({ + index: schema.string(), + reference: schema.string(), + retryAt: schema.number(), + }), + }, + async executor({ config, params, services }: ActionTypeExecutorOptions) { + await services.callCluster('index', { + index: params.index, + refresh: 'wait_for', + body: { + params, + config, + reference: params.reference, + source: 'action:test.rate-limit', + }, + }); + return { + status: 'error', + retry: new Date(params.retryAt), + actionId: '', + }; + }, + }; + const authorizationActionType: ActionType = { + id: 'test.authorization', + name: 'Test: Authorization', + minimumLicenseRequired: 'gold', + validate: { + params: schema.object({ + callClusterAuthorizationIndex: schema.string(), + savedObjectsClientType: schema.string(), + savedObjectsClientId: schema.string(), + index: schema.string(), + reference: schema.string(), + }), + }, + async executor({ params, services, actionId }: ActionTypeExecutorOptions) { + // Call cluster + let callClusterSuccess = false; + let callClusterError; + try { + await services.callCluster('index', { + index: params.callClusterAuthorizationIndex, + refresh: 'wait_for', + body: { + param1: 'test', + }, + }); + callClusterSuccess = true; + } catch (e) { + callClusterError = e; + } + // Call scoped cluster + const callScopedCluster = services.getScopedCallCluster(clusterClient); + let callScopedClusterSuccess = false; + let callScopedClusterError; + try { + await callScopedCluster('index', { + index: params.callClusterAuthorizationIndex, + refresh: 'wait_for', + body: { + param1: 'test', + }, + }); + callScopedClusterSuccess = true; + } catch (e) { + callScopedClusterError = e; + } + // Saved objects client + let savedObjectsClientSuccess = false; + let savedObjectsClientError; + try { + await services.savedObjectsClient.get( + params.savedObjectsClientType, + params.savedObjectsClientId + ); + savedObjectsClientSuccess = true; + } catch (e) { + savedObjectsClientError = e; + } + // Save the result + await services.callCluster('index', { + index: params.index, + refresh: 'wait_for', + body: { + state: { + callClusterSuccess, + callClusterError, + callScopedClusterSuccess, + callScopedClusterError, + savedObjectsClientSuccess, + savedObjectsClientError, + }, + params, + reference: params.reference, + source: 'action:test.authorization', + }, + }); + return { + actionId, + status: 'ok', + }; + }, + }; + actions.registerType(noopActionType); + actions.registerType(indexRecordActionType); + actions.registerType(failingActionType); + actions.registerType(rateLimitedActionType); + actions.registerType(authorizationActionType); +} diff --git a/x-pack/test/alerting_api_integration/common/fixtures/plugins/alerts/server/alert_types.ts b/x-pack/test/alerting_api_integration/common/fixtures/plugins/alerts/server/alert_types.ts index 05f25a462b2821..d2d95f3be92196 100644 --- a/x-pack/test/alerting_api_integration/common/fixtures/plugins/alerts/server/alert_types.ts +++ b/x-pack/test/alerting_api_integration/common/fixtures/plugins/alerts/server/alert_types.ts @@ -8,198 +8,13 @@ import { CoreSetup } from 'kibana/server'; import { schema } from '@kbn/config-schema'; import { times } from 'lodash'; import { FixtureStartDeps, FixtureSetupDeps } from './plugin'; -import { ActionType, ActionTypeExecutorOptions } from '../../../../../../../plugins/actions/server'; import { AlertType, AlertExecutorOptions } from '../../../../../../../plugins/alerting/server'; export function defineAlertTypes( core: CoreSetup, - { actions, alerting }: Pick + { alerting }: Pick ) { const clusterClient = core.elasticsearch.adminClient; - - // Action types - const noopActionType: ActionType = { - id: 'test.noop', - name: 'Test: Noop', - minimumLicenseRequired: 'gold', - async executor() { - return { status: 'ok', actionId: '' }; - }, - }; - const indexRecordActionType: ActionType = { - id: 'test.index-record', - name: 'Test: Index Record', - minimumLicenseRequired: 'gold', - validate: { - params: schema.object({ - index: schema.string(), - reference: schema.string(), - message: schema.string(), - }), - config: schema.object({ - unencrypted: schema.string(), - }), - secrets: schema.object({ - encrypted: schema.string(), - }), - }, - async executor({ config, secrets, params, services, actionId }: ActionTypeExecutorOptions) { - await services.callCluster('index', { - index: params.index, - refresh: 'wait_for', - body: { - params, - config, - secrets, - reference: params.reference, - source: 'action:test.index-record', - }, - }); - return { status: 'ok', actionId }; - }, - }; - const failingActionType: ActionType = { - id: 'test.failing', - name: 'Test: Failing', - minimumLicenseRequired: 'gold', - validate: { - params: schema.object({ - index: schema.string(), - reference: schema.string(), - }), - }, - async executor({ config, secrets, params, services }: ActionTypeExecutorOptions) { - await services.callCluster('index', { - index: params.index, - refresh: 'wait_for', - body: { - params, - config, - secrets, - reference: params.reference, - source: 'action:test.failing', - }, - }); - throw new Error(`expected failure for ${params.index} ${params.reference}`); - }, - }; - const rateLimitedActionType: ActionType = { - id: 'test.rate-limit', - name: 'Test: Rate Limit', - minimumLicenseRequired: 'gold', - maxAttempts: 2, - validate: { - params: schema.object({ - index: schema.string(), - reference: schema.string(), - retryAt: schema.number(), - }), - }, - async executor({ config, params, services }: ActionTypeExecutorOptions) { - await services.callCluster('index', { - index: params.index, - refresh: 'wait_for', - body: { - params, - config, - reference: params.reference, - source: 'action:test.rate-limit', - }, - }); - return { - status: 'error', - retry: new Date(params.retryAt), - actionId: '', - }; - }, - }; - const authorizationActionType: ActionType = { - id: 'test.authorization', - name: 'Test: Authorization', - minimumLicenseRequired: 'gold', - validate: { - params: schema.object({ - callClusterAuthorizationIndex: schema.string(), - savedObjectsClientType: schema.string(), - savedObjectsClientId: schema.string(), - index: schema.string(), - reference: schema.string(), - }), - }, - async executor({ params, services, actionId }: ActionTypeExecutorOptions) { - // Call cluster - let callClusterSuccess = false; - let callClusterError; - try { - await services.callCluster('index', { - index: params.callClusterAuthorizationIndex, - refresh: 'wait_for', - body: { - param1: 'test', - }, - }); - callClusterSuccess = true; - } catch (e) { - callClusterError = e; - } - // Call scoped cluster - const callScopedCluster = services.getScopedCallCluster(clusterClient); - let callScopedClusterSuccess = false; - let callScopedClusterError; - try { - await callScopedCluster('index', { - index: params.callClusterAuthorizationIndex, - refresh: 'wait_for', - body: { - param1: 'test', - }, - }); - callScopedClusterSuccess = true; - } catch (e) { - callScopedClusterError = e; - } - // Saved objects client - let savedObjectsClientSuccess = false; - let savedObjectsClientError; - try { - await services.savedObjectsClient.get( - params.savedObjectsClientType, - params.savedObjectsClientId - ); - savedObjectsClientSuccess = true; - } catch (e) { - savedObjectsClientError = e; - } - // Save the result - await services.callCluster('index', { - index: params.index, - refresh: 'wait_for', - body: { - state: { - callClusterSuccess, - callClusterError, - callScopedClusterSuccess, - callScopedClusterError, - savedObjectsClientSuccess, - savedObjectsClientError, - }, - params, - reference: params.reference, - source: 'action:test.authorization', - }, - }); - return { - actionId, - status: 'ok', - }; - }, - }; - actions.registerType(noopActionType); - actions.registerType(indexRecordActionType); - actions.registerType(failingActionType); - actions.registerType(rateLimitedActionType); - actions.registerType(authorizationActionType); - const alwaysFiringAlertType: AlertType = { id: 'test.always-firing', name: 'Test: Always Firing', diff --git a/x-pack/test/alerting_api_integration/common/fixtures/plugins/alerts/server/plugin.ts b/x-pack/test/alerting_api_integration/common/fixtures/plugins/alerts/server/plugin.ts index 4497254ff3a452..af8dd0282c5782 100644 --- a/x-pack/test/alerting_api_integration/common/fixtures/plugins/alerts/server/plugin.ts +++ b/x-pack/test/alerting_api_integration/common/fixtures/plugins/alerts/server/plugin.ts @@ -10,6 +10,7 @@ import { PluginSetupContract as AlertingPluginSetup } from '../../../../../../.. import { EncryptedSavedObjectsPluginStart } from '../../../../../../../plugins/encrypted_saved_objects/server'; import { PluginSetupContract as FeaturesPluginSetup } from '../../../../../../../plugins/features/server'; import { defineAlertTypes } from './alert_types'; +import { defineActionTypes } from './action_types'; import { defineRoutes } from './routes'; export interface FixtureSetupDeps { @@ -53,7 +54,8 @@ export class FixturePlugin implements Plugin