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
This commit is contained in:
Pierre Gayvallet 2021-04-18 20:42:07 +02:00 committed by GitHub
parent 05bd1c0cdb
commit f8838e3b89
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 30 additions and 53 deletions

View file

@ -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;

View file

@ -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;
}

View file

@ -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,

View file

@ -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<TelemetryResponse>({
type: 'sample-data',
fetch: fetchProvider(index),

View file

@ -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<ServiceStatus>;
let mockApiCaller: jest.Mocked<LegacyAPICaller>;
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' }),
},

View file

@ -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,
};