From a8ea52daee8aff75064bd421b9fada17396ab03b Mon Sep 17 00:00:00 2001 From: Simeon Widdis Date: Tue, 7 May 2024 12:26:47 -0700 Subject: [PATCH] Refactor away integrations adaptor class (#1825) * Refactor away integrations adaptor class Signed-off-by: Simeon Widdis * Remove adaptor use in tests Signed-off-by: Simeon Widdis --------- Signed-off-by: Simeon Widdis --- .../integrations/integrations_adaptor.ts | 35 ----------- .../integrations/integrations_manager.ts | 3 +- .../__tests__/integrations_router.test.ts | 10 ++-- .../integrations/integrations_router.ts | 59 ++++++++----------- 4 files changed, 31 insertions(+), 76 deletions(-) delete mode 100644 server/adaptors/integrations/integrations_adaptor.ts diff --git a/server/adaptors/integrations/integrations_adaptor.ts b/server/adaptors/integrations/integrations_adaptor.ts deleted file mode 100644 index 570c3d6d80..0000000000 --- a/server/adaptors/integrations/integrations_adaptor.ts +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright OpenSearch Contributors - * SPDX-License-Identifier: Apache-2.0 - */ - -export interface IntegrationsAdaptor { - getIntegrationTemplates: ( - query?: IntegrationTemplateQuery - ) => Promise; - - getIntegrationInstances: ( - query?: IntegrationInstanceQuery - ) => Promise; - - getIntegrationInstance: (query?: IntegrationInstanceQuery) => Promise; - - loadIntegrationInstance: ( - templateName: string, - name: string, - indexPattern: string, - workflows?: string[], - dataSource?: string, - tableName?: string - ) => Promise; - - deleteIntegrationInstance: (id: string) => Promise; - - getStatic: (templateName: string, path: string) => Promise; - - getSchemas: (templateName: string) => Promise<{ mappings: { [key: string]: unknown } }>; - - getAssets: (templateName: string) => Promise; - - getSampleData: (templateName: string) => Promise<{ sampleData: object[] | null }>; -} diff --git a/server/adaptors/integrations/integrations_manager.ts b/server/adaptors/integrations/integrations_manager.ts index fc44619089..e24ccfae1e 100644 --- a/server/adaptors/integrations/integrations_manager.ts +++ b/server/adaptors/integrations/integrations_manager.ts @@ -5,14 +5,13 @@ import path from 'path'; import { addRequestToMetric } from '../../common/metrics/metrics_helper'; -import { IntegrationsAdaptor } from './integrations_adaptor'; import { SavedObject, SavedObjectsClientContract } from '../../../../../src/core/server/types'; import { IntegrationInstanceBuilder } from './integrations_builder'; import { TemplateManager } from './repository/repository'; import { FileSystemDataAdaptor } from './repository/fs_data_adaptor'; import { IndexDataAdaptor } from './repository/index_data_adaptor'; -export class IntegrationsManager implements IntegrationsAdaptor { +export class IntegrationsManager { client: SavedObjectsClientContract; instanceBuilder: IntegrationInstanceBuilder; repository: TemplateManager; diff --git a/server/routes/integrations/__tests__/integrations_router.test.ts b/server/routes/integrations/__tests__/integrations_router.test.ts index 5f6a7c39e5..244da489be 100644 --- a/server/routes/integrations/__tests__/integrations_router.test.ts +++ b/server/routes/integrations/__tests__/integrations_router.test.ts @@ -6,14 +6,14 @@ import { DeepPartial } from 'redux'; import { OpenSearchDashboardsResponseFactory } from '../../../../../../src/core/server/http/router'; import { handleWithCallback } from '../integrations_router'; -import { IntegrationsAdaptor } from 'server/adaptors/integrations/integrations_adaptor'; +import { IntegrationsManager } from 'server/adaptors/integrations/integrations_manager'; jest .mock('../../../../../../src/core/server', () => jest.fn()) .mock('../../../../../../src/core/server/http/router', () => jest.fn()); describe('Data wrapper', () => { - const adaptorMock: Partial = {}; + const adaptorMock: Partial = {}; const responseMock: DeepPartial = { custom: jest.fn((data) => data), ok: jest.fn((data) => data), @@ -24,9 +24,9 @@ describe('Data wrapper', () => { return { test: 'data' }; }); const result = await handleWithCallback( - adaptorMock as IntegrationsAdaptor, + adaptorMock as IntegrationsManager, responseMock as OpenSearchDashboardsResponseFactory, - (callback as unknown) as (a: IntegrationsAdaptor) => Promise + (callback as unknown) as (a: IntegrationsManager) => Promise ); expect(callback).toHaveBeenCalled(); @@ -39,7 +39,7 @@ describe('Data wrapper', () => { throw new Error('test error'); }); const result = await handleWithCallback( - adaptorMock as IntegrationsAdaptor, + adaptorMock as IntegrationsManager, responseMock as OpenSearchDashboardsResponseFactory, callback ); diff --git a/server/routes/integrations/integrations_router.ts b/server/routes/integrations/integrations_router.ts index f8e7e7908d..68eed60d91 100644 --- a/server/routes/integrations/integrations_router.ts +++ b/server/routes/integrations/integrations_router.ts @@ -6,11 +6,9 @@ import { schema } from '@osd/config-schema'; import * as mime from 'mime'; import sanitize from 'sanitize-filename'; -import { IRouter, RequestHandlerContext } from '../../../../../src/core/server'; +import { IRouter } from '../../../../../src/core/server'; import { INTEGRATIONS_BASE } from '../../../common/constants/shared'; -import { IntegrationsAdaptor } from '../../adaptors/integrations/integrations_adaptor'; import { - OpenSearchDashboardsRequest, OpenSearchDashboardsResponse, OpenSearchDashboardsResponseFactory, } from '../../../../../src/core/server/http/router'; @@ -19,24 +17,24 @@ import { IntegrationsManager } from '../../adaptors/integrations/integrations_ma /** * Handle an `OpenSearchDashboardsRequest` using the provided `callback` function. * This is a convenience method that handles common error handling and response formatting. - * The callback must accept a `IntegrationsAdaptor` as its first argument. + * The callback must accept a `IntegrationsManager` as its first argument. * * If the callback throws an error, * the `OpenSearchDashboardsResponse` will be formatted using the error's `statusCode` and `message` properties. * Otherwise, the callback's return value will be formatted in a JSON object under the `data` field. * - * @param {IntegrationsAdaptor} adaptor The adaptor instance to use for making requests. + * @param {IntegrationsManager} manager The integration manager instance to use for making requests. * @param {OpenSearchDashboardsResponseFactory} response The factory to use for creating responses. * @callback callback A callback that will invoke a request on a provided adaptor. * @returns {Promise} An `OpenSearchDashboardsResponse` with the return data from the callback. */ export const handleWithCallback = async ( - adaptor: IntegrationsAdaptor, + manager: IntegrationsManager, response: OpenSearchDashboardsResponseFactory, - callback: (a: IntegrationsAdaptor) => Promise + callback: (a: IntegrationsManager) => Promise ): Promise> => { try { - const data = await callback(adaptor); + const data = await callback(manager); return response.ok({ body: { data, @@ -51,13 +49,6 @@ export const handleWithCallback = async ( } }; -const getAdaptor = ( - context: RequestHandlerContext, - _request: OpenSearchDashboardsRequest -): IntegrationsAdaptor => { - return new IntegrationsManager(context.core.savedObjects.client); -}; - export function registerIntegrationsRoute(router: IRouter) { router.get( { @@ -65,8 +56,8 @@ export function registerIntegrationsRoute(router: IRouter) { validate: false, }, async (context, request, response): Promise => { - const adaptor = getAdaptor(context, request); - return handleWithCallback(adaptor, response, async (a: IntegrationsAdaptor) => + const adaptor = new IntegrationsManager(context.core.savedObjects.client); + return handleWithCallback(adaptor, response, async (a: IntegrationsManager) => a.getIntegrationTemplates() ); } @@ -89,8 +80,8 @@ export function registerIntegrationsRoute(router: IRouter) { }, }, async (context, request, response): Promise => { - const adaptor = getAdaptor(context, request); - return handleWithCallback(adaptor, response, async (a: IntegrationsAdaptor) => { + const adaptor = new IntegrationsManager(context.core.savedObjects.client); + return handleWithCallback(adaptor, response, async (a: IntegrationsManager) => { return a.loadIntegrationInstance( request.params.templateName, request.body.name, @@ -113,11 +104,11 @@ export function registerIntegrationsRoute(router: IRouter) { }, }, async (context, request, response): Promise => { - const adaptor = getAdaptor(context, request); + const adaptor = new IntegrationsManager(context.core.savedObjects.client); return handleWithCallback( adaptor, response, - async (a: IntegrationsAdaptor) => + async (a: IntegrationsManager) => ( await a.getIntegrationTemplates({ name: request.params.name, @@ -138,7 +129,7 @@ export function registerIntegrationsRoute(router: IRouter) { }, }, async (context, request, response): Promise => { - const adaptor = getAdaptor(context, request); + const adaptor = new IntegrationsManager(context.core.savedObjects.client); try { const requestPath = sanitize(request.params.path); const result = await adaptor.getStatic(request.params.id, requestPath); @@ -167,8 +158,8 @@ export function registerIntegrationsRoute(router: IRouter) { }, }, async (context, request, response): Promise => { - const adaptor = getAdaptor(context, request); - return handleWithCallback(adaptor, response, async (a: IntegrationsAdaptor) => + const adaptor = new IntegrationsManager(context.core.savedObjects.client); + return handleWithCallback(adaptor, response, async (a: IntegrationsManager) => a.getSchemas(request.params.id) ); } @@ -184,8 +175,8 @@ export function registerIntegrationsRoute(router: IRouter) { }, }, async (context, request, response): Promise => { - const adaptor = getAdaptor(context, request); - return handleWithCallback(adaptor, response, async (a: IntegrationsAdaptor) => + const adaptor = new IntegrationsManager(context.core.savedObjects.client); + return handleWithCallback(adaptor, response, async (a: IntegrationsManager) => a.getAssets(request.params.id) ); } @@ -201,8 +192,8 @@ export function registerIntegrationsRoute(router: IRouter) { }, }, async (context, request, response): Promise => { - const adaptor = getAdaptor(context, request); - return handleWithCallback(adaptor, response, async (a: IntegrationsAdaptor) => + const adaptor = new IntegrationsManager(context.core.savedObjects.client); + return handleWithCallback(adaptor, response, async (a: IntegrationsManager) => a.getSampleData(request.params.id) ); } @@ -214,8 +205,8 @@ export function registerIntegrationsRoute(router: IRouter) { validate: false, }, async (context, request, response): Promise => { - const adaptor = getAdaptor(context, request); - return handleWithCallback(adaptor, response, async (a: IntegrationsAdaptor) => + const adaptor = new IntegrationsManager(context.core.savedObjects.client); + return handleWithCallback(adaptor, response, async (a: IntegrationsManager) => a.getIntegrationInstances({}) ); } @@ -231,8 +222,8 @@ export function registerIntegrationsRoute(router: IRouter) { }, }, async (context, request, response): Promise => { - const adaptor = getAdaptor(context, request); - return handleWithCallback(adaptor, response, async (a: IntegrationsAdaptor) => + const adaptor = new IntegrationsManager(context.core.savedObjects.client); + return handleWithCallback(adaptor, response, async (a: IntegrationsManager) => a.deleteIntegrationInstance(request.params.id) ); } @@ -248,8 +239,8 @@ export function registerIntegrationsRoute(router: IRouter) { }, }, async (context, request, response): Promise => { - const adaptor = getAdaptor(context, request); - return handleWithCallback(adaptor, response, async (a: IntegrationsAdaptor) => + const adaptor = new IntegrationsManager(context.core.savedObjects.client); + return handleWithCallback(adaptor, response, async (a: IntegrationsManager) => a.getIntegrationInstance({ id: request.params.id, })