Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Rollup] Migrate to new ES client #95926

Merged
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export const handleEsError = ({
return response.customError({
statusCode,
body: {
message: body.error?.reason,
message: body.error?.reason ?? error.message ?? 'Unknown error',
attributes: {
// The full original ES error object
error: body.error,
Expand Down
142 changes: 0 additions & 142 deletions x-pack/plugins/rollup/server/client/elasticsearch_rollup.ts

This file was deleted.

25 changes: 3 additions & 22 deletions x-pack/plugins/rollup/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,16 @@ import { i18n } from '@kbn/i18n';
import { schema } from '@kbn/config-schema';

import { PLUGIN, CONFIG_ROLLUPS } from '../common';
import { Dependencies, RollupHandlerContext } from './types';
import { Dependencies } from './types';
import { registerApiRoutes } from './routes';
import { License } from './services';
import { registerRollupUsageCollector } from './collectors';
import { rollupDataEnricher } from './rollup_data_enricher';
import { IndexPatternsFetcher } from './shared_imports';
import { elasticsearchJsPlugin } from './client/elasticsearch_rollup';
import { isEsError } from './shared_imports';
import { handleEsError } from './shared_imports';
import { formatEsError } from './lib/format_es_error';
import { getCapabilitiesForRollupIndices } from '../../../../src/plugins/data/server';

async function getCustomEsClient(getStartServices: CoreSetup['getStartServices']) {
const [core] = await getStartServices();
// Extend the elasticsearchJs client with additional endpoints.
const esClientConfig = { plugins: [elasticsearchJsPlugin] };

return core.elasticsearch.legacy.createClient('rollup', esClientConfig);
}

export class RollupPlugin implements Plugin<void, void, any, any> {
private readonly logger: Logger;
private readonly globalConfig$: Observable<SharedGlobalConfig>;
Expand Down Expand Up @@ -82,21 +73,11 @@ export class RollupPlugin implements Plugin<void, void, any, any> {
],
});

http.registerRouteHandlerContext<RollupHandlerContext, 'rollup'>(
'rollup',
async (context, request) => {
this.rollupEsClient = this.rollupEsClient ?? (await getCustomEsClient(getStartServices));
return {
client: this.rollupEsClient.asScoped(request),
};
}
);

registerApiRoutes({
router: http.createRouter(),
license: this.license,
lib: {
isEsError,
handleEsError,
formatEsError,
getCapabilitiesForRollupIndices,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { RouteDependencies } from '../../../types';
export const registerGetRoute = ({
router,
license,
lib: { isEsError, formatEsError, getCapabilitiesForRollupIndices },
lib: { handleEsError, getCapabilitiesForRollupIndices },
}: RouteDependencies) => {
router.get(
{
Expand All @@ -23,18 +23,13 @@ export const registerGetRoute = ({
},
license.guardApiRoute(async (context, request, response) => {
try {
const data = await context.rollup!.client.callAsCurrentUser(
'rollup.rollupIndexCapabilities',
{
indexPattern: '_all',
}
);
const { client: clusterClient } = context.core.elasticsearch;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: not sure why we need to rename client here.

const { body: data } = await clusterClient.asCurrentUser.rollup.getRollupIndexCaps({
index: '_all',
});
return response.ok({ body: getCapabilitiesForRollupIndices(data) });
} catch (err) {
if (isEsError(err)) {
return response.customError({ statusCode: err.statusCode, body: err });
}
throw err;
return handleEsError({ error: err, response });
}
})
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ interface FieldCapability {
scaled_float?: any;
}

interface FieldCapabilities {
fields: FieldCapability[];
}

function isNumericField(fieldCapability: FieldCapability) {
const numericTypes = [
'long',
Expand All @@ -59,7 +55,7 @@ function isNumericField(fieldCapability: FieldCapability) {
export const registerValidateIndexPatternRoute = ({
router,
license,
lib: { isEsError, formatEsError },
lib: { handleEsError },
}: RouteDependencies) => {
router.get(
{
Expand All @@ -71,16 +67,12 @@ export const registerValidateIndexPatternRoute = ({
},
},
license.guardApiRoute(async (context, request, response) => {
const { client: clusterClient } = context.core.elasticsearch;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same nit here :)

try {
const { indexPattern } = request.params;
const [fieldCapabilities, rollupIndexCapabilities]: [
FieldCapabilities,
{ [key: string]: any }
] = await Promise.all([
context.rollup!.client.callAsCurrentUser('rollup.fieldCapabilities', { indexPattern }),
context.rollup!.client.callAsCurrentUser('rollup.rollupIndexCapabilities', {
indexPattern,
}),
const [{ body: fieldCapabilities }, { body: rollupIndexCapabilities }] = await Promise.all([
clusterClient.asCurrentUser.fieldCaps({ index: indexPattern, fields: '*' }),
clusterClient.asCurrentUser.rollup.getRollupIndexCaps({ index: indexPattern }),
]);

const doesMatchIndices = Object.entries(fieldCapabilities.fields).length !== 0;
Expand All @@ -92,23 +84,21 @@ export const registerValidateIndexPatternRoute = ({

const fieldCapabilitiesEntries = Object.entries(fieldCapabilities.fields);

fieldCapabilitiesEntries.forEach(
([fieldName, fieldCapability]: [string, FieldCapability]) => {
if (fieldCapability.date) {
dateFields.push(fieldName);
return;
}
fieldCapabilitiesEntries.forEach(([fieldName, fieldCapability]) => {
if (fieldCapability.date) {
dateFields.push(fieldName);
return;
}

if (isNumericField(fieldCapability)) {
numericFields.push(fieldName);
return;
}
if (isNumericField(fieldCapability)) {
numericFields.push(fieldName);
return;
}

if (fieldCapability.keyword) {
keywordFields.push(fieldName);
}
if (fieldCapability.keyword) {
keywordFields.push(fieldName);
}
);
});

const body = {
doesMatchIndices,
Expand All @@ -132,11 +122,7 @@ export const registerValidateIndexPatternRoute = ({
return response.ok({ body: notFoundBody });
}

if (isEsError(err)) {
return response.customError({ statusCode: err.statusCode, body: err });
}

throw err;
return handleEsError({ error: err, response });
}
})
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { RouteDependencies } from '../../../types';
export const registerCreateRoute = ({
router,
license,
lib: { isEsError, formatEsError },
lib: { handleEsError },
}: RouteDependencies) => {
router.put(
{
Expand All @@ -29,21 +29,19 @@ export const registerCreateRoute = ({
},
},
license.guardApiRoute(async (context, request, response) => {
const { client: clusterClient } = context.core.elasticsearch;
try {
const { id, ...rest } = request.body.job;
// Create job.
await context.rollup!.client.callAsCurrentUser('rollup.createJob', {
await clusterClient.asCurrentUser.rollup.putJob({
id,
body: rest,
});
// Then request the newly created job.
const results = await context.rollup!.client.callAsCurrentUser('rollup.job', { id });
const { body: results } = await clusterClient.asCurrentUser.rollup.getJobs({ id });
return response.ok({ body: results.jobs[0] });
} catch (err) {
if (isEsError(err)) {
return response.customError({ statusCode: err.statusCode, body: err });
}
throw err;
return handleEsError({ error: err, response });
}
})
);
Expand Down
Loading