diff --git a/x-pack/plugins/ingest_manager/server/routes/agent_policy/handlers.ts b/x-pack/plugins/ingest_manager/server/routes/agent_policy/handlers.ts index 311b3bbf7f13b..645ae8880fa61 100644 --- a/x-pack/plugins/ingest_manager/server/routes/agent_policy/handlers.ts +++ b/x-pack/plugins/ingest_manager/server/routes/agent_policy/handlers.ts @@ -132,6 +132,8 @@ export const createAgentPolicyHandler: RequestHandler< }); } + await agentPolicyService.createFleetPolicyChangeAction(soClient, agentPolicy.id); + const body: CreateAgentPolicyResponse = { item: agentPolicy, }; @@ -185,6 +187,7 @@ export const copyAgentPolicyHandler: RequestHandler< user: user || undefined, } ); + const body: CopyAgentPolicyResponse = { item: agentPolicy }; return response.ok({ body, diff --git a/x-pack/plugins/ingest_manager/server/services/agent_policy.ts b/x-pack/plugins/ingest_manager/server/services/agent_policy.ts index 75c16df483a76..31bf3e7207774 100644 --- a/x-pack/plugins/ingest_manager/server/services/agent_policy.ts +++ b/x-pack/plugins/ingest_manager/server/services/agent_policy.ts @@ -34,6 +34,7 @@ import { agentPolicyUpdateEventHandler } from './agent_policy_update'; import { getSettings } from './settings'; import { normalizeKuery, escapeSearchQueryPhrase } from './saved_object'; import { getFullAgentPolicyKibanaConfig } from '../../common/services/full_agent_policy_kibana_config'; +import { isAgentsSetup } from './agents/setup'; const SAVED_OBJECT_TYPE = AGENT_POLICY_SAVED_OBJECT_TYPE; @@ -287,6 +288,8 @@ class AgentPolicyService { throw new Error('Copied agent policy not found'); } + await this.createFleetPolicyChangeAction(soClient, newAgentPolicy.id); + return updatedAgentPolicy; } @@ -433,6 +436,11 @@ class AgentPolicyService { soClient: SavedObjectsClientContract, agentPolicyId: string ) { + // If Agents is not setup skip the creation of POLICY_CHANGE agent actions + // the action will be created during the fleet setup + if (!(await isAgentsSetup(soClient))) { + return; + } const policy = await agentPolicyService.getFullAgentPolicy(soClient, agentPolicyId); if (!policy || !policy.revision) { return; diff --git a/x-pack/test/ingest_manager_api_integration/apis/settings/update.ts b/x-pack/test/ingest_manager_api_integration/apis/settings/update.ts index 2c5d154ab0416..4bd7b81b02d57 100644 --- a/x-pack/test/ingest_manager_api_integration/apis/settings/update.ts +++ b/x-pack/test/ingest_manager_api_integration/apis/settings/update.ts @@ -81,6 +81,24 @@ export default function (providerContext: FtrProviderContext) { }); createdAgentPolicyIds.push(testPolicyRes.item.id); + const beforeRes = await esClient.search({ + index: '.kibana', + body: { + query: { + bool: { + must: [ + { + terms: { + type: ['fleet-agent-actions'], + }, + }, + { match: { 'fleet-agent-actions.policy_id': testPolicyRes.item.id } }, + ], + }, + }, + }, + }); + await supertest .put(`/api/fleet/settings`) .set('kbn-xsrf', 'xxxx') @@ -105,7 +123,7 @@ export default function (providerContext: FtrProviderContext) { }, }); - expect(res.hits.hits.length).equal(2); + expect(res.hits.hits.length).equal(beforeRes.hits.hits.length + 1); }); }); }