Skip to content

Commit

Permalink
Handle bulk update errors
Browse files Browse the repository at this point in the history
  • Loading branch information
nchaulet committed Feb 3, 2021
1 parent 9749bb4 commit c8e1f15
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 31 deletions.
44 changes: 17 additions & 27 deletions x-pack/plugins/fleet/server/routes/agent/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,34 +316,24 @@ export const postBulkAgentsReassignHandler: RequestHandler<
const soClient = context.core.savedObjects.client;
const esClient = context.core.elasticsearch.client.asInternalUser;
try {
// Reassign by array of IDs
if (Array.isArray(request.body.agents)) {
await AgentService.reassignAgents(
soClient,
esClient,
{ agentIds: request.body.agents },
request.body.policy_id
);
} else {
await AgentService.reassignAgents(
soClient,
esClient,
{ kuery: request.body.agents },
request.body.policy_id
);
}
const results = await AgentService.reassignAgents(
soClient,
esClient,
Array.isArray(request.body.agents)
? { agentIds: request.body.agents }
: { kuery: request.body.agents },
request.body.policy_id
);

const body: PostBulkAgentReassignResponse = {};
// TODO fix
// const body: PostBulkAgentReassignResponse = result.saved_objects.reduce((acc, so) => {
// return {
// ...acc,
// [so.id]: {
// success: !so.error,
// error: so.error || undefined,
// },
// };
// }, {});
const body: PostBulkAgentReassignResponse = results.items.reduce((acc, so) => {
return {
...acc,
[so.id]: {
success: !so.error,
error: so.error || undefined,
},
};
}, {});
return response.ok({ body });
} catch (error) {
return defaultIngestErrorHandler({ error, response });
Expand Down
12 changes: 10 additions & 2 deletions x-pack/plugins/fleet/server/services/agents/crud_fleet_server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,15 +208,23 @@ export async function bulkUpdateAgents(
},
},
{
doc: agentSOAttributesToFleetServerAgentDoc(data),
doc: { ...agentSOAttributesToFleetServerAgentDoc(data) },
},
]);

await esClient.bulk({
const res = await esClient.bulk({
body,
index: AGENTS_INDEX,
refresh: 'wait_for',
});

return {
items: res.body.items.map((item: { update: { _id: string; error?: Error } }) => ({
id: item.update._id,
success: !item.update.error,
error: item.update.error,
})),
};
}

export async function deleteAgent(esClient: ElasticsearchClient, agentId: string) {
Expand Down
10 changes: 9 additions & 1 deletion x-pack/plugins/fleet/server/services/agents/crud_so.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,15 @@ export async function bulkUpdateAgents(
})
);

await soClient.bulkUpdate<AgentSOAttributes>(updates);
const res = await soClient.bulkUpdate<AgentSOAttributes>(updates);

return {
items: res.saved_objects.map((so) => ({
id: so.id,
success: !so.error,
error: so.error,
})),
};
}

export async function deleteAgent(soClient: SavedObjectsClientContract, agentId: string) {
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/fleet/server/services/agents/reassign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export async function reassignAgents(
kuery: string;
},
newAgentPolicyId: string
) {
): Promise<{ items: Array<{ id: string; sucess: boolean; error?: Error }> }> {
const agentPolicy = await agentPolicyService.get(soClient, newAgentPolicyId);
if (!agentPolicy) {
throw Boom.notFound(`Agent policy not found: ${newAgentPolicyId}`);
Expand Down

0 comments on commit c8e1f15

Please sign in to comment.