From 37b5f4a2dfd5f590d8aef8b22bc964fd7c13a55c Mon Sep 17 00:00:00 2001 From: Christos Nasikas Date: Tue, 8 Jun 2021 16:26:39 +0300 Subject: [PATCH] Convert factory to class --- .../cases/server/client/cases/utils.test.ts | 4 +-- x-pack/plugins/cases/server/client/factory.ts | 6 ++-- .../cases/server/connectors/factory.ts | 30 +++++++++++-------- .../plugins/cases/server/connectors/index.ts | 2 +- .../plugins/cases/server/connectors/types.ts | 4 +-- x-pack/plugins/cases/server/plugin.ts | 2 -- 6 files changed, 25 insertions(+), 23 deletions(-) diff --git a/x-pack/plugins/cases/server/client/cases/utils.test.ts b/x-pack/plugins/cases/server/client/cases/utils.test.ts index 10a84643eeb7d0..ad27a856442b08 100644 --- a/x-pack/plugins/cases/server/client/cases/utils.test.ts +++ b/x-pack/plugins/cases/server/client/cases/utils.test.ts @@ -30,7 +30,7 @@ import { } from './utils'; import { flattenCaseSavedObject } from '../../common'; import { SECURITY_SOLUTION_OWNER } from '../../../common'; -import { createCaseConnectorFactory } from '../../connectors'; +import { CasesConnectorsFactory } from '../../connectors'; const formatComment = { commentId: commentObj.id, @@ -436,7 +436,7 @@ describe('utils', () => { isPreconfigured: false, }; - const casesConnectors = createCaseConnectorFactory().getCasesConnectors(); + const casesConnectors = new CasesConnectorsFactory().getCasesConnectors(); it('creates an external incident', async () => { const res = await createIncident({ diff --git a/x-pack/plugins/cases/server/client/factory.ts b/x-pack/plugins/cases/server/client/factory.ts index fd894381947169..710383ac014330 100644 --- a/x-pack/plugins/cases/server/client/factory.ts +++ b/x-pack/plugins/cases/server/client/factory.ts @@ -27,7 +27,7 @@ import { PluginStartContract as FeaturesPluginStart } from '../../../features/se import { PluginStartContract as ActionsPluginStart } from '../../../actions/server'; import { AuthorizationAuditLogger } from '../authorization'; import { CasesClient, createCasesClient } from '.'; -import { CasesConnectorFactory } from '../connectors/types'; +import { CasesConnectorsFactory } from '../connectors'; interface CasesClientFactoryArgs { securityPluginSetup?: SecurityPluginSetup; @@ -35,7 +35,6 @@ interface CasesClientFactoryArgs { getSpace: GetSpaceFn; featuresPluginStart: FeaturesPluginStart; actionsPluginStart: ActionsPluginStart; - casesConnectorsFactory: CasesConnectorFactory; } /** @@ -92,6 +91,7 @@ export class CasesClientFactory { const caseService = new CasesService(this.logger, this.options?.securityPluginStart?.authc); const userInfo = caseService.getUser({ request }); + const casesConnectorsFactory = new CasesConnectorsFactory(); return createCasesClient({ alertsService: new AlertService(), @@ -112,7 +112,7 @@ export class CasesClientFactory { logger: this.logger, authorization: auth, actionsClient: await this.options.actionsPluginStart.getActionsClientWithRequest(request), - casesConnectors: this.options.casesConnectorsFactory.getCasesConnectors(), + casesConnectors: casesConnectorsFactory.getCasesConnectors(), }); } } diff --git a/x-pack/plugins/cases/server/connectors/factory.ts b/x-pack/plugins/cases/server/connectors/factory.ts index 234d7c63387ebc..a1e3c644d02b64 100644 --- a/x-pack/plugins/cases/server/connectors/factory.ts +++ b/x-pack/plugins/cases/server/connectors/factory.ts @@ -13,19 +13,25 @@ import { CasesConnectorsMap, CasesConnectorTypes, ICasesConnector, - CreateCasesConnectorFactory, + ICasesConnectorFactory, } from './types'; -const getCasesConnectors = (): CasesConnectorsMap => { - const casesConnectorsMap = new Map(); - casesConnectorsMap.set(ConnectorTypes.jira, getJiraCaseConnector()); - casesConnectorsMap.set(ConnectorTypes.serviceNowITSM, getServiceNowITSMCaseConnector()); - casesConnectorsMap.set(ConnectorTypes.serviceNowSIR, getServiceNowSIRCaseConnector()); - casesConnectorsMap.set(ConnectorTypes.resilient, getResilientCaseConnector()); +export class CasesConnectorsFactory implements ICasesConnectorFactory { + private readonly casesConnectorsMap: Map>; - return casesConnectorsMap; -}; + constructor() { + this.casesConnectorsMap = new Map(); + this.initMapping(); + } -export const createCaseConnectorFactory: CreateCasesConnectorFactory = () => ({ - getCasesConnectors, -}); + private initMapping() { + this.casesConnectorsMap.set(ConnectorTypes.jira, getJiraCaseConnector()); + this.casesConnectorsMap.set(ConnectorTypes.serviceNowITSM, getServiceNowITSMCaseConnector()); + this.casesConnectorsMap.set(ConnectorTypes.serviceNowSIR, getServiceNowSIRCaseConnector()); + this.casesConnectorsMap.set(ConnectorTypes.resilient, getResilientCaseConnector()); + } + + public getCasesConnectors = (): CasesConnectorsMap => { + return this.casesConnectorsMap; + }; +} diff --git a/x-pack/plugins/cases/server/connectors/index.ts b/x-pack/plugins/cases/server/connectors/index.ts index 6f21a2296a4900..ea55d717b19893 100644 --- a/x-pack/plugins/cases/server/connectors/index.ts +++ b/x-pack/plugins/cases/server/connectors/index.ts @@ -16,7 +16,7 @@ import { CommentRequest, CommentType } from '../../common/api'; export * from './types'; export { transformConnectorComment } from './case'; -export { createCaseConnectorFactory } from './factory'; +export { CasesConnectorsFactory } from './factory'; /** * Separator used for creating a json parsable array from the mustache syntax that the alerting framework diff --git a/x-pack/plugins/cases/server/connectors/types.ts b/x-pack/plugins/cases/server/connectors/types.ts index 4163fc51acc2f3..d720f8e9064684 100644 --- a/x-pack/plugins/cases/server/connectors/types.ts +++ b/x-pack/plugins/cases/server/connectors/types.ts @@ -34,8 +34,6 @@ export interface ICasesConnector { export type CasesConnectorTypes = Omit; export type CasesConnectorsMap = Map; -export interface CasesConnectorFactory { +export interface ICasesConnectorFactory { getCasesConnectors: () => CasesConnectorsMap; } - -export type CreateCasesConnectorFactory = () => CasesConnectorFactory; diff --git a/x-pack/plugins/cases/server/plugin.ts b/x-pack/plugins/cases/server/plugin.ts index 3d4b4e2fe88095..34cf71aff58ba8 100644 --- a/x-pack/plugins/cases/server/plugin.ts +++ b/x-pack/plugins/cases/server/plugin.ts @@ -32,7 +32,6 @@ import type { CasesRequestHandlerContext } from './types'; import { CasesClientFactory } from './client/factory'; import { SpacesPluginStart } from '../../spaces/server'; import { PluginStartContract as FeaturesPluginStart } from '../../features/server'; -import { createCaseConnectorFactory } from './connectors/factory'; function createConfig(context: PluginInitializerContext) { return context.config.get(); @@ -128,7 +127,6 @@ export class CasePlugin { }, featuresPluginStart: plugins.features, actionsPluginStart: plugins.actions, - casesConnectorsFactory: createCaseConnectorFactory(), }); const client = core.elasticsearch.client;