Skip to content

Commit

Permalink
Type connector adapter params
Browse files Browse the repository at this point in the history
  • Loading branch information
cnasikas committed Mar 30, 2024
1 parent e59c542 commit c5c06e9
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import Boom from '@hapi/boom';
import { i18n } from '@kbn/i18n';

import { ConnectorAdapter } from './types';
import { ConnectorAdapter, ConnectorAdapterParams } from './types';

export class ConnectorAdapterRegistry {
private readonly connectorAdapters: Map<string, ConnectorAdapter> = new Map();
Expand All @@ -17,14 +17,20 @@ export class ConnectorAdapterRegistry {
return this.connectorAdapters.has(connectorTypeId);
}

public register(connectorAdapter: ConnectorAdapter) {
public register<
RuleActionParams extends ConnectorAdapterParams = ConnectorAdapterParams,
ConnectorParams extends ConnectorAdapterParams = ConnectorAdapterParams
>(connectorAdapter: ConnectorAdapter<RuleActionParams, ConnectorParams>) {
if (this.has(connectorAdapter.connectorTypeId)) {
throw new Error(
`${connectorAdapter.connectorTypeId} is already registered to the ConnectorAdapterRegistry`
);
}

this.connectorAdapters.set(connectorAdapter.connectorTypeId, connectorAdapter);
this.connectorAdapters.set(
connectorAdapter.connectorTypeId,
connectorAdapter as unknown as ConnectorAdapter
);
}

public get(connectorTypeId: string): ConnectorAdapter {
Expand Down
25 changes: 11 additions & 14 deletions x-pack/plugins/alerting/server/connector_adapters/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,27 @@
*/

import { ObjectType } from '@kbn/config-schema';
import type {
RuleActionParams as GenericRuleActionParams,
RuleTypeParams,
SanitizedRule,
} from '../../common';
import type { RuleTypeParams, SanitizedRule } from '../../common';
import { CombinedSummarizedAlerts } from '../types';

type ActionTypeParams = Record<string, unknown>;

type Rule = Pick<SanitizedRule<RuleTypeParams>, 'id' | 'name' | 'tags'>;

interface BuildActionParamsArgs<
RuleActionParams extends GenericRuleActionParams = GenericRuleActionParams
> {
export interface ConnectorAdapterParams {
[x: string]: unknown;
}

interface BuildActionParamsArgs<RuleActionParams> {
alerts: CombinedSummarizedAlerts;
rule: Rule;
params: RuleActionParams;
spaceId: string;
ruleUrl?: string;
}

export interface ConnectorAdapter {
export interface ConnectorAdapter<
RuleActionParams extends ConnectorAdapterParams = ConnectorAdapterParams,
ConnectorParams extends ConnectorAdapterParams = ConnectorAdapterParams
> {
connectorTypeId: string;
/**
* The schema of the action persisted
Expand All @@ -38,7 +37,5 @@ export interface ConnectorAdapter {
* changes.
*/
ruleActionParamsSchema: ObjectType;
buildActionParams: <RuleActionParams extends GenericRuleActionParams>(
args: BuildActionParamsArgs<RuleActionParams>
) => ActionTypeParams;
buildActionParams: (args: BuildActionParamsArgs<RuleActionParams>) => ConnectorParams;
}
16 changes: 13 additions & 3 deletions x-pack/plugins/alerting/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ import {
import { getRulesSettingsFeature } from './rules_settings_feature';
import { maintenanceWindowFeature } from './maintenance_window_feature';
import { ConnectorAdapterRegistry } from './connector_adapters/connector_adapter_registry';
import { ConnectorAdapter } from './connector_adapters/types';
import { ConnectorAdapter, ConnectorAdapterParams } from './connector_adapters/types';
import { DataStreamAdapter, getDataStreamAdapter } from './alerts_service/lib/data_stream_adapter';
import { createGetAlertIndicesAliasFn, GetAlertIndicesAlias } from './lib';

Expand All @@ -120,7 +120,12 @@ export const LEGACY_EVENT_LOG_ACTIONS = {
};

export interface PluginSetupContract {
registerConnectorAdapter(adapter: ConnectorAdapter): void;
registerConnectorAdapter<
RuleActionParams extends ConnectorAdapterParams = ConnectorAdapterParams,
ConnectorParams extends ConnectorAdapterParams = ConnectorAdapterParams
>(
adapter: ConnectorAdapter<RuleActionParams, ConnectorParams>
): void;
registerType<
Params extends RuleTypeParams = RuleTypeParams,
ExtractedParams extends RuleTypeParams = RuleTypeParams,
Expand Down Expand Up @@ -382,7 +387,12 @@ export class AlertingPlugin {
});

return {
registerConnectorAdapter: (adapter: ConnectorAdapter) => {
registerConnectorAdapter: <
RuleActionParams extends ConnectorAdapterParams = ConnectorAdapterParams,
ConnectorParams extends ConnectorAdapterParams = ConnectorAdapterParams
>(
adapter: ConnectorAdapter<RuleActionParams, ConnectorParams>
) => {
this.connectorAdapterRegistry.register(adapter);
},
registerType: <
Expand Down

0 comments on commit c5c06e9

Please sign in to comment.