From 61200d585d36e74fb13634407c6fb5b32ba1b284 Mon Sep 17 00:00:00 2001 From: Yuliia Naumenko Date: Tue, 9 Mar 2021 11:41:26 -0800 Subject: [PATCH 1/9] [Connectors UI] Make UI use new connector APIs --- .../builtin_action_types/jira/api.test.ts | 8 ++++---- .../components/builtin_action_types/jira/api.ts | 8 ++++---- .../builtin_action_types/resilient/api.test.ts | 4 ++-- .../builtin_action_types/resilient/api.ts | 4 ++-- .../builtin_action_types/servicenow/api.test.ts | 2 +- .../builtin_action_types/servicenow/api.ts | 2 +- .../application/lib/action_connector_api.test.ts | 14 +++++++------- .../public/application/lib/action_connector_api.ts | 10 +++++----- 8 files changed, 26 insertions(+), 26 deletions(-) diff --git a/x-pack/plugins/triggers_actions_ui/public/application/components/builtin_action_types/jira/api.test.ts b/x-pack/plugins/triggers_actions_ui/public/application/components/builtin_action_types/jira/api.test.ts index 3ad3c6ce02372..679bc3d53c40d 100644 --- a/x-pack/plugins/triggers_actions_ui/public/application/components/builtin_action_types/jira/api.test.ts +++ b/x-pack/plugins/triggers_actions_ui/public/application/components/builtin_action_types/jira/api.test.ts @@ -102,7 +102,7 @@ describe('Jira API', () => { const res = await getIssueTypes({ http, signal: abortCtrl.signal, connectorId: 'test' }); expect(res).toEqual(issueTypesResponse); - expect(http.post).toHaveBeenCalledWith('/api/actions/action/test/_execute', { + expect(http.post).toHaveBeenCalledWith('/api/actions/connector/test/_execute', { body: '{"params":{"subAction":"issueTypes","subActionParams":{}}}', signal: abortCtrl.signal, }); @@ -121,7 +121,7 @@ describe('Jira API', () => { }); expect(res).toEqual(fieldsResponse); - expect(http.post).toHaveBeenCalledWith('/api/actions/action/test/_execute', { + expect(http.post).toHaveBeenCalledWith('/api/actions/connector/test/_execute', { body: '{"params":{"subAction":"fieldsByIssueType","subActionParams":{"id":"10006"}}}', signal: abortCtrl.signal, }); @@ -140,7 +140,7 @@ describe('Jira API', () => { }); expect(res).toEqual(issuesResponse); - expect(http.post).toHaveBeenCalledWith('/api/actions/action/test/_execute', { + expect(http.post).toHaveBeenCalledWith('/api/actions/connector/test/_execute', { body: '{"params":{"subAction":"issues","subActionParams":{"title":"test issue"}}}', signal: abortCtrl.signal, }); @@ -159,7 +159,7 @@ describe('Jira API', () => { }); expect(res).toEqual(issuesResponse); - expect(http.post).toHaveBeenCalledWith('/api/actions/action/test/_execute', { + expect(http.post).toHaveBeenCalledWith('/api/actions/connector/test/_execute', { body: '{"params":{"subAction":"issue","subActionParams":{"id":"RJ-107"}}}', signal: abortCtrl.signal, }); diff --git a/x-pack/plugins/triggers_actions_ui/public/application/components/builtin_action_types/jira/api.ts b/x-pack/plugins/triggers_actions_ui/public/application/components/builtin_action_types/jira/api.ts index 0184a1f0ca2e5..46ea9dea3aa56 100644 --- a/x-pack/plugins/triggers_actions_ui/public/application/components/builtin_action_types/jira/api.ts +++ b/x-pack/plugins/triggers_actions_ui/public/application/components/builtin_action_types/jira/api.ts @@ -17,7 +17,7 @@ export async function getIssueTypes({ signal: AbortSignal; connectorId: string; }): Promise> { - return await http.post(`${BASE_ACTION_API_PATH}/action/${connectorId}/_execute`, { + return await http.post(`${BASE_ACTION_API_PATH}/connector/${connectorId}/_execute`, { body: JSON.stringify({ params: { subAction: 'issueTypes', subActionParams: {} }, }), @@ -36,7 +36,7 @@ export async function getFieldsByIssueType({ connectorId: string; id: string; }): Promise> { - return await http.post(`${BASE_ACTION_API_PATH}/action/${connectorId}/_execute`, { + return await http.post(`${BASE_ACTION_API_PATH}/connector/${connectorId}/_execute`, { body: JSON.stringify({ params: { subAction: 'fieldsByIssueType', subActionParams: { id } }, }), @@ -55,7 +55,7 @@ export async function getIssues({ connectorId: string; title: string; }): Promise> { - return await http.post(`${BASE_ACTION_API_PATH}/action/${connectorId}/_execute`, { + return await http.post(`${BASE_ACTION_API_PATH}/connector/${connectorId}/_execute`, { body: JSON.stringify({ params: { subAction: 'issues', subActionParams: { title } }, }), @@ -74,7 +74,7 @@ export async function getIssue({ connectorId: string; id: string; }): Promise> { - return await http.post(`${BASE_ACTION_API_PATH}/action/${connectorId}/_execute`, { + return await http.post(`${BASE_ACTION_API_PATH}/connector/${connectorId}/_execute`, { body: JSON.stringify({ params: { subAction: 'issue', subActionParams: { id } }, }), diff --git a/x-pack/plugins/triggers_actions_ui/public/application/components/builtin_action_types/resilient/api.test.ts b/x-pack/plugins/triggers_actions_ui/public/application/components/builtin_action_types/resilient/api.test.ts index 68edff4dc3950..01208f93405d2 100644 --- a/x-pack/plugins/triggers_actions_ui/public/application/components/builtin_action_types/resilient/api.test.ts +++ b/x-pack/plugins/triggers_actions_ui/public/application/components/builtin_action_types/resilient/api.test.ts @@ -61,7 +61,7 @@ describe('Resilient API', () => { }); expect(res).toEqual(incidentTypesResponse); - expect(http.post).toHaveBeenCalledWith('/api/actions/action/test/_execute', { + expect(http.post).toHaveBeenCalledWith('/api/actions/connector/test/_execute', { body: '{"params":{"subAction":"incidentTypes","subActionParams":{}}}', signal: abortCtrl.signal, }); @@ -79,7 +79,7 @@ describe('Resilient API', () => { }); expect(res).toEqual(severityResponse); - expect(http.post).toHaveBeenCalledWith('/api/actions/action/test/_execute', { + expect(http.post).toHaveBeenCalledWith('/api/actions/connector/test/_execute', { body: '{"params":{"subAction":"severity","subActionParams":{}}}', signal: abortCtrl.signal, }); diff --git a/x-pack/plugins/triggers_actions_ui/public/application/components/builtin_action_types/resilient/api.ts b/x-pack/plugins/triggers_actions_ui/public/application/components/builtin_action_types/resilient/api.ts index 46077fcaf6890..8ea3c3c63e50f 100644 --- a/x-pack/plugins/triggers_actions_ui/public/application/components/builtin_action_types/resilient/api.ts +++ b/x-pack/plugins/triggers_actions_ui/public/application/components/builtin_action_types/resilient/api.ts @@ -17,7 +17,7 @@ export async function getIncidentTypes({ signal: AbortSignal; connectorId: string; }): Promise> { - return await http.post(`${BASE_ACTION_API_PATH}/action/${connectorId}/_execute`, { + return await http.post(`${BASE_ACTION_API_PATH}/connector/${connectorId}/_execute`, { body: JSON.stringify({ params: { subAction: 'incidentTypes', subActionParams: {} }, }), @@ -34,7 +34,7 @@ export async function getSeverity({ signal: AbortSignal; connectorId: string; }): Promise> { - return await http.post(`${BASE_ACTION_API_PATH}/action/${connectorId}/_execute`, { + return await http.post(`${BASE_ACTION_API_PATH}/connector/${connectorId}/_execute`, { body: JSON.stringify({ params: { subAction: 'severity', subActionParams: {} }, }), diff --git a/x-pack/plugins/triggers_actions_ui/public/application/components/builtin_action_types/servicenow/api.test.ts b/x-pack/plugins/triggers_actions_ui/public/application/components/builtin_action_types/servicenow/api.test.ts index 9d9b3c5e64909..5c814bbfd6450 100644 --- a/x-pack/plugins/triggers_actions_ui/public/application/components/builtin_action_types/servicenow/api.test.ts +++ b/x-pack/plugins/triggers_actions_ui/public/application/components/builtin_action_types/servicenow/api.test.ts @@ -61,7 +61,7 @@ describe('ServiceNow API', () => { }); expect(res).toEqual(choicesResponse); - expect(http.post).toHaveBeenCalledWith('/api/actions/action/test/_execute', { + expect(http.post).toHaveBeenCalledWith('/api/actions/connector/test/_execute', { body: '{"params":{"subAction":"getChoices","subActionParams":{"fields":["priority"]}}}', signal: abortCtrl.signal, }); diff --git a/x-pack/plugins/triggers_actions_ui/public/application/components/builtin_action_types/servicenow/api.ts b/x-pack/plugins/triggers_actions_ui/public/application/components/builtin_action_types/servicenow/api.ts index 3e92515e49b49..bb90915591285 100644 --- a/x-pack/plugins/triggers_actions_ui/public/application/components/builtin_action_types/servicenow/api.ts +++ b/x-pack/plugins/triggers_actions_ui/public/application/components/builtin_action_types/servicenow/api.ts @@ -19,7 +19,7 @@ export async function getChoices({ connectorId: string; fields: string[]; }): Promise> { - return await http.post(`${BASE_ACTION_API_PATH}/action/${connectorId}/_execute`, { + return await http.post(`${BASE_ACTION_API_PATH}/connector/${connectorId}/_execute`, { body: JSON.stringify({ params: { subAction: 'getChoices', subActionParams: { fields } }, }), diff --git a/x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api.test.ts b/x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api.test.ts index bf70e4c5f2408..67307228c57f0 100644 --- a/x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api.test.ts +++ b/x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api.test.ts @@ -38,7 +38,7 @@ describe('loadActionTypes', () => { expect(result).toEqual(resolvedValue); expect(http.get.mock.calls[0]).toMatchInlineSnapshot(` Array [ - "/api/actions/list_action_types", + "/api/actions/list_connector_types", ] `); }); @@ -74,7 +74,7 @@ describe('createActionConnector', () => { expect(result).toEqual(resolvedValue); expect(http.post.mock.calls[0]).toMatchInlineSnapshot(` Array [ - "/api/actions/action", + "/api/actions/connector", Object { "body": "{\\"actionTypeId\\":\\"test\\",\\"isPreconfigured\\":false,\\"name\\":\\"My test\\",\\"config\\":{},\\"secrets\\":{}}", }, @@ -100,7 +100,7 @@ describe('updateActionConnector', () => { expect(result).toEqual(resolvedValue); expect(http.put.mock.calls[0]).toMatchInlineSnapshot(` Array [ - "/api/actions/action/123", + "/api/actions/connector/123", Object { "body": "{\\"name\\":\\"My test\\",\\"config\\":{},\\"secrets\\":{}}", }, @@ -118,13 +118,13 @@ describe('deleteActions', () => { expect(http.delete.mock.calls).toMatchInlineSnapshot(` Array [ Array [ - "/api/actions/action/1", + "/api/actions/connector/1", ], Array [ - "/api/actions/action/2", + "/api/actions/connector/2", ], Array [ - "/api/actions/action/3", + "/api/actions/connector/3", ], ] `); @@ -151,7 +151,7 @@ describe('executeAction', () => { }); expect(http.post.mock.calls[0]).toMatchInlineSnapshot(` Array [ - "/api/actions/action/123/_execute", + "/api/actions/connector/123/_execute", Object { "body": "{\\"params\\":{\\"stringParams\\":\\"someString\\",\\"numericParams\\":123}}", }, diff --git a/x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api.ts b/x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api.ts index 57fb079d97299..3893f12c98ba5 100644 --- a/x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api.ts +++ b/x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api.ts @@ -11,7 +11,7 @@ import type { ActionConnector, ActionConnectorWithoutId, ActionType } from '../. import { ActionTypeExecutorResult } from '../../../../../plugins/actions/common'; export async function loadActionTypes({ http }: { http: HttpSetup }): Promise { - return await http.get(`${BASE_ACTION_API_PATH}/list_action_types`); + return await http.get(`${BASE_ACTION_API_PATH}/list_connector_types`); } export async function loadAllActions({ http }: { http: HttpSetup }): Promise { @@ -25,7 +25,7 @@ export async function createActionConnector({ http: HttpSetup; connector: Omit; }): Promise { - return await http.post(`${BASE_ACTION_API_PATH}/action`, { + return await http.post(`${BASE_ACTION_API_PATH}/connector`, { body: JSON.stringify(connector), }); } @@ -39,7 +39,7 @@ export async function updateActionConnector({ connector: Pick; id: string; }): Promise { - return await http.put(`${BASE_ACTION_API_PATH}/action/${id}`, { + return await http.put(`${BASE_ACTION_API_PATH}/connector/${id}`, { body: JSON.stringify({ name: connector.name, config: connector.config, @@ -57,7 +57,7 @@ export async function deleteActions({ }): Promise<{ successes: string[]; errors: string[] }> { const successes: string[] = []; const errors: string[] = []; - await Promise.all(ids.map((id) => http.delete(`${BASE_ACTION_API_PATH}/action/${id}`))).then( + await Promise.all(ids.map((id) => http.delete(`${BASE_ACTION_API_PATH}/connector/${id}`))).then( function (fulfilled) { successes.push(...fulfilled); }, @@ -77,7 +77,7 @@ export async function executeAction({ http: HttpSetup; params: Record; }): Promise> { - return http.post(`${BASE_ACTION_API_PATH}/action/${id}/_execute`, { + return http.post(`${BASE_ACTION_API_PATH}/connector/${id}/_execute`, { body: JSON.stringify({ params }), }); } From 04054260059da3842b4849a61e8b1a8c07a53427 Mon Sep 17 00:00:00 2001 From: Yuliia Naumenko Date: Tue, 9 Mar 2021 12:47:04 -0800 Subject: [PATCH 2/9] fixed tests --- .../apps/triggers_actions_ui/connectors.ts | 2 +- .../functional_with_es_ssl/apps/triggers_actions_ui/details.ts | 2 +- .../apps/triggers_actions_ui/home_page.ts | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/x-pack/test/functional_with_es_ssl/apps/triggers_actions_ui/connectors.ts b/x-pack/test/functional_with_es_ssl/apps/triggers_actions_ui/connectors.ts index 7d43001bd0374..e40c821d98851 100644 --- a/x-pack/test/functional_with_es_ssl/apps/triggers_actions_ui/connectors.ts +++ b/x-pack/test/functional_with_es_ssl/apps/triggers_actions_ui/connectors.ts @@ -23,7 +23,7 @@ export default ({ getPageObjects, getService }: FtrProviderContext) => { before(async () => { const { body: createdAction } = await supertest - .post(`/api/actions/action`) + .post(`/api/actions/connector`) .set('kbn-xsrf', 'foo') .send(getTestActionData()) .expect(200); diff --git a/x-pack/test/functional_with_es_ssl/apps/triggers_actions_ui/details.ts b/x-pack/test/functional_with_es_ssl/apps/triggers_actions_ui/details.ts index 0017f7ba04b87..9f01d5e698087 100644 --- a/x-pack/test/functional_with_es_ssl/apps/triggers_actions_ui/details.ts +++ b/x-pack/test/functional_with_es_ssl/apps/triggers_actions_ui/details.ts @@ -27,7 +27,7 @@ export default ({ getPageObjects, getService }: FtrProviderContext) => { async function createActionManualCleanup(overwrites: Record = {}) { const { body: createdAction } = await supertest - .post(`/api/actions/action`) + .post(`/api/actions/connector`) .set('kbn-xsrf', 'foo') .send(getTestActionData(overwrites)) .expect(200); diff --git a/x-pack/test/functional_with_es_ssl/apps/triggers_actions_ui/home_page.ts b/x-pack/test/functional_with_es_ssl/apps/triggers_actions_ui/home_page.ts index 6c23a04a0a8d2..fc783d669694d 100644 --- a/x-pack/test/functional_with_es_ssl/apps/triggers_actions_ui/home_page.ts +++ b/x-pack/test/functional_with_es_ssl/apps/triggers_actions_ui/home_page.ts @@ -84,7 +84,7 @@ export default ({ getPageObjects, getService }: FtrProviderContext) => { it('navigates to an alert details page', async () => { const { body: createdAction } = await supertest - .post(`/api/actions/action`) + .post(`/api/actions/connector`) .set('kbn-xsrf', 'foo') .send(getTestActionData()) .expect(200); From 642780994bc73a87401fa863e1cc2e082cda7a61 Mon Sep 17 00:00:00 2001 From: Yuliia Naumenko Date: Wed, 10 Mar 2021 10:17:23 -0800 Subject: [PATCH 3/9] fixed due to comment --- .../public/application/lib/action_connector_api.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api.test.ts b/x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api.test.ts index 67307228c57f0..4f669963d45c0 100644 --- a/x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api.test.ts +++ b/x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api.test.ts @@ -38,7 +38,7 @@ describe('loadActionTypes', () => { expect(result).toEqual(resolvedValue); expect(http.get.mock.calls[0]).toMatchInlineSnapshot(` Array [ - "/api/actions/list_connector_types", + "/api/actions/connector_types", ] `); }); From b21ac0ec231730cb09becb4d8665a87fe03017d8 Mon Sep 17 00:00:00 2001 From: Yuliia Naumenko Date: Wed, 10 Mar 2021 16:53:15 -0800 Subject: [PATCH 4/9] fixed due to comment --- .../application/lib/action_connector_api.ts | 83 ------------------- .../action_connector_api.test.ts | 49 ++++++++--- .../action_connector_api/connector_types.ts | 35 ++++++++ .../lib/action_connector_api/connectors.ts | 41 +++++++++ .../lib/action_connector_api/create.ts | 47 +++++++++++ .../lib/action_connector_api/delete.ts | 28 +++++++ .../lib/action_connector_api/execute.ts | 37 +++++++++ .../lib/action_connector_api/index.ts | 13 +++ .../lib/action_connector_api/update.ts | 45 ++++++++++ .../triggers_actions_ui/public/types.ts | 2 +- .../apps/triggers_actions_ui/alerts_list.ts | 2 +- 11 files changed, 287 insertions(+), 95 deletions(-) delete mode 100644 x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api.ts rename x-pack/plugins/triggers_actions_ui/public/application/lib/{ => action_connector_api}/action_connector_api.test.ts (77%) create mode 100644 x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api/connector_types.ts create mode 100644 x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api/connectors.ts create mode 100644 x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api/create.ts create mode 100644 x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api/delete.ts create mode 100644 x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api/execute.ts create mode 100644 x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api/index.ts create mode 100644 x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api/update.ts diff --git a/x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api.ts b/x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api.ts deleted file mode 100644 index 3893f12c98ba5..0000000000000 --- a/x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api.ts +++ /dev/null @@ -1,83 +0,0 @@ -/* - * 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 { HttpSetup } from 'kibana/public'; -import { BASE_ACTION_API_PATH } from '../constants'; -import type { ActionConnector, ActionConnectorWithoutId, ActionType } from '../../types'; -import { ActionTypeExecutorResult } from '../../../../../plugins/actions/common'; - -export async function loadActionTypes({ http }: { http: HttpSetup }): Promise { - return await http.get(`${BASE_ACTION_API_PATH}/list_connector_types`); -} - -export async function loadAllActions({ http }: { http: HttpSetup }): Promise { - return await http.get(`${BASE_ACTION_API_PATH}`); -} - -export async function createActionConnector({ - http, - connector, -}: { - http: HttpSetup; - connector: Omit; -}): Promise { - return await http.post(`${BASE_ACTION_API_PATH}/connector`, { - body: JSON.stringify(connector), - }); -} - -export async function updateActionConnector({ - http, - connector, - id, -}: { - http: HttpSetup; - connector: Pick; - id: string; -}): Promise { - return await http.put(`${BASE_ACTION_API_PATH}/connector/${id}`, { - body: JSON.stringify({ - name: connector.name, - config: connector.config, - secrets: connector.secrets, - }), - }); -} - -export async function deleteActions({ - ids, - http, -}: { - ids: string[]; - http: HttpSetup; -}): Promise<{ successes: string[]; errors: string[] }> { - const successes: string[] = []; - const errors: string[] = []; - await Promise.all(ids.map((id) => http.delete(`${BASE_ACTION_API_PATH}/connector/${id}`))).then( - function (fulfilled) { - successes.push(...fulfilled); - }, - function (rejected) { - errors.push(...rejected); - } - ); - return { successes, errors }; -} - -export async function executeAction({ - id, - params, - http, -}: { - id: string; - http: HttpSetup; - params: Record; -}): Promise> { - return http.post(`${BASE_ACTION_API_PATH}/connector/${id}/_execute`, { - body: JSON.stringify({ params }), - }); -} diff --git a/x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api.test.ts b/x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api/action_connector_api.test.ts similarity index 77% rename from x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api.test.ts rename to x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api/action_connector_api.test.ts index 4f669963d45c0..92914dacd4e9b 100644 --- a/x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api.test.ts +++ b/x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api/action_connector_api.test.ts @@ -5,8 +5,8 @@ * 2.0. */ -import { ActionConnector, ActionConnectorWithoutId, ActionType } from '../../types'; -import { httpServiceMock } from '../../../../../../src/core/public/mocks'; +import { ActionConnector, ActionConnectorWithoutId, ActionType } from '../../../types'; +import { httpServiceMock } from '../../../../../../../src/core/public/mocks'; import { createActionConnector, deleteActions, @@ -14,7 +14,7 @@ import { loadAllActions, updateActionConnector, executeAction, -} from './action_connector_api'; +} from './index'; const http = httpServiceMock.createStartContract(); @@ -22,6 +22,18 @@ beforeEach(() => jest.resetAllMocks()); describe('loadActionTypes', () => { test('should call get types API', async () => { + const apiResponseValue = [ + { + id: 'test', + name: 'Test', + enabled: true, + enabled_in_config: true, + enabled_in_license: true, + minimum_license_required: 'basic', + }, + ]; + http.get.mockResolvedValueOnce(apiResponseValue); + const resolvedValue: ActionType[] = [ { id: 'test', @@ -32,7 +44,6 @@ describe('loadActionTypes', () => { minimumLicenseRequired: 'basic', }, ]; - http.get.mockResolvedValueOnce(resolvedValue); const result = await loadActionTypes({ http }); expect(result).toEqual(resolvedValue); @@ -52,7 +63,7 @@ describe('loadAllActions', () => { expect(result).toEqual([]); expect(http.get.mock.calls[0]).toMatchInlineSnapshot(` Array [ - "/api/actions", + "/api/actions/connectors", ] `); }); @@ -60,6 +71,16 @@ describe('loadAllActions', () => { describe('createActionConnector', () => { test('should call create action API', async () => { + const apiResponse = { + connector_type_id: 'test', + is_preconfigured: false, + name: 'My test', + config: {}, + secrets: {}, + id: '123', + }; + http.post.mockResolvedValueOnce(apiResponse); + const connector: ActionConnectorWithoutId<{}, {}> = { actionTypeId: 'test', isPreconfigured: false, @@ -67,8 +88,7 @@ describe('createActionConnector', () => { config: {}, secrets: {}, }; - const resolvedValue: ActionConnector = { ...connector, id: '123' }; - http.post.mockResolvedValueOnce(resolvedValue); + const resolvedValue = { ...connector, id: '123' }; const result = await createActionConnector({ http, connector }); expect(result).toEqual(resolvedValue); @@ -76,7 +96,7 @@ describe('createActionConnector', () => { Array [ "/api/actions/connector", Object { - "body": "{\\"actionTypeId\\":\\"test\\",\\"isPreconfigured\\":false,\\"name\\":\\"My test\\",\\"config\\":{},\\"secrets\\":{}}", + "body": "{\\"name\\":\\"My test\\",\\"config\\":{},\\"secrets\\":{},\\"connector_type_id\\":\\"test\\",\\"is_preconfigured\\":false}", }, ] `); @@ -86,6 +106,16 @@ describe('createActionConnector', () => { describe('updateActionConnector', () => { test('should call the update API', async () => { const id = '123'; + const apiResponse = { + connector_type_id: 'test', + is_preconfigured: false, + name: 'My test', + config: {}, + secrets: {}, + id, + }; + http.put.mockResolvedValueOnce(apiResponse); + const connector: ActionConnectorWithoutId<{}, {}> = { actionTypeId: 'test', isPreconfigured: false, @@ -94,7 +124,6 @@ describe('updateActionConnector', () => { secrets: {}, }; const resolvedValue = { ...connector, id }; - http.put.mockResolvedValueOnce(resolvedValue); const result = await updateActionConnector({ http, connector, id }); expect(result).toEqual(resolvedValue); @@ -140,7 +169,7 @@ describe('executeAction', () => { }; http.post.mockResolvedValueOnce({ - actionId: id, + connector_id: id, status: 'ok', }); diff --git a/x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api/connector_types.ts b/x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api/connector_types.ts new file mode 100644 index 0000000000000..99fb0d24b487c --- /dev/null +++ b/x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api/connector_types.ts @@ -0,0 +1,35 @@ +/* + * 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 { HttpSetup } from 'kibana/public'; +import { + AsApiContract, + RewriteRequestCase, + // eslint-disable-next-line @kbn/eslint/no-restricted-paths +} from '../../../../../actions/server/routes/rewrite_request_case'; +import { BASE_ACTION_API_PATH } from '../../constants'; +import type { ActionType } from '../../../types'; + +const rewriteResponseRes = (results: Array>): ActionType[] => { + return results.map((item) => rewriteBodyReq(item)); +}; + +const rewriteBodyReq: RewriteRequestCase = ({ + enabled_in_config: enabledInConfig, + enabled_in_license: enabledInLicense, + minimum_license_required: minimumLicenseRequired, + ...res +}: AsApiContract) => ({ + enabledInConfig, + enabledInLicense, + minimumLicenseRequired, + ...res, +}); + +export async function loadActionTypes({ http }: { http: HttpSetup }): Promise { + const res = await http.get(`${BASE_ACTION_API_PATH}/connector_types`); + return rewriteResponseRes(res); +} diff --git a/x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api/connectors.ts b/x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api/connectors.ts new file mode 100644 index 0000000000000..0dbf9726d1db1 --- /dev/null +++ b/x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api/connectors.ts @@ -0,0 +1,41 @@ +/* + * 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 { HttpSetup } from 'kibana/public'; +import { + AsApiContract, + RewriteRequestCase, + // eslint-disable-next-line @kbn/eslint/no-restricted-paths +} from '../../../../../actions/server/routes/rewrite_request_case'; +import { BASE_ACTION_API_PATH } from '../../constants'; +import type { ActionConnector, ActionConnectorProps } from '../../../types'; + +const rewriteResponseRes = ( + results: Array< + AsApiContract, Record>> + > +): Array, Record>> => { + return results.map((item) => transformConnector(item)); +}; + +const transformConnector: RewriteRequestCase< + ActionConnectorProps, Record> +> = ({ + connector_type_id: actionTypeId, + is_preconfigured: isPreconfigured, + referenced_by_count: referencedByCount, + ...res +}) => ({ + actionTypeId, + isPreconfigured, + referencedByCount, + ...res, +}); + +export async function loadAllActions({ http }: { http: HttpSetup }): Promise { + const res = await http.get(`${BASE_ACTION_API_PATH}/connectors`); + return rewriteResponseRes(res); +} diff --git a/x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api/create.ts b/x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api/create.ts new file mode 100644 index 0000000000000..4daf8dd59c70a --- /dev/null +++ b/x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api/create.ts @@ -0,0 +1,47 @@ +/* + * 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 { HttpSetup } from 'kibana/public'; +import { + RewriteRequestCase, + RewriteResponseCase, + // eslint-disable-next-line @kbn/eslint/no-restricted-paths +} from '../../../../../actions/server/routes/rewrite_request_case'; +import { BASE_ACTION_API_PATH } from '../../constants'; +import type { + ActionConnector, + ActionConnectorProps, + ActionConnectorWithoutId, +} from '../../../types'; + +const rewriteBodyRequest: RewriteResponseCase< + Omit +> = ({ actionTypeId, isPreconfigured, ...res }) => ({ + ...res, + connector_type_id: actionTypeId, + is_preconfigured: isPreconfigured, +}); + +const rewriteBodyRes: RewriteRequestCase< + ActionConnectorProps, Record> +> = ({ connector_type_id: actionTypeId, is_preconfigured: isPreconfigured, ...res }) => ({ + ...res, + actionTypeId, + isPreconfigured, +}); + +export async function createActionConnector({ + http, + connector, +}: { + http: HttpSetup; + connector: Omit; +}): Promise { + const res = await http.post(`${BASE_ACTION_API_PATH}/connector`, { + body: JSON.stringify(rewriteBodyRequest(connector)), + }); + return rewriteBodyRes(res); +} diff --git a/x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api/delete.ts b/x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api/delete.ts new file mode 100644 index 0000000000000..c9c25db676a06 --- /dev/null +++ b/x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api/delete.ts @@ -0,0 +1,28 @@ +/* + * 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 { HttpSetup } from 'kibana/public'; +import { BASE_ACTION_API_PATH } from '../../constants'; + +export async function deleteActions({ + ids, + http, +}: { + ids: string[]; + http: HttpSetup; +}): Promise<{ successes: string[]; errors: string[] }> { + const successes: string[] = []; + const errors: string[] = []; + await Promise.all(ids.map((id) => http.delete(`${BASE_ACTION_API_PATH}/connector/${id}`))).then( + function (fulfilled) { + successes.push(...fulfilled); + }, + function (rejected) { + errors.push(...rejected); + } + ); + return { successes, errors }; +} diff --git a/x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api/execute.ts b/x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api/execute.ts new file mode 100644 index 0000000000000..f6cf6b4c37634 --- /dev/null +++ b/x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api/execute.ts @@ -0,0 +1,37 @@ +/* + * 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 { HttpSetup } from 'kibana/public'; +// eslint-disable-next-line @kbn/eslint/no-restricted-paths +import { RewriteRequestCase } from '../../../../../actions/server/routes/rewrite_request_case'; +import { ActionTypeExecutorResult } from '../../../../../../plugins/actions/common'; +import { BASE_ACTION_API_PATH } from '../../constants'; + +const rewriteBodyRes: RewriteRequestCase> = ({ + connector_id: actionId, + service_message: serviceMessage, + ...res +}) => ({ + ...res, + actionId, + serviceMessage, +}); + +export async function executeAction({ + id, + params, + http, +}: { + id: string; + http: HttpSetup; + params: Record; +}): Promise> { + const res = await http.post(`${BASE_ACTION_API_PATH}/connector/${id}/_execute`, { + body: JSON.stringify({ params }), + }); + return rewriteBodyRes(res); +} diff --git a/x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api/index.ts b/x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api/index.ts new file mode 100644 index 0000000000000..7cc4f1df6a735 --- /dev/null +++ b/x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api/index.ts @@ -0,0 +1,13 @@ +/* + * 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. + */ + +export { loadActionTypes } from './connector_types'; +export { loadAllActions } from './connectors'; +export { createActionConnector } from './create'; +export { deleteActions } from './delete'; +export { executeAction } from './execute'; +export { updateActionConnector } from './update'; diff --git a/x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api/update.ts b/x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api/update.ts new file mode 100644 index 0000000000000..8d97f7f4a213b --- /dev/null +++ b/x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api/update.ts @@ -0,0 +1,45 @@ +/* + * 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 { HttpSetup } from 'kibana/public'; +import { + RewriteRequestCase, + // eslint-disable-next-line @kbn/eslint/no-restricted-paths +} from '../../../../../actions/server/routes/rewrite_request_case'; +import { BASE_ACTION_API_PATH } from '../../constants'; +import type { + ActionConnector, + ActionConnectorProps, + ActionConnectorWithoutId, +} from '../../../types'; + +const rewriteBodyRes: RewriteRequestCase< + ActionConnectorProps, Record> +> = ({ connector_type_id: actionTypeId, is_preconfigured: isPreconfigured, ...res }) => ({ + ...res, + actionTypeId, + isPreconfigured, +}); + +export async function updateActionConnector({ + http, + connector, + id, +}: { + http: HttpSetup; + connector: Pick; + id: string; +}): Promise { + const res = await http.put(`${BASE_ACTION_API_PATH}/connector/${id}`, { + body: JSON.stringify({ + name: connector.name, + config: connector.config, + secrets: connector.secrets, + }), + }); + + return rewriteBodyRes(res); +} diff --git a/x-pack/plugins/triggers_actions_ui/public/types.ts b/x-pack/plugins/triggers_actions_ui/public/types.ts index 5c5dcf344b10b..cf2dda203bb2d 100644 --- a/x-pack/plugins/triggers_actions_ui/public/types.ts +++ b/x-pack/plugins/triggers_actions_ui/public/types.ts @@ -121,7 +121,7 @@ export interface ConnectorValidationResult { secrets?: GenericValidationResult; } -interface ActionConnectorProps { +export interface ActionConnectorProps { secrets: Secrets; id: string; actionTypeId: string; diff --git a/x-pack/test/functional_with_es_ssl/apps/triggers_actions_ui/alerts_list.ts b/x-pack/test/functional_with_es_ssl/apps/triggers_actions_ui/alerts_list.ts index 829fd9c85cbf0..6bfcef451aa09 100644 --- a/x-pack/test/functional_with_es_ssl/apps/triggers_actions_ui/alerts_list.ts +++ b/x-pack/test/functional_with_es_ssl/apps/triggers_actions_ui/alerts_list.ts @@ -42,7 +42,7 @@ export default ({ getPageObjects, getService }: FtrProviderContext) => { async function createAction(overwrites: Record = {}) { const { body: createdAction } = await supertest - .post(`/api/actions/action`) + .post(`/api/actions/connector`) .set('kbn-xsrf', 'foo') .send(getTestActionData(overwrites)) .expect(200); From 89cdc8b1f7d952e7b9a94e826a7d62ecabf2443d Mon Sep 17 00:00:00 2001 From: Yuliia Naumenko Date: Wed, 10 Mar 2021 17:14:47 -0800 Subject: [PATCH 5/9] fixed test --- .../lib/action_connector_api/action_connector_api.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api/action_connector_api.test.ts b/x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api/action_connector_api.test.ts index 92914dacd4e9b..372a22c76c1fa 100644 --- a/x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api/action_connector_api.test.ts +++ b/x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api/action_connector_api.test.ts @@ -5,7 +5,7 @@ * 2.0. */ -import { ActionConnector, ActionConnectorWithoutId, ActionType } from '../../../types'; +import { ActionConnectorWithoutId, ActionType } from '../../../types'; import { httpServiceMock } from '../../../../../../../src/core/public/mocks'; import { createActionConnector, From 77c003c720c5e38633c9741de897be1c1a7224f7 Mon Sep 17 00:00:00 2001 From: Yuliia Naumenko Date: Wed, 10 Mar 2021 19:36:48 -0800 Subject: [PATCH 6/9] fixed test --- x-pack/test/functional_with_es_ssl/lib/get_test_data.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x-pack/test/functional_with_es_ssl/lib/get_test_data.ts b/x-pack/test/functional_with_es_ssl/lib/get_test_data.ts index a93c9987fd640..11ccd15571259 100644 --- a/x-pack/test/functional_with_es_ssl/lib/get_test_data.ts +++ b/x-pack/test/functional_with_es_ssl/lib/get_test_data.ts @@ -30,7 +30,7 @@ export function getTestAlertData(overwrites = {}) { export function getTestActionData(overwrites = {}) { return { name: `slack-${Date.now()}`, - actionTypeId: '.slack', + connector_type_id: '.slack', config: {}, secrets: { webhookUrl: 'https://test', From 8670ea3e1a3980442abc310496b1dcc09e353c08 Mon Sep 17 00:00:00 2001 From: Yuliia Naumenko Date: Thu, 11 Mar 2021 07:55:13 -0800 Subject: [PATCH 7/9] fixed test --- .../functional_with_es_ssl/apps/triggers_actions_ui/details.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x-pack/test/functional_with_es_ssl/apps/triggers_actions_ui/details.ts b/x-pack/test/functional_with_es_ssl/apps/triggers_actions_ui/details.ts index 9f01d5e698087..9b1a78e772503 100644 --- a/x-pack/test/functional_with_es_ssl/apps/triggers_actions_ui/details.ts +++ b/x-pack/test/functional_with_es_ssl/apps/triggers_actions_ui/details.ts @@ -372,7 +372,7 @@ export default ({ getPageObjects, getService }: FtrProviderContext) => { it('should show and update deleted connectors when there are no existing connectors of the same type', async () => { const action = await createActionManualCleanup({ name: `index-${testRunUuid}-${0}`, - actionTypeId: '.index', + connector_type_id: '.index', config: { index: `index-${testRunUuid}-${0}`, }, From 5c1aca4e3f714614515c6f8aafd0d474cf8a8e6d Mon Sep 17 00:00:00 2001 From: Yuliia Naumenko Date: Mon, 15 Mar 2021 12:31:47 -0700 Subject: [PATCH 8/9] moved rewrite_request_case to the common folder --- x-pack/plugins/actions/common/index.ts | 2 ++ .../{server/routes => common}/rewrite_request_case.ts | 0 x-pack/plugins/actions/server/routes/connector_types.ts | 3 +-- x-pack/plugins/actions/server/routes/create.ts | 3 +-- x-pack/plugins/actions/server/routes/execute.ts | 3 +-- x-pack/plugins/actions/server/routes/get.ts | 3 +-- x-pack/plugins/actions/server/routes/get_all.ts | 3 +-- x-pack/plugins/actions/server/routes/update.ts | 3 +-- .../lib/action_connector_api/connector_types.ts | 7 ++----- .../application/lib/action_connector_api/connectors.ts | 6 +----- .../public/application/lib/action_connector_api/create.ts | 6 +----- .../public/application/lib/action_connector_api/execute.ts | 7 ++++--- .../public/application/lib/action_connector_api/update.ts | 5 +---- 13 files changed, 17 insertions(+), 34 deletions(-) rename x-pack/plugins/actions/{server/routes => common}/rewrite_request_case.ts (100%) diff --git a/x-pack/plugins/actions/common/index.ts b/x-pack/plugins/actions/common/index.ts index f0e1439bce3e3..184ae9c226b8f 100644 --- a/x-pack/plugins/actions/common/index.ts +++ b/x-pack/plugins/actions/common/index.ts @@ -8,3 +8,5 @@ export * from './types'; export const BASE_ACTION_API_PATH = '/api/actions'; + +export * from './rewrite_request_case'; diff --git a/x-pack/plugins/actions/server/routes/rewrite_request_case.ts b/x-pack/plugins/actions/common/rewrite_request_case.ts similarity index 100% rename from x-pack/plugins/actions/server/routes/rewrite_request_case.ts rename to x-pack/plugins/actions/common/rewrite_request_case.ts diff --git a/x-pack/plugins/actions/server/routes/connector_types.ts b/x-pack/plugins/actions/server/routes/connector_types.ts index d686ddbdaee70..9f9ad5b2aea68 100644 --- a/x-pack/plugins/actions/server/routes/connector_types.ts +++ b/x-pack/plugins/actions/server/routes/connector_types.ts @@ -7,10 +7,9 @@ import { IRouter } from 'kibana/server'; import { ILicenseState } from '../lib'; -import { ActionType, BASE_ACTION_API_PATH } from '../../common'; +import { ActionType, BASE_ACTION_API_PATH, RewriteResponseCase } from '../../common'; import { ActionsRequestHandlerContext } from '../types'; import { verifyAccessAndContext } from './verify_access_and_context'; -import { RewriteResponseCase } from './rewrite_request_case'; const rewriteBodyRes: RewriteResponseCase = (results) => { return results.map(({ enabledInConfig, enabledInLicense, minimumLicenseRequired, ...res }) => ({ diff --git a/x-pack/plugins/actions/server/routes/create.ts b/x-pack/plugins/actions/server/routes/create.ts index e1717891231db..c05f2180bd62b 100644 --- a/x-pack/plugins/actions/server/routes/create.ts +++ b/x-pack/plugins/actions/server/routes/create.ts @@ -9,9 +9,8 @@ import { schema } from '@kbn/config-schema'; import { IRouter } from 'kibana/server'; import { ActionResult, ActionsRequestHandlerContext } from '../types'; import { ILicenseState } from '../lib'; -import { BASE_ACTION_API_PATH } from '../../common'; +import { BASE_ACTION_API_PATH, RewriteRequestCase, RewriteResponseCase } from '../../common'; import { verifyAccessAndContext } from './verify_access_and_context'; -import { RewriteRequestCase, RewriteResponseCase } from './rewrite_request_case'; import { CreateOptions } from '../actions_client'; export const bodySchema = schema.object({ diff --git a/x-pack/plugins/actions/server/routes/execute.ts b/x-pack/plugins/actions/server/routes/execute.ts index 0d1bee83ed047..377fe1215b3fb 100644 --- a/x-pack/plugins/actions/server/routes/execute.ts +++ b/x-pack/plugins/actions/server/routes/execute.ts @@ -10,10 +10,9 @@ import { IRouter } from 'kibana/server'; import { ILicenseState } from '../lib'; import { ActionTypeExecutorResult, ActionsRequestHandlerContext } from '../types'; -import { BASE_ACTION_API_PATH } from '../../common'; +import { BASE_ACTION_API_PATH, RewriteResponseCase } from '../../common'; import { asHttpRequestExecutionSource } from '../lib/action_execution_source'; import { verifyAccessAndContext } from './verify_access_and_context'; -import { RewriteResponseCase } from './rewrite_request_case'; const paramSchema = schema.object({ id: schema.string(), diff --git a/x-pack/plugins/actions/server/routes/get.ts b/x-pack/plugins/actions/server/routes/get.ts index 63f89d6b3ca49..59766fc133ba6 100644 --- a/x-pack/plugins/actions/server/routes/get.ts +++ b/x-pack/plugins/actions/server/routes/get.ts @@ -8,10 +8,9 @@ import { schema } from '@kbn/config-schema'; import { IRouter } from 'kibana/server'; import { ILicenseState } from '../lib'; -import { BASE_ACTION_API_PATH } from '../../common'; +import { BASE_ACTION_API_PATH, RewriteResponseCase } from '../../common'; import { ActionResult, ActionsRequestHandlerContext } from '../types'; import { verifyAccessAndContext } from './verify_access_and_context'; -import { RewriteResponseCase } from './rewrite_request_case'; const paramSchema = schema.object({ id: schema.string(), diff --git a/x-pack/plugins/actions/server/routes/get_all.ts b/x-pack/plugins/actions/server/routes/get_all.ts index 32f48e32ab278..831722fd36eed 100644 --- a/x-pack/plugins/actions/server/routes/get_all.ts +++ b/x-pack/plugins/actions/server/routes/get_all.ts @@ -7,10 +7,9 @@ import { IRouter } from 'kibana/server'; import { ILicenseState } from '../lib'; -import { BASE_ACTION_API_PATH } from '../../common'; +import { BASE_ACTION_API_PATH, RewriteResponseCase } from '../../common'; import { ActionsRequestHandlerContext, FindActionResult } from '../types'; import { verifyAccessAndContext } from './verify_access_and_context'; -import { RewriteResponseCase } from './rewrite_request_case'; const rewriteBodyRes: RewriteResponseCase = (results) => { return results.map(({ actionTypeId, isPreconfigured, referencedByCount, ...res }) => ({ diff --git a/x-pack/plugins/actions/server/routes/update.ts b/x-pack/plugins/actions/server/routes/update.ts index af55fa32b76ca..d1758717e80f9 100644 --- a/x-pack/plugins/actions/server/routes/update.ts +++ b/x-pack/plugins/actions/server/routes/update.ts @@ -8,10 +8,9 @@ import { schema } from '@kbn/config-schema'; import { IRouter } from 'kibana/server'; import { ILicenseState } from '../lib'; -import { BASE_ACTION_API_PATH } from '../../common'; +import { BASE_ACTION_API_PATH, RewriteResponseCase } from '../../common'; import { ActionResult, ActionsRequestHandlerContext } from '../types'; import { verifyAccessAndContext } from './verify_access_and_context'; -import { RewriteResponseCase } from './rewrite_request_case'; const paramSchema = schema.object({ id: schema.string(), diff --git a/x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api/connector_types.ts b/x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api/connector_types.ts index 99fb0d24b487c..6f7e8b03658e0 100644 --- a/x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api/connector_types.ts +++ b/x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api/connector_types.ts @@ -5,11 +5,8 @@ * 2.0. */ import { HttpSetup } from 'kibana/public'; -import { - AsApiContract, - RewriteRequestCase, - // eslint-disable-next-line @kbn/eslint/no-restricted-paths -} from '../../../../../actions/server/routes/rewrite_request_case'; + +import { AsApiContract, RewriteRequestCase } from '../../../../../actions/common'; import { BASE_ACTION_API_PATH } from '../../constants'; import type { ActionType } from '../../../types'; diff --git a/x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api/connectors.ts b/x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api/connectors.ts index 0dbf9726d1db1..cf424ea1e7317 100644 --- a/x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api/connectors.ts +++ b/x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api/connectors.ts @@ -5,11 +5,7 @@ * 2.0. */ import { HttpSetup } from 'kibana/public'; -import { - AsApiContract, - RewriteRequestCase, - // eslint-disable-next-line @kbn/eslint/no-restricted-paths -} from '../../../../../actions/server/routes/rewrite_request_case'; +import { AsApiContract, RewriteRequestCase } from '../../../../../actions/common'; import { BASE_ACTION_API_PATH } from '../../constants'; import type { ActionConnector, ActionConnectorProps } from '../../../types'; diff --git a/x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api/create.ts b/x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api/create.ts index 4daf8dd59c70a..e6e74f3f3c059 100644 --- a/x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api/create.ts +++ b/x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api/create.ts @@ -5,11 +5,7 @@ * 2.0. */ import { HttpSetup } from 'kibana/public'; -import { - RewriteRequestCase, - RewriteResponseCase, - // eslint-disable-next-line @kbn/eslint/no-restricted-paths -} from '../../../../../actions/server/routes/rewrite_request_case'; +import { RewriteRequestCase, RewriteResponseCase } from '../../../../../actions/common'; import { BASE_ACTION_API_PATH } from '../../constants'; import type { ActionConnector, diff --git a/x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api/execute.ts b/x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api/execute.ts index f6cf6b4c37634..638ceddb5652f 100644 --- a/x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api/execute.ts +++ b/x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api/execute.ts @@ -6,9 +6,10 @@ */ import { HttpSetup } from 'kibana/public'; -// eslint-disable-next-line @kbn/eslint/no-restricted-paths -import { RewriteRequestCase } from '../../../../../actions/server/routes/rewrite_request_case'; -import { ActionTypeExecutorResult } from '../../../../../../plugins/actions/common'; +import { + ActionTypeExecutorResult, + RewriteRequestCase, +} from '../../../../../../plugins/actions/common'; import { BASE_ACTION_API_PATH } from '../../constants'; const rewriteBodyRes: RewriteRequestCase> = ({ diff --git a/x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api/update.ts b/x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api/update.ts index 8d97f7f4a213b..18b8871ce25d1 100644 --- a/x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api/update.ts +++ b/x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api/update.ts @@ -5,10 +5,7 @@ * 2.0. */ import { HttpSetup } from 'kibana/public'; -import { - RewriteRequestCase, - // eslint-disable-next-line @kbn/eslint/no-restricted-paths -} from '../../../../../actions/server/routes/rewrite_request_case'; +import { RewriteRequestCase } from '../../../../../actions/common'; import { BASE_ACTION_API_PATH } from '../../constants'; import type { ActionConnector, From db83818d964e409014c64ab835e0b0f7c251803d Mon Sep 17 00:00:00 2001 From: Yuliia Naumenko Date: Tue, 16 Mar 2021 10:05:19 -0700 Subject: [PATCH 9/9] fixed due to comments --- .../action_connector_api.test.ts | 190 ------------------ .../connector_types.test.ts | 49 +++++ .../action_connector_api/connectors.test.ts | 27 +++ .../lib/action_connector_api/create.test.ts | 48 +++++ .../lib/action_connector_api/delete.test.ts | 35 ++++ .../lib/action_connector_api/execute.test.ts | 42 ++++ .../lib/action_connector_api/update.test.ts | 49 +++++ 7 files changed, 250 insertions(+), 190 deletions(-) delete mode 100644 x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api/action_connector_api.test.ts create mode 100644 x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api/connector_types.test.ts create mode 100644 x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api/connectors.test.ts create mode 100644 x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api/create.test.ts create mode 100644 x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api/delete.test.ts create mode 100644 x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api/execute.test.ts create mode 100644 x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api/update.test.ts diff --git a/x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api/action_connector_api.test.ts b/x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api/action_connector_api.test.ts deleted file mode 100644 index 372a22c76c1fa..0000000000000 --- a/x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api/action_connector_api.test.ts +++ /dev/null @@ -1,190 +0,0 @@ -/* - * 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 { ActionConnectorWithoutId, ActionType } from '../../../types'; -import { httpServiceMock } from '../../../../../../../src/core/public/mocks'; -import { - createActionConnector, - deleteActions, - loadActionTypes, - loadAllActions, - updateActionConnector, - executeAction, -} from './index'; - -const http = httpServiceMock.createStartContract(); - -beforeEach(() => jest.resetAllMocks()); - -describe('loadActionTypes', () => { - test('should call get types API', async () => { - const apiResponseValue = [ - { - id: 'test', - name: 'Test', - enabled: true, - enabled_in_config: true, - enabled_in_license: true, - minimum_license_required: 'basic', - }, - ]; - http.get.mockResolvedValueOnce(apiResponseValue); - - const resolvedValue: ActionType[] = [ - { - id: 'test', - name: 'Test', - enabled: true, - enabledInConfig: true, - enabledInLicense: true, - minimumLicenseRequired: 'basic', - }, - ]; - - const result = await loadActionTypes({ http }); - expect(result).toEqual(resolvedValue); - expect(http.get.mock.calls[0]).toMatchInlineSnapshot(` - Array [ - "/api/actions/connector_types", - ] - `); - }); -}); - -describe('loadAllActions', () => { - test('should call getAll actions API', async () => { - http.get.mockResolvedValueOnce([]); - - const result = await loadAllActions({ http }); - expect(result).toEqual([]); - expect(http.get.mock.calls[0]).toMatchInlineSnapshot(` - Array [ - "/api/actions/connectors", - ] - `); - }); -}); - -describe('createActionConnector', () => { - test('should call create action API', async () => { - const apiResponse = { - connector_type_id: 'test', - is_preconfigured: false, - name: 'My test', - config: {}, - secrets: {}, - id: '123', - }; - http.post.mockResolvedValueOnce(apiResponse); - - const connector: ActionConnectorWithoutId<{}, {}> = { - actionTypeId: 'test', - isPreconfigured: false, - name: 'My test', - config: {}, - secrets: {}, - }; - const resolvedValue = { ...connector, id: '123' }; - - const result = await createActionConnector({ http, connector }); - expect(result).toEqual(resolvedValue); - expect(http.post.mock.calls[0]).toMatchInlineSnapshot(` - Array [ - "/api/actions/connector", - Object { - "body": "{\\"name\\":\\"My test\\",\\"config\\":{},\\"secrets\\":{},\\"connector_type_id\\":\\"test\\",\\"is_preconfigured\\":false}", - }, - ] - `); - }); -}); - -describe('updateActionConnector', () => { - test('should call the update API', async () => { - const id = '123'; - const apiResponse = { - connector_type_id: 'test', - is_preconfigured: false, - name: 'My test', - config: {}, - secrets: {}, - id, - }; - http.put.mockResolvedValueOnce(apiResponse); - - const connector: ActionConnectorWithoutId<{}, {}> = { - actionTypeId: 'test', - isPreconfigured: false, - name: 'My test', - config: {}, - secrets: {}, - }; - const resolvedValue = { ...connector, id }; - - const result = await updateActionConnector({ http, connector, id }); - expect(result).toEqual(resolvedValue); - expect(http.put.mock.calls[0]).toMatchInlineSnapshot(` - Array [ - "/api/actions/connector/123", - Object { - "body": "{\\"name\\":\\"My test\\",\\"config\\":{},\\"secrets\\":{}}", - }, - ] - `); - }); -}); - -describe('deleteActions', () => { - test('should call delete API per action', async () => { - const ids = ['1', '2', '3']; - - const result = await deleteActions({ ids, http }); - expect(result).toEqual({ errors: [], successes: [undefined, undefined, undefined] }); - expect(http.delete.mock.calls).toMatchInlineSnapshot(` - Array [ - Array [ - "/api/actions/connector/1", - ], - Array [ - "/api/actions/connector/2", - ], - Array [ - "/api/actions/connector/3", - ], - ] - `); - }); -}); - -describe('executeAction', () => { - test('should call execute API', async () => { - const id = '123'; - const params = { - stringParams: 'someString', - numericParams: 123, - }; - - http.post.mockResolvedValueOnce({ - connector_id: id, - status: 'ok', - }); - - const result = await executeAction({ id, http, params }); - expect(result).toEqual({ - actionId: id, - status: 'ok', - }); - expect(http.post.mock.calls[0]).toMatchInlineSnapshot(` - Array [ - "/api/actions/connector/123/_execute", - Object { - "body": "{\\"params\\":{\\"stringParams\\":\\"someString\\",\\"numericParams\\":123}}", - }, - ] - `); - }); -}); diff --git a/x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api/connector_types.test.ts b/x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api/connector_types.test.ts new file mode 100644 index 0000000000000..8815757df6af5 --- /dev/null +++ b/x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api/connector_types.test.ts @@ -0,0 +1,49 @@ +/* + * 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 { ActionType } from '../../../types'; +import { httpServiceMock } from '../../../../../../../src/core/public/mocks'; +import { loadActionTypes } from './index'; + +const http = httpServiceMock.createStartContract(); + +beforeEach(() => jest.resetAllMocks()); + +describe('loadActionTypes', () => { + test('should call get types API', async () => { + const apiResponseValue = [ + { + id: 'test', + name: 'Test', + enabled: true, + enabled_in_config: true, + enabled_in_license: true, + minimum_license_required: 'basic', + }, + ]; + http.get.mockResolvedValueOnce(apiResponseValue); + + const resolvedValue: ActionType[] = [ + { + id: 'test', + name: 'Test', + enabled: true, + enabledInConfig: true, + enabledInLicense: true, + minimumLicenseRequired: 'basic', + }, + ]; + + const result = await loadActionTypes({ http }); + expect(result).toEqual(resolvedValue); + expect(http.get.mock.calls[0]).toMatchInlineSnapshot(` + Array [ + "/api/actions/connector_types", + ] + `); + }); +}); diff --git a/x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api/connectors.test.ts b/x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api/connectors.test.ts new file mode 100644 index 0000000000000..565cc0afebfea --- /dev/null +++ b/x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api/connectors.test.ts @@ -0,0 +1,27 @@ +/* + * 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 { httpServiceMock } from '../../../../../../../src/core/public/mocks'; +import { loadAllActions } from './index'; + +const http = httpServiceMock.createStartContract(); + +beforeEach(() => jest.resetAllMocks()); + +describe('loadAllActions', () => { + test('should call getAll actions API', async () => { + http.get.mockResolvedValueOnce([]); + + const result = await loadAllActions({ http }); + expect(result).toEqual([]); + expect(http.get.mock.calls[0]).toMatchInlineSnapshot(` + Array [ + "/api/actions/connectors", + ] + `); + }); +}); diff --git a/x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api/create.test.ts b/x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api/create.test.ts new file mode 100644 index 0000000000000..208970fbfc061 --- /dev/null +++ b/x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api/create.test.ts @@ -0,0 +1,48 @@ +/* + * 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 { ActionConnectorWithoutId } from '../../../types'; +import { httpServiceMock } from '../../../../../../../src/core/public/mocks'; +import { createActionConnector } from './index'; + +const http = httpServiceMock.createStartContract(); + +beforeEach(() => jest.resetAllMocks()); + +describe('createActionConnector', () => { + test('should call create action API', async () => { + const apiResponse = { + connector_type_id: 'test', + is_preconfigured: false, + name: 'My test', + config: {}, + secrets: {}, + id: '123', + }; + http.post.mockResolvedValueOnce(apiResponse); + + const connector: ActionConnectorWithoutId<{}, {}> = { + actionTypeId: 'test', + isPreconfigured: false, + name: 'My test', + config: {}, + secrets: {}, + }; + const resolvedValue = { ...connector, id: '123' }; + + const result = await createActionConnector({ http, connector }); + expect(result).toEqual(resolvedValue); + expect(http.post.mock.calls[0]).toMatchInlineSnapshot(` + Array [ + "/api/actions/connector", + Object { + "body": "{\\"name\\":\\"My test\\",\\"config\\":{},\\"secrets\\":{},\\"connector_type_id\\":\\"test\\",\\"is_preconfigured\\":false}", + }, + ] + `); + }); +}); diff --git a/x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api/delete.test.ts b/x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api/delete.test.ts new file mode 100644 index 0000000000000..bb00c8c30e4ed --- /dev/null +++ b/x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api/delete.test.ts @@ -0,0 +1,35 @@ +/* + * 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 { httpServiceMock } from '../../../../../../../src/core/public/mocks'; +import { deleteActions } from './index'; + +const http = httpServiceMock.createStartContract(); + +beforeEach(() => jest.resetAllMocks()); + +describe('deleteActions', () => { + test('should call delete API per action', async () => { + const ids = ['1', '2', '3']; + + const result = await deleteActions({ ids, http }); + expect(result).toEqual({ errors: [], successes: [undefined, undefined, undefined] }); + expect(http.delete.mock.calls).toMatchInlineSnapshot(` + Array [ + Array [ + "/api/actions/connector/1", + ], + Array [ + "/api/actions/connector/2", + ], + Array [ + "/api/actions/connector/3", + ], + ] + `); + }); +}); diff --git a/x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api/execute.test.ts b/x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api/execute.test.ts new file mode 100644 index 0000000000000..60cd3132aa756 --- /dev/null +++ b/x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api/execute.test.ts @@ -0,0 +1,42 @@ +/* + * 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 { httpServiceMock } from '../../../../../../../src/core/public/mocks'; +import { executeAction } from './index'; + +const http = httpServiceMock.createStartContract(); + +beforeEach(() => jest.resetAllMocks()); + +describe('executeAction', () => { + test('should call execute API', async () => { + const id = '123'; + const params = { + stringParams: 'someString', + numericParams: 123, + }; + + http.post.mockResolvedValueOnce({ + connector_id: id, + status: 'ok', + }); + + const result = await executeAction({ id, http, params }); + expect(result).toEqual({ + actionId: id, + status: 'ok', + }); + expect(http.post.mock.calls[0]).toMatchInlineSnapshot(` + Array [ + "/api/actions/connector/123/_execute", + Object { + "body": "{\\"params\\":{\\"stringParams\\":\\"someString\\",\\"numericParams\\":123}}", + }, + ] + `); + }); +}); diff --git a/x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api/update.test.ts b/x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api/update.test.ts new file mode 100644 index 0000000000000..29e7a1e4bed3d --- /dev/null +++ b/x-pack/plugins/triggers_actions_ui/public/application/lib/action_connector_api/update.test.ts @@ -0,0 +1,49 @@ +/* + * 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 { ActionConnectorWithoutId } from '../../../types'; +import { httpServiceMock } from '../../../../../../../src/core/public/mocks'; +import { updateActionConnector } from './index'; + +const http = httpServiceMock.createStartContract(); + +beforeEach(() => jest.resetAllMocks()); + +describe('updateActionConnector', () => { + test('should call the update API', async () => { + const id = '123'; + const apiResponse = { + connector_type_id: 'test', + is_preconfigured: false, + name: 'My test', + config: {}, + secrets: {}, + id, + }; + http.put.mockResolvedValueOnce(apiResponse); + + const connector: ActionConnectorWithoutId<{}, {}> = { + actionTypeId: 'test', + isPreconfigured: false, + name: 'My test', + config: {}, + secrets: {}, + }; + const resolvedValue = { ...connector, id }; + + const result = await updateActionConnector({ http, connector, id }); + expect(result).toEqual(resolvedValue); + expect(http.put.mock.calls[0]).toMatchInlineSnapshot(` + Array [ + "/api/actions/connector/123", + Object { + "body": "{\\"name\\":\\"My test\\",\\"config\\":{},\\"secrets\\":{}}", + }, + ] + `); + }); +});