Skip to content

Commit

Permalink
Change logic in update endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
criamico committed Oct 19, 2021
1 parent 8eab9fc commit 09d42c9
Showing 1 changed file with 11 additions and 20 deletions.
31 changes: 11 additions & 20 deletions x-pack/plugins/fleet/server/services/package_policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,8 @@ import type {
UpgradePackagePolicyDryRunResponseItem,
RegistryDataStream,
} from '../../common';
import { PACKAGE_POLICY_SAVED_OBJECT_TYPE, AGENT_POLICY_SAVED_OBJECT_TYPE } from '../constants';
import {
HostedAgentPolicyRestrictionRelatedError,
IngestManagerError,
ingestErrorToResponseOptions,
} from '../errors';
import { PACKAGE_POLICY_SAVED_OBJECT_TYPE } from '../constants';
import { IngestManagerError, ingestErrorToResponseOptions } from '../errors';
import { NewPackagePolicySchema, UpdatePackagePolicySchema } from '../types';
import type {
NewPackagePolicy,
Expand All @@ -54,7 +50,6 @@ import type {
PackagePolicySOAttributes,
RegistryPackage,
DryRunPackagePolicy,
AgentPolicy,
} from '../types';
import type { ExternalCallback } from '..';

Expand Down Expand Up @@ -100,7 +95,7 @@ class PackagePolicyService {
kuery: `${PACKAGE_POLICY_SAVED_OBJECT_TYPE}.name: "${packagePolicy.name}"`,
});

// Check that the name does not exist already across all the agent policies
// Check that the name does not exist already
if (existingPoliciesWithName.items.length > 0) {
throw new IngestManagerError('There is already a package with the same name');
}
Expand Down Expand Up @@ -355,18 +350,14 @@ class PackagePolicyService {
throw new Error('Package policy not found');
}

// Check that its agent policy does not have a package policy with the same name
const parentAgentPolicy = await agentPolicyService.get(soClient, packagePolicy.policy_id);
if (!parentAgentPolicy) {
throw new Error('Agent policy not found');
}
if (
(parentAgentPolicy.package_policies as PackagePolicy[]).find(
(siblingPackagePolicy) =>
siblingPackagePolicy.id !== id && siblingPackagePolicy.name === packagePolicy.name
)
) {
throw new Error('There is already a package with the same name on this agent policy');
const existingPoliciesWithName = await this.list(soClient, {
perPage: 1,
kuery: `${PACKAGE_POLICY_SAVED_OBJECT_TYPE}.name: "${packagePolicy.name}"`,
});

// Check that the name does not exist already
if (existingPoliciesWithName.items.length > 0) {
throw new IngestManagerError('There is already a package with the same name');
}

let inputs = restOfPackagePolicy.inputs.map((input) =>
Expand Down

0 comments on commit 09d42c9

Please sign in to comment.