Skip to content

Commit

Permalink
[Fleet] Fix deleting policies belonging to multiple space (#192871)
Browse files Browse the repository at this point in the history
(cherry picked from commit 46606b1)
  • Loading branch information
nchaulet committed Sep 13, 2024
1 parent eafb15c commit 03ea821
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
4 changes: 3 additions & 1 deletion x-pack/plugins/fleet/server/services/agent_policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1205,7 +1205,9 @@ class AgentPolicyService {
});
}

await soClient.delete(savedObjectType, id);
await soClient.delete(savedObjectType, id, {
force: true, // need to delete through multiple space
});
await this.triggerAgentPolicyUpdatedEvent(esClient, 'deleted', id, {
spaceId: soClient.getCurrentNamespace(),
});
Expand Down
5 changes: 4 additions & 1 deletion x-pack/plugins/fleet/server/services/package_policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1369,7 +1369,10 @@ class PackagePolicyClientImpl implements PackagePolicyClient {
const secretsToDelete: string[] = [];
if (idsToDelete.length > 0) {
const { statuses } = await soClient.bulkDelete(
idsToDelete.map((id) => ({ id, type: savedObjectType }))
idsToDelete.map((id) => ({ id, type: savedObjectType })),
{
force: true, // need to delete through multiple space
}
);

statuses.forEach(({ id, success, error }) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,5 +194,32 @@ export default function (providerContext: FtrProviderContext) {
);
});
});

describe('DELETE /agent_policies/{id}', () => {
let policyRes: CreateAgentPolicyResponse;
before(async () => {
const _policyRes = await apiClient.createAgentPolicy();
policyRes = _policyRes;
await apiClient.createPackagePolicy(undefined, {
policy_ids: [policyRes.item.id],
name: `test-nginx-${Date.now()}`,
description: 'test',
package: {
name: 'nginx',
version: '1.20.0',
},
inputs: {},
});
await apiClient.putAgentPolicy(policyRes.item.id, {
name: `test-nginx-${Date.now()}`,
namespace: 'default',
description: 'tata',
space_ids: ['default', TEST_SPACE_1],
});
});
it('should allow to delete an agent policy through multiple spaces', async () => {
await apiClient.deleteAgentPolicy(policyRes.item.id);
});
});
});
}

0 comments on commit 03ea821

Please sign in to comment.