Skip to content

Commit

Permalink
devide system actions validation into 2 functions
Browse files Browse the repository at this point in the history
  • Loading branch information
guskovaue committed Oct 24, 2023
1 parent 3957566 commit 35153ca
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Semver from 'semver';
import Boom from '@hapi/boom';
import { SavedObject, SavedObjectsUtils } from '@kbn/core/server';
import { withSpan } from '@kbn/apm-utils';
import { validateSystemActions } from '../../../../lib/validate_system_actions';
import { validateSystemActionsWithRuleTypeId } from '../../../../lib/validate_system_actions';
import { parseDuration } from '../../../../../common/parse_duration';
import { WriteOperations, AlertingAuthorizationEntity } from '../../../../authorization';
import {
Expand Down Expand Up @@ -80,8 +80,7 @@ export async function createRule<Params extends RuleParams = never>(
throw Boom.badRequest(`Error validating create data - ${error.message}`);
}

await validateSystemActions({
actionsClient,
validateSystemActionsWithRuleTypeId({
connectorAdapterRegistry: context.connectorAdapterRegistry,
systemActions,
});
Expand Down
27 changes: 23 additions & 4 deletions x-pack/plugins/alerting/server/lib/validate_system_actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,43 @@ import { ConnectorAdapterRegistry } from '../connector_adapters/connector_adapte
import { bulkValidateConnectorAdapterActionParams } from '../connector_adapters/validate_rule_action_params';
import { NormalizedSystemAction } from '../rules_client';
import { RuleSystemAction } from '../types';
interface Params {
interface ValidateSystemActionsWithRuleTypeIdParams {
connectorAdapterRegistry: ConnectorAdapterRegistry;
systemActions: RuleSystemAction[];
}

interface ValidateSystemActionsWithoutRuleTypeIdParams {
actionsClient: ActionsClient;
connectorAdapterRegistry: ConnectorAdapterRegistry;
systemActions: Array<RuleSystemAction | NormalizedSystemAction>;
}

export const validateSystemActions = async ({
export const validateSystemActionsWithRuleTypeId = ({
connectorAdapterRegistry,
systemActions,
}: ValidateSystemActionsWithRuleTypeIdParams) => {
if (systemActions.length === 0) {
return;
}

bulkValidateConnectorAdapterActionParams({
connectorAdapterRegistry,
actions: systemActions,
});
};

export const validateSystemActionsWithoutRuleTypeId = async ({
actionsClient,
connectorAdapterRegistry,
systemActions,
}: Params) => {
}: ValidateSystemActionsWithoutRuleTypeIdParams) => {
if (systemActions.length === 0) {
return;
}

/**
* When updating or creating a rule the actions may not contain
* the actionTypeId. We need to getBulk using the
* the actionTypeId (for example: bulk operations). We need to getBulk using the
* actionsClient to get the actionTypeId of each action.
* The actionTypeId is needed to get the schema of
* the action params using the connector adapter registry
Expand Down

0 comments on commit 35153ca

Please sign in to comment.