Skip to content

Commit

Permalink
refactor to reduce duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
juliaElastic committed Sep 30, 2022
1 parent 4780e87 commit 92932a7
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 79 deletions.
22 changes: 22 additions & 0 deletions x-pack/plugins/fleet/server/services/agents/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import uuid from 'uuid';
import type { ElasticsearchClient } from '@kbn/core/server';

import { appContextService } from '../app_context';
import type {
Agent,
AgentAction,
Expand Down Expand Up @@ -101,6 +102,27 @@ export async function bulkCreateAgentActions(
return actions;
}

export async function createErrorActionResults(
esClient: ElasticsearchClient,
actionId: string,
errors: Record<Agent['id'], Error>
) {
const errorCount = Object.keys(errors).length;
if (errorCount > 0) {
appContextService.getLogger().info(`Writing error action results of ${errorCount} agents`);

// writing out error result for those agents that have errors, so the action is not going to stay in progress forever
await bulkCreateAgentActionResults(
esClient,
Object.keys(errors).map((agentId) => ({
agentId,
actionId,
error: errors[agentId].message,
}))
);
}
}

export async function bulkCreateAgentActionResults(
esClient: ElasticsearchClient,
results: Array<{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { appContextService } from '../app_context';
import { ActionRunner } from './action_runner';

import { bulkUpdateAgents } from './crud';
import { bulkCreateAgentActionResults, createAgentAction } from './actions';
import { createErrorActionResults, createAgentAction } from './actions';
import { getHostedPolicies, isHostedAgent } from './hosted_agent';
import { BulkActionTaskType } from './bulk_actions_resolver';

Expand Down Expand Up @@ -85,8 +85,7 @@ export async function reassignBatch(
);

const actionId = options.actionId ?? uuid();
const errorCount = Object.keys(errors).length;
const total = options.total ?? agentsToUpdate.length + errorCount;
const total = options.total ?? givenAgents.length;

const now = new Date().toISOString();
await createAgentAction(esClient, {
Expand All @@ -100,23 +99,7 @@ export async function reassignBatch(
},
});

if (errorCount > 0) {
appContextService
.getLogger()
.info(
`Skipping ${errorCount} agents, as failed validation (already assigned or assigned to hosted policy)`
);

// writing out error result for those agents that failed validation, so the action is not going to stay in progress forever
await bulkCreateAgentActionResults(
esClient,
Object.keys(errors).map((agentId) => ({
agentId,
actionId,
error: errors[agentId].message,
}))
);
}
await createErrorActionResults(esClient, actionId, errors);

return { actionId };
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { bulkUpdateAgents } from './crud';
import {
bulkCreateAgentActionResults,
createAgentAction,
createErrorActionResults,
getUnenrollAgentActions,
} from './actions';
import { getHostedPolicies, isHostedAgent } from './hosted_agent';
Expand Down Expand Up @@ -95,7 +96,6 @@ export async function unenrollBatch(
);

const actionId = options.actionId ?? uuid();
const errorCount = Object.keys(outgoingErrors).length;
const total = options.total ?? givenAgents.length;

const agentIds = agentsToUpdate.map((agent) => agent.id);
Expand All @@ -116,23 +116,7 @@ export async function unenrollBatch(
});
}

if (errorCount > 0) {
appContextService
.getLogger()
.info(
`Skipping ${errorCount} agents, as failed validation (cannot unenroll from a hosted policy or already unenrolled)`
);

// writing out error result for those agents that failed validation, so the action is not going to stay in progress forever
await bulkCreateAgentActionResults(
esClient,
Object.keys(outgoingErrors).map((agentId) => ({
agentId,
actionId,
error: outgoingErrors[agentId].message,
}))
);
}
await createErrorActionResults(esClient, actionId, outgoingErrors);

return {
actionId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@ import { difference, uniq } from 'lodash';

import type { Agent } from '../../types';

import { appContextService } from '../app_context';

import { ActionRunner } from './action_runner';

import { bulkUpdateAgents } from './crud';
import { BulkActionTaskType } from './bulk_actions_resolver';
import { filterHostedPolicies } from './filter_hosted_agents';
import { bulkCreateAgentActionResults, createAgentAction } from './actions';
import {
createErrorActionResults,
bulkCreateAgentActionResults,
createAgentAction,
} from './actions';

export class UpdateAgentTagsActionRunner extends ActionRunner {
protected async processAgents(agents: Agent[]): Promise<{ actionId: string }> {
Expand Down Expand Up @@ -96,7 +98,6 @@ export async function updateTagsBatch(

const actionId = options.actionId ?? uuid();
const total = options.total ?? givenAgents.length;
const errorCount = Object.keys(errors).length;

// creating an action doc so that update tags shows up in activity
await createAgentAction(esClient, {
Expand All @@ -114,23 +115,7 @@ export async function updateTagsBatch(
}))
);

if (errorCount > 0) {
appContextService
.getLogger()
.info(
`Skipping ${errorCount} agents, as failed validation (cannot modified tags on hosted agents)`
);

// writing out error result for those agents that failed validation, so the action is not going to stay in progress forever
await bulkCreateAgentActionResults(
esClient,
Object.keys(errors).map((agentId) => ({
agentId,
actionId,
error: errors[agentId].message,
}))
);
}
await createErrorActionResults(esClient, actionId, errors);

return { actionId };
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { ActionRunner } from './action_runner';

import type { GetAgentsOptions } from './crud';
import { bulkUpdateAgents } from './crud';
import { bulkCreateAgentActionResults, createAgentAction } from './actions';
import { createErrorActionResults, createAgentAction } from './actions';
import { getHostedPolicies, isHostedAgent } from './hosted_agent';
import { BulkActionTaskType } from './bulk_actions_resolver';

Expand Down Expand Up @@ -121,8 +121,7 @@ export async function upgradeBatch(
);

const actionId = options.actionId ?? uuid();
const errorCount = Object.keys(errors).length;
const total = options.total ?? agentsToUpdate.length + errorCount;
const total = options.total ?? givenAgents.length;

await createAgentAction(esClient, {
id: actionId,
Expand All @@ -135,23 +134,7 @@ export async function upgradeBatch(
...rollingUpgradeOptions,
});

if (errorCount > 0) {
appContextService
.getLogger()
.info(
`Skipping ${errorCount} agents, as failed validation (cannot upgrade hosted agent or agent not upgradeable)`
);

// writing out error result for those agents that failed validation, so the action is not going to stay in progress forever
await bulkCreateAgentActionResults(
esClient,
Object.keys(errors).map((agentId) => ({
agentId,
actionId,
error: errors[agentId].message,
}))
);
}
await createErrorActionResults(esClient, actionId, errors);

return {
actionId,
Expand Down

0 comments on commit 92932a7

Please sign in to comment.