Skip to content

Commit

Permalink
Remove legacy ES client usages in home and xpack_legacy (#97359)
Browse files Browse the repository at this point in the history
* Home plugin: remove usages of the legacy ES client

* remove legacy es client usage in xpack_legacy
  • Loading branch information
pgayvallet committed Apr 18, 2021
1 parent 0c81ea5 commit f900251
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 53 deletions.
20 changes: 9 additions & 11 deletions src/plugins/home/server/services/sample_data/routes/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -22,7 +22,7 @@ const insertDataIntoIndex = (
dataIndexConfig: any,
index: string,
nowReference: string,
context: RequestHandlerContext,
esClient: IScopedClusterClient,
logger: Logger
) => {
function updateTimestamps(doc: any) {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -100,25 +102,21 @@ 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) {
// ignore delete errors
}

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);
Expand All @@ -130,7 +128,7 @@ export function createInstallRoute(
dataIndexConfig,
index,
nowReference,
context,
context.core.elasticsearch.client,
logger
);
(counts as any)[index] = count;
Expand Down
20 changes: 9 additions & 11 deletions src/plugins/home/server/services/sample_data/routes/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
10 changes: 4 additions & 6 deletions src/plugins/home/server/services/sample_data/routes/uninstall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,7 @@ export function createUninstallRoute(
async (
{
core: {
elasticsearch: {
legacy: {
client: { callAsCurrentUser },
},
},
elasticsearch: { client: esClient },
savedObjects: { getClient: getSavedObjectsClient, typeRegistry },
},
},
Expand All @@ -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,
Expand Down
17 changes: 6 additions & 11 deletions src/plugins/home/server/services/sample_data/usage/collector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
14 changes: 2 additions & 12 deletions x-pack/plugins/xpack_legacy/server/routes/settings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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' }),
},
Expand Down
2 changes: 0 additions & 2 deletions x-pack/plugins/xpack_legacy/server/routes/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down

0 comments on commit f900251

Please sign in to comment.