Skip to content

Commit

Permalink
removed hosted agents error from udpate tags without kuery too
Browse files Browse the repository at this point in the history
  • Loading branch information
juliaElastic committed Dec 16, 2022
1 parent 0edb432 commit 4c25056
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export async function getActionStatuses(
const docCount = matchingBucket?.doc_count ?? 0;
const nbAgentsAck =
action.type === 'UPDATE_TAGS'
? docCount
? Math.min(docCount, nbAgentsActioned)
: Math.min(
docCount,
// only using cardinality count when count lower than precision threshold
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ describe('update_agent_tags', () => {
});

it('should update action results on success', async () => {
esClient.updateByQuery.mockReset();
esClient.updateByQuery.mockResolvedValue({ failures: [], updated: 1, total: 1 } as any);

await updateAgentTags(soClient, esClient, { agentIds: ['agent1'] }, ['one'], []);

const agentAction = esClient.create.mock.calls[0][0] as any;
Expand Down Expand Up @@ -133,11 +136,11 @@ describe('update_agent_tags', () => {
expect(actionResults.body[1].error).not.toBeDefined();
});

it('should write error action results for hosted agent when agentIds are passed', async () => {
it('should skip hosted agent from total when agentIds are passed', async () => {
const { esClient: esClientMock, agentInHostedDoc } = createClientMock();

esClientMock.updateByQuery.mockReset();
esClientMock.updateByQuery.mockResolvedValue({ failures: [], updated: 0, total: '0' } as any);
esClientMock.updateByQuery.mockResolvedValue({ failures: [], updated: 0, total: 0 } as any);

await updateAgentTags(
soClient,
Expand All @@ -153,13 +156,9 @@ describe('update_agent_tags', () => {
action_id: expect.anything(),
agents: [],
type: 'UPDATE_TAGS',
total: 1,
total: 0,
})
);

const errorResults = esClientMock.bulk.mock.calls[0][0] as any;
expect(errorResults.body[1].agent_id).toEqual(agentInHostedDoc._id);
expect(errorResults.body[1].error).toEqual('Cannot modify tags on a hosted agent');
});

it('should write error action results when failures are returned', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,6 @@ export async function updateTagsBatch(
appContextService.getLogger().debug(JSON.stringify(res).slice(0, 1000));

const actionId = options.actionId ?? uuid();
const total = options.total ?? givenAgents.length;

if (options.retryCount === undefined) {
// creating an action doc so that update tags shows up in activity
Expand All @@ -176,8 +175,7 @@ export async function updateTagsBatch(
agents: options.kuery === undefined ? agentIds : [],
created_at: new Date().toISOString(),
type: 'UPDATE_TAGS',
// for kuery cases, the total can be less than the initial selection, as we filter out tags that are already added/removed
total: options.kuery === undefined ? total : res.total,
total: res.total,
});
}

Expand Down Expand Up @@ -207,18 +205,6 @@ export async function updateTagsBatch(
);
}

// writing hosted agent errors - hosted agents filtered out
if (options.kuery === undefined && hostedAgentIds.length > 0) {
await bulkCreateAgentActionResults(
esClient,
hostedAgentIds.map((id) => ({
agentId: id + '',
actionId,
error: hostedAgentError,
}))
);
}

if (res.version_conflicts ?? 0 > 0) {
// write out error results on last retry, so action is not stuck in progress
if (options.retryCount === 3) {
Expand Down

0 comments on commit 4c25056

Please sign in to comment.