From f8838e3b89abbcf155c9a2381ad631af69cc4864 Mon Sep 17 00:00:00 2001 From: Pierre Gayvallet Date: Sun, 18 Apr 2021 20:42:07 +0200 Subject: [PATCH] Remove legacy ES client usages in `home` and `xpack_legacy` (#97359) * Home plugin: remove usages of the legacy ES client * remove legacy es client usage in xpack_legacy --- .../services/sample_data/routes/install.ts | 20 +++++++++---------- .../services/sample_data/routes/list.ts | 20 +++++++++---------- .../services/sample_data/routes/uninstall.ts | 10 ++++------ .../services/sample_data/usage/collector.ts | 17 ++++++---------- .../server/routes/settings.test.ts | 14 ++----------- .../xpack_legacy/server/routes/settings.ts | 2 -- 6 files changed, 30 insertions(+), 53 deletions(-) diff --git a/src/plugins/home/server/services/sample_data/routes/install.ts b/src/plugins/home/server/services/sample_data/routes/install.ts index a20c3e350222..e5ff33d5c199 100644 --- a/src/plugins/home/server/services/sample_data/routes/install.ts +++ b/src/plugins/home/server/services/sample_data/routes/install.ts @@ -7,7 +7,7 @@ */ import { schema } from '@kbn/config-schema'; -import { IRouter, Logger, RequestHandlerContext } from 'src/core/server'; +import { IRouter, Logger, IScopedClusterClient } from 'src/core/server'; import { SampleDatasetSchema } from '../lib/sample_dataset_registry_types'; import { createIndexName } from '../lib/create_index_name'; import { @@ -22,7 +22,7 @@ const insertDataIntoIndex = ( dataIndexConfig: any, index: string, nowReference: string, - context: RequestHandlerContext, + esClient: IScopedClusterClient, logger: Logger ) => { function updateTimestamps(doc: any) { @@ -51,9 +51,11 @@ const insertDataIntoIndex = ( bulk.push(insertCmd); bulk.push(updateTimestamps(doc)); }); - const resp = await context.core.elasticsearch.legacy.client.callAsCurrentUser('bulk', { + + const { body: resp } = await esClient.asCurrentUser.bulk({ body: bulk, }); + if (resp.errors) { const errMsg = `sample_data install errors while bulk inserting. Elasticsearch response: ${JSON.stringify( resp, @@ -100,7 +102,7 @@ export function createInstallRoute( // clean up any old installation of dataset try { - await context.core.elasticsearch.legacy.client.callAsCurrentUser('indices.delete', { + await context.core.elasticsearch.client.asCurrentUser.indices.delete({ index, }); } catch (err) { @@ -108,17 +110,13 @@ export function createInstallRoute( } try { - const createIndexParams = { + await context.core.elasticsearch.client.asCurrentUser.indices.create({ index, body: { settings: { index: { number_of_shards: 1, auto_expand_replicas: '0-1' } }, mappings: { properties: dataIndexConfig.fields }, }, - }; - await context.core.elasticsearch.legacy.client.callAsCurrentUser( - 'indices.create', - createIndexParams - ); + }); } catch (err) { const errMsg = `Unable to create sample data index "${index}", error: ${err.message}`; logger.warn(errMsg); @@ -130,7 +128,7 @@ export function createInstallRoute( dataIndexConfig, index, nowReference, - context, + context.core.elasticsearch.client, logger ); (counts as any)[index] = count; diff --git a/src/plugins/home/server/services/sample_data/routes/list.ts b/src/plugins/home/server/services/sample_data/routes/list.ts index 86e286644f93..72d8c31cbafd 100644 --- a/src/plugins/home/server/services/sample_data/routes/list.ts +++ b/src/plugins/home/server/services/sample_data/routes/list.ts @@ -36,22 +36,20 @@ export const createListRoute = (router: IRouter, sampleDatasets: SampleDatasetSc const dataIndexConfig = sampleDataset.dataIndices[i]; const index = createIndexName(sampleDataset.id, dataIndexConfig.id); try { - const indexExists = await context.core.elasticsearch.legacy.client.callAsCurrentUser( - 'indices.exists', - { index } - ); + const { + body: indexExists, + } = await context.core.elasticsearch.client.asCurrentUser.indices.exists({ + index, + }); if (!indexExists) { sampleDataset.status = NOT_INSTALLED; return; } - const { count } = await context.core.elasticsearch.legacy.client.callAsCurrentUser( - 'count', - { - index, - } - ); - if (count === 0) { + const { body: count } = await context.core.elasticsearch.client.asCurrentUser.count({ + index, + }); + if (count.count === 0) { sampleDataset.status = NOT_INSTALLED; return; } diff --git a/src/plugins/home/server/services/sample_data/routes/uninstall.ts b/src/plugins/home/server/services/sample_data/routes/uninstall.ts index aa8ed67cf840..3108c06492dd 100644 --- a/src/plugins/home/server/services/sample_data/routes/uninstall.ts +++ b/src/plugins/home/server/services/sample_data/routes/uninstall.ts @@ -28,11 +28,7 @@ export function createUninstallRoute( async ( { core: { - elasticsearch: { - legacy: { - client: { callAsCurrentUser }, - }, - }, + elasticsearch: { client: esClient }, savedObjects: { getClient: getSavedObjectsClient, typeRegistry }, }, }, @@ -50,7 +46,9 @@ export function createUninstallRoute( const index = createIndexName(sampleDataset.id, dataIndexConfig.id); try { - await callAsCurrentUser('indices.delete', { index }); + await esClient.asCurrentUser.indices.delete({ + index, + }); } catch (err) { return response.customError({ statusCode: err.status, diff --git a/src/plugins/home/server/services/sample_data/usage/collector.ts b/src/plugins/home/server/services/sample_data/usage/collector.ts index 81958a2e3c87..df7d485c1f6f 100644 --- a/src/plugins/home/server/services/sample_data/usage/collector.ts +++ b/src/plugins/home/server/services/sample_data/usage/collector.ts @@ -6,22 +6,17 @@ * Side Public License, v 1. */ -import { PluginInitializerContext } from 'kibana/server'; -import { first } from 'rxjs/operators'; +import type { PluginInitializerContext } from 'kibana/server'; +import type { UsageCollectionSetup } from '../../../../../usage_collection/server'; import { fetchProvider, TelemetryResponse } from './collector_fetch'; -import { UsageCollectionSetup } from '../../../../../usage_collection/server'; -export async function makeSampleDataUsageCollector( +export function makeSampleDataUsageCollector( usageCollection: UsageCollectionSetup, context: PluginInitializerContext ) { - let index: string; - try { - const config = await context.config.legacy.globalConfig$.pipe(first()).toPromise(); - index = config.kibana.index; - } catch (err) { - return; // kibana plugin is not enabled (test environment) - } + const config = context.config.legacy.get(); + const index = config.kibana.index; + const collector = usageCollection.makeUsageCollector({ type: 'sample-data', fetch: fetchProvider(index), diff --git a/x-pack/plugins/xpack_legacy/server/routes/settings.test.ts b/x-pack/plugins/xpack_legacy/server/routes/settings.test.ts index 08b5a0f60521..2034a4e5b74b 100644 --- a/x-pack/plugins/xpack_legacy/server/routes/settings.test.ts +++ b/x-pack/plugins/xpack_legacy/server/routes/settings.test.ts @@ -9,11 +9,7 @@ import { BehaviorSubject } from 'rxjs'; import { UnwrapPromise } from '@kbn/utility-types'; import supertest from 'supertest'; -import { - LegacyAPICaller, - ServiceStatus, - ServiceStatusLevels, -} from '../../../../../src/core/server'; +import { ServiceStatus, ServiceStatusLevels } from '../../../../../src/core/server'; import { contextServiceMock, elasticsearchServiceMock, @@ -31,24 +27,18 @@ export function mockGetClusterInfo(clusterInfo: any) { esClient.info.mockResolvedValue({ body: { ...clusterInfo } }); return esClient; } + describe('/api/settings', () => { let server: HttpService; let httpSetup: HttpSetup; let overallStatus$: BehaviorSubject; - let mockApiCaller: jest.Mocked; beforeEach(async () => { - mockApiCaller = jest.fn(); server = createHttpServer(); httpSetup = await server.setup({ context: contextServiceMock.createSetupContract({ core: { elasticsearch: { - legacy: { - client: { - callAsCurrentUser: mockApiCaller, - }, - }, client: { asCurrentUser: mockGetClusterInfo({ cluster_uuid: 'yyy-yyyyy' }), }, diff --git a/x-pack/plugins/xpack_legacy/server/routes/settings.ts b/x-pack/plugins/xpack_legacy/server/routes/settings.ts index 9117637b70be..b9052ca0c84e 100644 --- a/x-pack/plugins/xpack_legacy/server/routes/settings.ts +++ b/x-pack/plugins/xpack_legacy/server/routes/settings.ts @@ -42,9 +42,7 @@ export function registerSettingsRoute({ validate: false, }, async (context, req, res) => { - const { callAsCurrentUser } = context.core.elasticsearch.legacy.client; const collectorFetchContext = { - callCluster: callAsCurrentUser, esClient: context.core.elasticsearch.client.asCurrentUser, soClient: context.core.savedObjects.client, };