Skip to content

Commit

Permalink
add the _clean endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
pgayvallet committed Jan 26, 2023
1 parent 7efb27f commit 0ec638a
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 74 deletions.
125 changes: 51 additions & 74 deletions packages/kbn-test/src/kbn_client/kbn_client_saved_objects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,22 +54,15 @@ interface MigrateResponse {
result: Array<{ status: string }>;
}

interface FindApiResponse {
saved_objects: Array<{
type: string;
id: string;
[key: string]: unknown;
}>;
total: number;
per_page: number;
page: number;
}

interface CleanOptions {
space?: string;
types: string[];
}

interface CleanApiResponse {
deleted: number;
}

interface DeleteObjectsOptions {
space?: string;
objects: Array<{
Expand All @@ -80,6 +73,42 @@ interface DeleteObjectsOptions {

const DELETE_CHUNK_SIZE = 50;

// add types here
const STANDARD_LIST_TYPES = [
'url',
'index-pattern',
'action',
'query',
'alert',
'graph-workspace',
'tag',
'visualization',
'canvas-element',
'canvas-workpad',
'dashboard',
'search',
'lens',
'map',
'cases',
'uptime-dynamic-settings',
'osquery-saved-query',
'osquery-pack',
'infrastructure-ui-source',
'metrics-explorer-view',
'inventory-view',
'infrastructure-monitoring-log-view',
'apm-indices',
// Fleet saved object types
'ingest-outputs',
'ingest-download-sources',
'ingest-agent-policies',
'ingest-package-policies',
'epm-packages',
'epm-packages-assets',
'fleet-preconfiguration-deletion-record',
'fleet-fleet-server-host',
];

/**
* SO client for FTR.
*
Expand Down Expand Up @@ -185,74 +214,22 @@ export class KbnClientSavedObjects {
public async clean(options: CleanOptions) {
this.log.debug('Cleaning all saved objects', { space: options.space });

let deleted = 0;

while (true) {
const resp = await this.requester.request<FindApiResponse>({
method: 'GET',
path: options.space
? uriencode`/s/${options.space}/internal/ftr/kbn_client_so/_find`
: `/internal/ftr/kbn_client_so/_find`,
query: {
per_page: 1000,
type: options.types,
fields: 'none',
},
});

this.log.info('deleting batch of', resp.data.saved_objects.length, 'objects');
const deletion = await this.bulkDelete({
space: options.space,
objects: resp.data.saved_objects,
});
deleted += deletion.deleted;

if (resp.data.total <= resp.data.per_page) {
break;
}
}
const resp = await this.requester.request<CleanApiResponse>({
method: 'POST',
path: options.space
? uriencode`/s/${options.space}/internal/ftr/kbn_client_so/_clean`
: `/internal/ftr/kbn_client_so/_clean`,
body: {
types: options.types,
},
});
const deleted = resp.data.deleted;

this.log.success('deleted', deleted, 'objects');
}

public async cleanStandardList(options?: { space?: string }) {
// add types here
const types = [
'url',
'index-pattern',
'action',
'query',
'alert',
'graph-workspace',
'tag',
'visualization',
'canvas-element',
'canvas-workpad',
'dashboard',
'search',
'lens',
'map',
'cases',
'uptime-dynamic-settings',
'osquery-saved-query',
'osquery-pack',
'infrastructure-ui-source',
'metrics-explorer-view',
'inventory-view',
'infrastructure-monitoring-log-view',
'apm-indices',
// Fleet saved object types
'ingest-outputs',
'ingest-download-sources',
'ingest-agent-policies',
'ingest-package-policies',
'epm-packages',
'epm-packages-assets',
'fleet-preconfiguration-deletion-record',
'fleet-fleet-server-host',
];

const newOptions = { types, space: options?.space };
const newOptions = { types: STANDARD_LIST_TYPES, space: options?.space };
await this.clean(newOptions);
}

Expand Down
48 changes: 48 additions & 0 deletions src/plugins/ftr_apis/server/routes/kbn_client_so/clean.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import type { IRouter } from '@kbn/core/server';
import { schema } from '@kbn/config-schema';
import { KBN_CLIENT_API_PREFIX, listHiddenTypes, catchAndReturnBoomErrors } from './utils';

export const registerCleanRoute = (router: IRouter) => {
router.post(
{
path: `${KBN_CLIENT_API_PREFIX}/_clean`,
options: {
tags: ['access:ftrApis'],
},
validate: {
body: schema.object({
types: schema.arrayOf(schema.string()),
}),
},
},
catchAndReturnBoomErrors(async (ctx, req, res) => {
const { types } = req.body;
const { savedObjects } = await ctx.core;
const hiddenTypes = listHiddenTypes(savedObjects.typeRegistry);
const soClient = savedObjects.getClient({ includedHiddenTypes: hiddenTypes });

const finder = soClient.createPointInTimeFinder({ type: types, perPage: 100 });
let deleted = 0;

for await (const response of finder.find()) {
const objects = response.saved_objects.map(({ type, id }) => ({ type, id }));
const { statuses } = await soClient.bulkDelete(objects);
deleted += statuses.filter((status) => status.success).length;
}

return res.ok({
body: {
deleted,
},
});
})
);
};
2 changes: 2 additions & 0 deletions src/plugins/ftr_apis/server/routes/kbn_client_so/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { registerDeleteRoute } from './delete';
import { registerFindRoute } from './find';
import { registerGetRoute } from './get';
import { registerUpdateRoute } from './update';
import { registerCleanRoute } from './clean';

export const registerKbnClientSoRoutes = (router: IRouter) => {
registerBulkDeleteRoute(router);
Expand All @@ -21,4 +22,5 @@ export const registerKbnClientSoRoutes = (router: IRouter) => {
registerFindRoute(router);
registerGetRoute(router);
registerUpdateRoute(router);
registerCleanRoute(router);
};

0 comments on commit 0ec638a

Please sign in to comment.