Skip to content

Commit

Permalink
Convert factory to class
Browse files Browse the repository at this point in the history
  • Loading branch information
cnasikas committed Jun 8, 2021
1 parent 986b0d0 commit 37b5f4a
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 23 deletions.
4 changes: 2 additions & 2 deletions x-pack/plugins/cases/server/client/cases/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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({
Expand Down
6 changes: 3 additions & 3 deletions x-pack/plugins/cases/server/client/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,14 @@ 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;
securityPluginStart?: SecurityPluginStart;
getSpace: GetSpaceFn;
featuresPluginStart: FeaturesPluginStart;
actionsPluginStart: ActionsPluginStart;
casesConnectorsFactory: CasesConnectorFactory;
}

/**
Expand Down Expand Up @@ -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(),
Expand All @@ -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(),
});
}
}
30 changes: 18 additions & 12 deletions x-pack/plugins/cases/server/connectors/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,25 @@ import {
CasesConnectorsMap,
CasesConnectorTypes,
ICasesConnector,
CreateCasesConnectorFactory,
ICasesConnectorFactory,
} from './types';

const getCasesConnectors = (): CasesConnectorsMap => {
const casesConnectorsMap = new Map<CasesConnectorTypes, ICasesConnector>();
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<CasesConnectorTypes, ICasesConnector<{}>>;

return casesConnectorsMap;
};
constructor() {
this.casesConnectorsMap = new Map<CasesConnectorTypes, ICasesConnector>();
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;
};
}
2 changes: 1 addition & 1 deletion x-pack/plugins/cases/server/connectors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions x-pack/plugins/cases/server/connectors/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ export interface ICasesConnector<TExternalServiceParams = {}> {
export type CasesConnectorTypes = Omit<ConnectorTypes, ConnectorTypes.none>;
export type CasesConnectorsMap = Map<CasesConnectorTypes, ICasesConnector>;

export interface CasesConnectorFactory {
export interface ICasesConnectorFactory {
getCasesConnectors: () => CasesConnectorsMap;
}

export type CreateCasesConnectorFactory = () => CasesConnectorFactory;
2 changes: 0 additions & 2 deletions x-pack/plugins/cases/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ConfigType>();
Expand Down Expand Up @@ -128,7 +127,6 @@ export class CasePlugin {
},
featuresPluginStart: plugins.features,
actionsPluginStart: plugins.actions,
casesConnectorsFactory: createCaseConnectorFactory(),
});

const client = core.elasticsearch.client;
Expand Down

0 comments on commit 37b5f4a

Please sign in to comment.