Skip to content

Commit

Permalink
[Rollup] Migrate to new ES client (#95926)
Browse files Browse the repository at this point in the history
* initial pass at es client migration

* fixed potential for not passing in an error message and triggering an unhandled exception

* reworked ad hoc fixing of error response

* delete legacy client file and remove use of legacyEs service

* remove unused import

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
  • Loading branch information
jloleysens and kibanamachine committed Apr 7, 2021
1 parent 7584b72 commit 4d43a4f
Show file tree
Hide file tree
Showing 18 changed files with 88 additions and 302 deletions.
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;
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;
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

0 comments on commit 4d43a4f

Please sign in to comment.