From 2c3660df0acab69d7ceb9d9fc43b7a05773975fb Mon Sep 17 00:00:00 2001 From: Julia Bardi Date: Fri, 16 Dec 2022 11:26:07 +0100 Subject: [PATCH] fix and test --- .../services/agents/update_agent_tags.test.ts | 21 +++++++++++++++++++ .../agents/update_agent_tags_action_runner.ts | 7 +++---- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/x-pack/plugins/fleet/server/services/agents/update_agent_tags.test.ts b/x-pack/plugins/fleet/server/services/agents/update_agent_tags.test.ts index c1170c5055043..1998f346dc726 100644 --- a/x-pack/plugins/fleet/server/services/agents/update_agent_tags.test.ts +++ b/x-pack/plugins/fleet/server/services/agents/update_agent_tags.test.ts @@ -112,6 +112,27 @@ describe('update_agent_tags', () => { expect(actionResults.body[1].error).not.toBeDefined(); }); + it('should update action results on success - kuery', async () => { + await updateTagsBatch( + soClient, + esClient, + [], + {}, + { + tagsToAdd: ['new'], + tagsToRemove: [], + kuery: '', + } + ); + + const actionResults = esClient.bulk.mock.calls[0][0] as any; + const agentIds = actionResults?.body + ?.filter((i: any) => i.agent_id) + .map((i: any) => i.agent_id); + expect(agentIds[0]).toHaveLength(36); // uuid + expect(actionResults.body[1].error).not.toBeDefined(); + }); + it('should write error action results for hosted agent when agentIds are passed', async () => { const { esClient: esClientMock, agentInHostedDoc } = createClientMock(); diff --git a/x-pack/plugins/fleet/server/services/agents/update_agent_tags_action_runner.ts b/x-pack/plugins/fleet/server/services/agents/update_agent_tags_action_runner.ts index 1be5ccb17e73e..4e757e92624ec 100644 --- a/x-pack/plugins/fleet/server/services/agents/update_agent_tags_action_runner.ts +++ b/x-pack/plugins/fleet/server/services/agents/update_agent_tags_action_runner.ts @@ -182,15 +182,14 @@ export async function updateTagsBatch( } // creating unique ids to use as agentId, as we don't have all agent ids in case of action by kuery - const getArray = (count: number) => [...Array(count).keys()]; + const getArray = (count: number) => Array.from({ length: count }, () => uuid()); // writing successful action results if (res.updated ?? 0 > 0) { await bulkCreateAgentActionResults( esClient, - - (options.kuery === undefined ? agentIds : getArray(res.updated!)).map(() => ({ - agentId: uuid(), + (options.kuery === undefined ? agentIds : getArray(res.updated!)).map((id) => ({ + agentId: id, actionId, })) );