Skip to content

Commit

Permalink
simplified logic to use retry for all kuery actions
Browse files Browse the repository at this point in the history
  • Loading branch information
juliaElastic committed Dec 19, 2022
1 parent c5eb223 commit 53e670b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 51 deletions.
41 changes: 13 additions & 28 deletions x-pack/plugins/fleet/server/services/agents/update_agent_tags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ import type { ElasticsearchClient, SavedObjectsClientContract } from '@kbn/core/
import type { Agent } from '../../types';
import { AgentReassignmentError } from '../../errors';

import { SO_SEARCH_LIMIT } from '../../constants';

import { getAgentDocuments, getAgentsByKuery } from './crud';
import { getAgentDocuments } from './crud';
import type { GetAgentsOptions } from '.';
import { searchHitToAgent } from './helpers';
import { UpdateAgentTagsActionRunner, updateTagsBatch } from './update_agent_tags_action_runner';
Expand All @@ -30,7 +28,7 @@ export async function updateAgentTags(
tagsToRemove: string[]
): Promise<{ actionId: string }> {
const outgoingErrors: Record<Agent['id'], Error> = {};
let givenAgents: Agent[] = [];
const givenAgents: Agent[] = [];

if ('agentIds' in options) {
const givenAgentsResults = await getAgentDocuments(esClient, options.agentIds);
Expand All @@ -44,30 +42,17 @@ export async function updateAgentTags(
}
}
} else if ('kuery' in options) {
const batchSize = options.batchSize ?? SO_SEARCH_LIMIT;
const res = await getAgentsByKuery(esClient, {
kuery: options.kuery,
showInactive: options.showInactive ?? false,
page: 1,
perPage: batchSize,
});
if (res.total <= batchSize) {
givenAgents = res.agents;
} else {
return await new UpdateAgentTagsActionRunner(
esClient,
soClient,
{
...options,
batchSize,
total: res.total,
kuery: options.kuery,
tagsToAdd,
tagsToRemove,
},
{ pitId: '' }
).runActionAsyncWithRetry();
}
return await new UpdateAgentTagsActionRunner(
esClient,
soClient,
{
...options,
kuery: options.kuery,
tagsToAdd,
tagsToRemove,
},
{ pitId: '' }
).runActionAsyncWithRetry();
}

return await updateTagsBatch(soClient, esClient, givenAgents, outgoingErrors, {
Expand Down
25 changes: 2 additions & 23 deletions x-pack/test/fleet_api_integration/apis/agents/update_agent_tags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,25 +68,6 @@ export default function (providerContext: FtrProviderContext) {
expect(agent2data.body.item.tags).to.eql(['existingTag']);
});

it('should allow to update tags of multiple agents by kuery', async () => {
await supertest
.post(`/api/fleet/agents/bulk_update_agent_tags`)
.set('kbn-xsrf', 'xxx')
.send({
agents: 'active: true',
tagsToAdd: ['newTag'],
tagsToRemove: ['existingTag'],
})
.expect(200);

const { body } = await supertest.get(`/api/fleet/agents`).set('kbn-xsrf', 'xxx');
expect(body.total).to.eql(4);
body.items.forEach((agent: any) => {
expect(agent.tags.includes('newTag')).to.be(true);
expect(agent.tags.includes('existingTag')).to.be(false);
});
});

async function pollResult(
actionId: string,
nbAgentsAck: number,
Expand Down Expand Up @@ -115,15 +96,14 @@ export default function (providerContext: FtrProviderContext) {
});
}

it('should bulk update tags of multiple agents by kuery in batches - add', async () => {
it('should bulk update tags of multiple agents by kuery - add', async () => {
const { body: actionBody } = await supertest
.post(`/api/fleet/agents/bulk_update_agent_tags`)
.set('kbn-xsrf', 'xxx')
.send({
agents: 'active: true',
tagsToAdd: ['newTag'],
tagsToRemove: [],
batchSize: 3,
})
.expect(200);

Expand All @@ -139,15 +119,14 @@ export default function (providerContext: FtrProviderContext) {
await pollResult(actionId, 4, verifyActionResult);
});

it('should bulk update tags of multiple agents by kuery in batches - remove', async () => {
it('should bulk update tags of multiple agents by kuery - remove', async () => {
const { body: actionBody } = await supertest
.post(`/api/fleet/agents/bulk_update_agent_tags`)
.set('kbn-xsrf', 'xxx')
.send({
agents: 'active: true',
tagsToAdd: [],
tagsToRemove: ['existingTag'],
batchSize: 3,
})
.expect(200);

Expand Down

0 comments on commit 53e670b

Please sign in to comment.