Skip to content

Commit

Permalink
Refactor away integrations adaptor class (opensearch-project#1825)
Browse files Browse the repository at this point in the history
* Refactor away integrations adaptor class

Signed-off-by: Simeon Widdis <[email protected]>

* Remove adaptor use in tests

Signed-off-by: Simeon Widdis <[email protected]>

---------

Signed-off-by: Simeon Widdis <[email protected]>
  • Loading branch information
Swiddis authored May 7, 2024
1 parent 0632d4a commit a8ea52d
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 76 deletions.
35 changes: 0 additions & 35 deletions server/adaptors/integrations/integrations_adaptor.ts

This file was deleted.

3 changes: 1 addition & 2 deletions server/adaptors/integrations/integrations_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 5 additions & 5 deletions server/routes/integrations/__tests__/integrations_router.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<IntegrationsAdaptor> = {};
const adaptorMock: Partial<IntegrationsManager> = {};
const responseMock: DeepPartial<OpenSearchDashboardsResponseFactory> = {
custom: jest.fn((data) => data),
ok: jest.fn((data) => data),
Expand All @@ -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<unknown>
(callback as unknown) as (a: IntegrationsManager) => Promise<unknown>
);

expect(callback).toHaveBeenCalled();
Expand All @@ -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
);
Expand Down
59 changes: 25 additions & 34 deletions server/routes/integrations/integrations_router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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<OpenSearchDashboardsResponse>} An `OpenSearchDashboardsResponse` with the return data from the callback.
*/
export const handleWithCallback = async <T>(
adaptor: IntegrationsAdaptor,
manager: IntegrationsManager,
response: OpenSearchDashboardsResponseFactory,
callback: (a: IntegrationsAdaptor) => Promise<T>
callback: (a: IntegrationsManager) => Promise<T>
): Promise<OpenSearchDashboardsResponse<{ data: T } | string>> => {
try {
const data = await callback(adaptor);
const data = await callback(manager);
return response.ok({
body: {
data,
Expand All @@ -51,22 +49,15 @@ export const handleWithCallback = async <T>(
}
};

const getAdaptor = (
context: RequestHandlerContext,
_request: OpenSearchDashboardsRequest
): IntegrationsAdaptor => {
return new IntegrationsManager(context.core.savedObjects.client);
};

export function registerIntegrationsRoute(router: IRouter) {
router.get(
{
path: `${INTEGRATIONS_BASE}/repository`,
validate: false,
},
async (context, request, response): Promise<OpenSearchDashboardsResponse> => {
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()
);
}
Expand All @@ -89,8 +80,8 @@ export function registerIntegrationsRoute(router: IRouter) {
},
},
async (context, request, response): Promise<OpenSearchDashboardsResponse> => {
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,
Expand All @@ -113,11 +104,11 @@ export function registerIntegrationsRoute(router: IRouter) {
},
},
async (context, request, response): Promise<OpenSearchDashboardsResponse> => {
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,
Expand All @@ -138,7 +129,7 @@ export function registerIntegrationsRoute(router: IRouter) {
},
},
async (context, request, response): Promise<OpenSearchDashboardsResponse> => {
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);
Expand Down Expand Up @@ -167,8 +158,8 @@ export function registerIntegrationsRoute(router: IRouter) {
},
},
async (context, request, response): Promise<OpenSearchDashboardsResponse> => {
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)
);
}
Expand All @@ -184,8 +175,8 @@ export function registerIntegrationsRoute(router: IRouter) {
},
},
async (context, request, response): Promise<OpenSearchDashboardsResponse> => {
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)
);
}
Expand All @@ -201,8 +192,8 @@ export function registerIntegrationsRoute(router: IRouter) {
},
},
async (context, request, response): Promise<OpenSearchDashboardsResponse> => {
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)
);
}
Expand All @@ -214,8 +205,8 @@ export function registerIntegrationsRoute(router: IRouter) {
validate: false,
},
async (context, request, response): Promise<OpenSearchDashboardsResponse> => {
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({})
);
}
Expand All @@ -231,8 +222,8 @@ export function registerIntegrationsRoute(router: IRouter) {
},
},
async (context, request, response): Promise<OpenSearchDashboardsResponse> => {
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)
);
}
Expand All @@ -248,8 +239,8 @@ export function registerIntegrationsRoute(router: IRouter) {
},
},
async (context, request, response): Promise<OpenSearchDashboardsResponse> => {
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,
})
Expand Down

0 comments on commit a8ea52d

Please sign in to comment.