Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

import type { Client } from '@elastic/elasticsearch';
import expect from '@kbn/expect';
import { asyncForEach } from '@kbn/std';
import pRetry from 'p-retry';

import {
AGENT_ACTIONS_INDEX,
Expand All @@ -16,10 +18,11 @@ import {
type FleetServerAgent,
} from '@kbn/fleet-plugin/common';
import { ENROLLMENT_API_KEYS_INDEX } from '@kbn/fleet-plugin/common/constants';
import { asyncForEach } from '@kbn/std';

const ES_INDEX_OPTIONS = { headers: { 'X-elastic-product-origin': 'fleet' } };

const DELETE_RETRIES = 3;

export async function expectToRejectWithNotFound(fn: any) {
await expectToRejectWithError(fn, /404 "Not Found"/);
}
Expand All @@ -37,68 +40,89 @@ export async function expectToRejectWithError(fn: any, errRegexp: RegExp) {

export async function cleanFleetIndices(esClient: Client) {
await Promise.all([
esClient.deleteByQuery({
index: ENROLLMENT_API_KEYS_INDEX,
q: '*',
ignore_unavailable: true,
refresh: true,
conflicts: 'proceed',
}),
esClient.deleteByQuery({
index: AGENTS_INDEX,
q: '*',
ignore_unavailable: true,
refresh: true,
conflicts: 'proceed',
}),
esClient.deleteByQuery({
index: AGENT_ACTIONS_INDEX,
q: '*',
ignore_unavailable: true,
refresh: true,
conflicts: 'proceed',
}),
pRetry(
() =>
esClient.deleteByQuery({
index: ENROLLMENT_API_KEYS_INDEX,
q: '*',
ignore_unavailable: true,
refresh: true,
}),
{ retries: DELETE_RETRIES }
),
pRetry(
() =>
esClient.deleteByQuery({
index: AGENTS_INDEX,
q: '*',
ignore_unavailable: true,
refresh: true,
}),
{ retries: DELETE_RETRIES }
),
pRetry(
() =>
esClient.deleteByQuery({
index: AGENT_ACTIONS_INDEX,
q: '*',
ignore_unavailable: true,
refresh: true,
}),
{ retries: DELETE_RETRIES }
),
]);
}

export async function cleanFleetAgents(esClient: Client) {
await esClient.deleteByQuery({
index: AGENTS_INDEX,
q: '*',
ignore_unavailable: true,
refresh: true,
conflicts: 'proceed',
});
await pRetry(
() =>
esClient.deleteByQuery({
index: AGENTS_INDEX,
q: '*',
ignore_unavailable: true,
refresh: true,
}),
{ retries: DELETE_RETRIES }
);
}

export async function cleanFleetAgentPolicies(esClient: Client) {
await esClient.deleteByQuery({
index: AGENT_POLICY_INDEX,
q: '*',
refresh: true,
ignore_unavailable: true,
conflicts: 'proceed',
});
await pRetry(
() =>
esClient.deleteByQuery({
index: AGENT_POLICY_INDEX,
q: '*',
refresh: true,
ignore_unavailable: true,
}),
{ retries: DELETE_RETRIES }
);
}

export async function cleanFleetActionIndices(esClient: Client) {
try {
await Promise.all([
esClient.deleteByQuery({
index: AGENT_ACTIONS_INDEX,
q: '*',
ignore_unavailable: true,
refresh: true,
conflicts: 'proceed',
}),
esClient.deleteByQuery(
{
index: AGENT_ACTIONS_RESULTS_INDEX,
q: '*',
refresh: true,
conflicts: 'proceed',
},
ES_INDEX_OPTIONS
pRetry(
() =>
esClient.deleteByQuery({
index: AGENT_ACTIONS_INDEX,
q: '*',
ignore_unavailable: true,
refresh: true,
}),
{ retries: DELETE_RETRIES }
),
pRetry(
() =>
esClient.deleteByQuery(
{
index: AGENT_ACTIONS_RESULTS_INDEX,
q: '*',
refresh: true,
},
ES_INDEX_OPTIONS
),
{ retries: DELETE_RETRIES }
),
]);
} catch (error) {
Expand Down
Loading