[ML] Refactor in preparation for new es client (#74552)

* [ML] Refactor in preparation for new es client

* removing commented out code

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
This commit is contained in:
James Gowdy 2020-08-11 08:53:16 +01:00 committed by GitHub
parent 6bea373e8f
commit 01d8f00b50
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
18 changed files with 369 additions and 339 deletions

View file

@ -6,30 +6,47 @@
import {
KibanaRequest,
KibanaResponseFactory,
RequestHandler,
RequestHandlerContext,
ILegacyScopedClusterClient,
IScopedClusterClient,
RequestHandler,
} from 'kibana/server';
import { MlLicense } from '../../../common/license';
type Handler = (handlerParams: {
legacyClient: ILegacyScopedClusterClient;
client: IScopedClusterClient;
request: KibanaRequest<any, any, any, any>;
response: KibanaResponseFactory;
context: RequestHandlerContext;
}) => ReturnType<RequestHandler>;
export class MlServerLicense extends MlLicense {
public fullLicenseAPIGuard(handler: RequestHandler<any, any, any>) {
public fullLicenseAPIGuard(handler: Handler) {
return guard(() => this.isFullLicense(), handler);
}
public basicLicenseAPIGuard(handler: RequestHandler<any, any, any>) {
public basicLicenseAPIGuard(handler: Handler) {
return guard(() => this.isMinimumLicense(), handler);
}
}
function guard(check: () => boolean, handler: RequestHandler<any, any, any>) {
function guard(check: () => boolean, handler: Handler) {
return (
context: RequestHandlerContext,
request: KibanaRequest,
request: KibanaRequest<any, any, any, any>,
response: KibanaResponseFactory
) => {
if (check() === false) {
return response.forbidden();
}
return handler(context, request, response);
return handler({
legacyClient: context.ml!.mlClient,
client: context.core.elasticsearch.client,
request,
response,
context,
});
};
}

View file

@ -58,9 +58,9 @@ export function annotationRoutes(
tags: ['access:ml:canGetAnnotations'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const { getAnnotations } = annotationServiceProvider(context.ml!.mlClient);
const { getAnnotations } = annotationServiceProvider(legacyClient);
const resp = await getAnnotations(request.body);
return response.ok({
@ -91,16 +91,14 @@ export function annotationRoutes(
tags: ['access:ml:canCreateAnnotation'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const annotationsFeatureAvailable = await isAnnotationsFeatureAvailable(
context.ml!.mlClient
);
const annotationsFeatureAvailable = await isAnnotationsFeatureAvailable(legacyClient);
if (annotationsFeatureAvailable === false) {
throw getAnnotationsFeatureUnavailableErrorMessage();
}
const { indexAnnotation } = annotationServiceProvider(context.ml!.mlClient);
const { indexAnnotation } = annotationServiceProvider(legacyClient);
const currentUser =
securityPlugin !== undefined ? securityPlugin.authc.getCurrentUser(request) : {};
@ -136,17 +134,15 @@ export function annotationRoutes(
tags: ['access:ml:canDeleteAnnotation'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const annotationsFeatureAvailable = await isAnnotationsFeatureAvailable(
context.ml!.mlClient
);
const annotationsFeatureAvailable = await isAnnotationsFeatureAvailable(legacyClient);
if (annotationsFeatureAvailable === false) {
throw getAnnotationsFeatureUnavailableErrorMessage();
}
const annotationId = request.params.annotationId;
const { deleteAnnotation } = annotationServiceProvider(context.ml!.mlClient);
const { deleteAnnotation } = annotationServiceProvider(legacyClient);
const resp = await deleteAnnotation(annotationId);
return response.ok({

View file

@ -43,9 +43,9 @@ export function jobRoutes({ router, mlLicense }: RouteInitialization) {
tags: ['access:ml:canGetJobs'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, response }) => {
try {
const results = await context.ml!.mlClient.callAsInternalUser('ml.jobs');
const results = await legacyClient.callAsInternalUser('ml.jobs');
return response.ok({
body: results,
});
@ -74,10 +74,10 @@ export function jobRoutes({ router, mlLicense }: RouteInitialization) {
tags: ['access:ml:canGetJobs'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const { jobId } = request.params;
const results = await context.ml!.mlClient.callAsInternalUser('ml.jobs', { jobId });
const results = await legacyClient.callAsInternalUser('ml.jobs', { jobId });
return response.ok({
body: results,
});
@ -105,9 +105,9 @@ export function jobRoutes({ router, mlLicense }: RouteInitialization) {
tags: ['access:ml:canGetJobs'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, response }) => {
try {
const results = await context.ml!.mlClient.callAsInternalUser('ml.jobStats');
const results = await legacyClient.callAsInternalUser('ml.jobStats');
return response.ok({
body: results,
});
@ -136,10 +136,10 @@ export function jobRoutes({ router, mlLicense }: RouteInitialization) {
tags: ['access:ml:canGetJobs'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const { jobId } = request.params;
const results = await context.ml!.mlClient.callAsInternalUser('ml.jobStats', { jobId });
const results = await legacyClient.callAsInternalUser('ml.jobStats', { jobId });
return response.ok({
body: results,
});
@ -172,10 +172,10 @@ export function jobRoutes({ router, mlLicense }: RouteInitialization) {
tags: ['access:ml:canCreateJob'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const { jobId } = request.params;
const results = await context.ml!.mlClient.callAsInternalUser('ml.addJob', {
const results = await legacyClient.callAsInternalUser('ml.addJob', {
jobId,
body: request.body,
});
@ -209,10 +209,10 @@ export function jobRoutes({ router, mlLicense }: RouteInitialization) {
tags: ['access:ml:canUpdateJob'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const { jobId } = request.params;
const results = await context.ml!.mlClient.callAsInternalUser('ml.updateJob', {
const results = await legacyClient.callAsInternalUser('ml.updateJob', {
jobId,
body: request.body,
});
@ -244,10 +244,10 @@ export function jobRoutes({ router, mlLicense }: RouteInitialization) {
tags: ['access:ml:canOpenJob'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const { jobId } = request.params;
const results = await context.ml!.mlClient.callAsInternalUser('ml.openJob', {
const results = await legacyClient.callAsInternalUser('ml.openJob', {
jobId,
});
return response.ok({
@ -278,7 +278,7 @@ export function jobRoutes({ router, mlLicense }: RouteInitialization) {
tags: ['access:ml:canCloseJob'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const options: { jobId: string; force?: boolean } = {
jobId: request.params.jobId,
@ -287,7 +287,7 @@ export function jobRoutes({ router, mlLicense }: RouteInitialization) {
if (force !== undefined) {
options.force = force;
}
const results = await context.ml!.mlClient.callAsInternalUser('ml.closeJob', options);
const results = await legacyClient.callAsInternalUser('ml.closeJob', options);
return response.ok({
body: results,
});
@ -316,7 +316,7 @@ export function jobRoutes({ router, mlLicense }: RouteInitialization) {
tags: ['access:ml:canDeleteJob'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const options: { jobId: string; force?: boolean } = {
jobId: request.params.jobId,
@ -325,7 +325,7 @@ export function jobRoutes({ router, mlLicense }: RouteInitialization) {
if (force !== undefined) {
options.force = force;
}
const results = await context.ml!.mlClient.callAsInternalUser('ml.deleteJob', options);
const results = await legacyClient.callAsInternalUser('ml.deleteJob', options);
return response.ok({
body: results,
});
@ -352,9 +352,9 @@ export function jobRoutes({ router, mlLicense }: RouteInitialization) {
tags: ['access:ml:canCreateJob'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const results = await context.ml!.mlClient.callAsInternalUser('ml.validateDetector', {
const results = await legacyClient.callAsInternalUser('ml.validateDetector', {
body: request.body,
});
return response.ok({
@ -387,11 +387,11 @@ export function jobRoutes({ router, mlLicense }: RouteInitialization) {
tags: ['access:ml:canForecastJob'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const jobId = request.params.jobId;
const duration = request.body.duration;
const results = await context.ml!.mlClient.callAsInternalUser('ml.forecast', {
const results = await legacyClient.callAsInternalUser('ml.forecast', {
jobId,
duration,
});
@ -428,9 +428,9 @@ export function jobRoutes({ router, mlLicense }: RouteInitialization) {
tags: ['access:ml:canGetJobs'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const results = await context.ml!.mlClient.callAsInternalUser('ml.records', {
const results = await legacyClient.callAsInternalUser('ml.records', {
jobId: request.params.jobId,
body: request.body,
});
@ -467,9 +467,9 @@ export function jobRoutes({ router, mlLicense }: RouteInitialization) {
tags: ['access:ml:canGetJobs'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const results = await context.ml!.mlClient.callAsInternalUser('ml.buckets', {
const results = await legacyClient.callAsInternalUser('ml.buckets', {
jobId: request.params.jobId,
timestamp: request.params.timestamp,
body: request.body,
@ -507,9 +507,9 @@ export function jobRoutes({ router, mlLicense }: RouteInitialization) {
tags: ['access:ml:canGetJobs'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const results = await context.ml!.mlClient.callAsInternalUser('ml.overallBuckets', {
const results = await legacyClient.callAsInternalUser('ml.overallBuckets', {
jobId: request.params.jobId,
top_n: request.body.topN,
bucket_span: request.body.bucketSpan,
@ -544,9 +544,9 @@ export function jobRoutes({ router, mlLicense }: RouteInitialization) {
tags: ['access:ml:canGetJobs'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const results = await context.ml!.mlClient.callAsInternalUser('ml.categories', {
const results = await legacyClient.callAsInternalUser('ml.categories', {
jobId: request.params.jobId,
categoryId: request.params.categoryId,
});
@ -578,9 +578,9 @@ export function jobRoutes({ router, mlLicense }: RouteInitialization) {
tags: ['access:ml:canGetJobs'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const results = await context.ml!.mlClient.callAsInternalUser('ml.modelSnapshots', {
const results = await legacyClient.callAsInternalUser('ml.modelSnapshots', {
jobId: request.params.jobId,
});
return response.ok({
@ -611,9 +611,9 @@ export function jobRoutes({ router, mlLicense }: RouteInitialization) {
tags: ['access:ml:canGetJobs'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const results = await context.ml!.mlClient.callAsInternalUser('ml.modelSnapshots', {
const results = await legacyClient.callAsInternalUser('ml.modelSnapshots', {
jobId: request.params.jobId,
snapshotId: request.params.snapshotId,
});
@ -647,9 +647,9 @@ export function jobRoutes({ router, mlLicense }: RouteInitialization) {
tags: ['access:ml:canCreateJob'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const results = await context.ml!.mlClient.callAsInternalUser('ml.updateModelSnapshot', {
const results = await legacyClient.callAsInternalUser('ml.updateModelSnapshot', {
jobId: request.params.jobId,
snapshotId: request.params.snapshotId,
body: request.body,
@ -682,9 +682,9 @@ export function jobRoutes({ router, mlLicense }: RouteInitialization) {
tags: ['access:ml:canCreateJob'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const results = await context.ml!.mlClient.callAsInternalUser('ml.deleteModelSnapshot', {
const results = await legacyClient.callAsInternalUser('ml.deleteModelSnapshot', {
jobId: request.params.jobId,
snapshotId: request.params.snapshotId,
});

View file

@ -4,39 +4,43 @@
* you may not use this file except in compliance with the Elastic License.
*/
import { RequestHandlerContext } from 'kibana/server';
import { ILegacyScopedClusterClient } from 'kibana/server';
import { wrapError } from '../client/error_wrapper';
import { RouteInitialization } from '../types';
import { calendarSchema, calendarIdSchema, calendarIdsSchema } from './schemas/calendars_schema';
import { CalendarManager, Calendar, FormCalendar } from '../models/calendar';
function getAllCalendars(context: RequestHandlerContext) {
const cal = new CalendarManager(context.ml!.mlClient);
function getAllCalendars(legacyClient: ILegacyScopedClusterClient) {
const cal = new CalendarManager(legacyClient);
return cal.getAllCalendars();
}
function getCalendar(context: RequestHandlerContext, calendarId: string) {
const cal = new CalendarManager(context.ml!.mlClient);
function getCalendar(legacyClient: ILegacyScopedClusterClient, calendarId: string) {
const cal = new CalendarManager(legacyClient);
return cal.getCalendar(calendarId);
}
function newCalendar(context: RequestHandlerContext, calendar: FormCalendar) {
const cal = new CalendarManager(context.ml!.mlClient);
function newCalendar(legacyClient: ILegacyScopedClusterClient, calendar: FormCalendar) {
const cal = new CalendarManager(legacyClient);
return cal.newCalendar(calendar);
}
function updateCalendar(context: RequestHandlerContext, calendarId: string, calendar: Calendar) {
const cal = new CalendarManager(context.ml!.mlClient);
function updateCalendar(
legacyClient: ILegacyScopedClusterClient,
calendarId: string,
calendar: Calendar
) {
const cal = new CalendarManager(legacyClient);
return cal.updateCalendar(calendarId, calendar);
}
function deleteCalendar(context: RequestHandlerContext, calendarId: string) {
const cal = new CalendarManager(context.ml!.mlClient);
function deleteCalendar(legacyClient: ILegacyScopedClusterClient, calendarId: string) {
const cal = new CalendarManager(legacyClient);
return cal.deleteCalendar(calendarId);
}
function getCalendarsByIds(context: RequestHandlerContext, calendarIds: string) {
const cal = new CalendarManager(context.ml!.mlClient);
function getCalendarsByIds(legacyClient: ILegacyScopedClusterClient, calendarIds: string) {
const cal = new CalendarManager(legacyClient);
return cal.getCalendarsByIds(calendarIds);
}
@ -56,9 +60,9 @@ export function calendars({ router, mlLicense }: RouteInitialization) {
tags: ['access:ml:canGetCalendars'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, response }) => {
try {
const resp = await getAllCalendars(context);
const resp = await getAllCalendars(legacyClient);
return response.ok({
body: resp,
@ -88,15 +92,15 @@ export function calendars({ router, mlLicense }: RouteInitialization) {
tags: ['access:ml:canGetCalendars'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
let returnValue;
try {
const calendarIds = request.params.calendarIds.split(',');
if (calendarIds.length === 1) {
returnValue = await getCalendar(context, calendarIds[0]);
returnValue = await getCalendar(legacyClient, calendarIds[0]);
} else {
returnValue = await getCalendarsByIds(context, calendarIds);
returnValue = await getCalendarsByIds(legacyClient, calendarIds);
}
return response.ok({
@ -127,10 +131,10 @@ export function calendars({ router, mlLicense }: RouteInitialization) {
tags: ['access:ml:canCreateCalendar'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const body = request.body;
const resp = await newCalendar(context, body);
const resp = await newCalendar(legacyClient, body);
return response.ok({
body: resp,
@ -162,11 +166,11 @@ export function calendars({ router, mlLicense }: RouteInitialization) {
tags: ['access:ml:canCreateCalendar'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const { calendarId } = request.params;
const body = request.body;
const resp = await updateCalendar(context, calendarId, body);
const resp = await updateCalendar(legacyClient, calendarId, body);
return response.ok({
body: resp,
@ -196,10 +200,10 @@ export function calendars({ router, mlLicense }: RouteInitialization) {
tags: ['access:ml:canDeleteCalendar'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const { calendarId } = request.params;
const resp = await deleteCalendar(context, calendarId);
const resp = await deleteCalendar(legacyClient, calendarId);
return response.ok({
body: resp,

View file

@ -4,7 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/
import { RequestHandlerContext } from 'kibana/server';
import { RequestHandlerContext, ILegacyScopedClusterClient } from 'kibana/server';
import { wrapError } from '../client/error_wrapper';
import { analyticsAuditMessagesProvider } from '../models/data_frame_analytics/analytics_audit_messages';
import { RouteInitialization } from '../types';
@ -36,13 +36,13 @@ function deleteDestIndexPatternById(context: RequestHandlerContext, indexPattern
*/
export function dataFrameAnalyticsRoutes({ router, mlLicense }: RouteInitialization) {
async function userCanDeleteIndex(
context: RequestHandlerContext,
legacyClient: ILegacyScopedClusterClient,
destinationIndex: string
): Promise<boolean> {
if (!mlLicense.isSecurityEnabled()) {
return true;
}
const privilege = await context.ml!.mlClient.callAsCurrentUser('ml.privilegeCheck', {
const privilege = await legacyClient.callAsCurrentUser('ml.privilegeCheck', {
body: {
index: [
{
@ -76,9 +76,9 @@ export function dataFrameAnalyticsRoutes({ router, mlLicense }: RouteInitializat
tags: ['access:ml:canGetDataFrameAnalytics'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, response }) => {
try {
const results = await context.ml!.mlClient.callAsInternalUser('ml.getDataFrameAnalytics');
const results = await legacyClient.callAsInternalUser('ml.getDataFrameAnalytics');
return response.ok({
body: results,
});
@ -107,10 +107,10 @@ export function dataFrameAnalyticsRoutes({ router, mlLicense }: RouteInitializat
tags: ['access:ml:canGetDataFrameAnalytics'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const { analyticsId } = request.params;
const results = await context.ml!.mlClient.callAsInternalUser('ml.getDataFrameAnalytics', {
const results = await legacyClient.callAsInternalUser('ml.getDataFrameAnalytics', {
analyticsId,
});
return response.ok({
@ -137,11 +137,9 @@ export function dataFrameAnalyticsRoutes({ router, mlLicense }: RouteInitializat
tags: ['access:ml:canGetDataFrameAnalytics'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const results = await context.ml!.mlClient.callAsInternalUser(
'ml.getDataFrameAnalyticsStats'
);
const results = await legacyClient.callAsInternalUser('ml.getDataFrameAnalyticsStats');
return response.ok({
body: results,
});
@ -170,15 +168,12 @@ export function dataFrameAnalyticsRoutes({ router, mlLicense }: RouteInitializat
tags: ['access:ml:canGetDataFrameAnalytics'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const { analyticsId } = request.params;
const results = await context.ml!.mlClient.callAsInternalUser(
'ml.getDataFrameAnalyticsStats',
{
analyticsId,
}
);
const results = await legacyClient.callAsInternalUser('ml.getDataFrameAnalyticsStats', {
analyticsId,
});
return response.ok({
body: results,
});
@ -210,17 +205,14 @@ export function dataFrameAnalyticsRoutes({ router, mlLicense }: RouteInitializat
tags: ['access:ml:canCreateDataFrameAnalytics'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const { analyticsId } = request.params;
const results = await context.ml!.mlClient.callAsInternalUser(
'ml.createDataFrameAnalytics',
{
body: request.body,
analyticsId,
...getAuthorizationHeader(request),
}
);
const results = await legacyClient.callAsInternalUser('ml.createDataFrameAnalytics', {
body: request.body,
analyticsId,
...getAuthorizationHeader(request),
});
return response.ok({
body: results,
});
@ -249,15 +241,12 @@ export function dataFrameAnalyticsRoutes({ router, mlLicense }: RouteInitializat
tags: ['access:ml:canGetDataFrameAnalytics'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const results = await context.ml!.mlClient.callAsInternalUser(
'ml.evaluateDataFrameAnalytics',
{
body: request.body,
...getAuthorizationHeader(request),
}
);
const results = await legacyClient.callAsInternalUser('ml.evaluateDataFrameAnalytics', {
body: request.body,
...getAuthorizationHeader(request),
});
return response.ok({
body: results,
});
@ -287,14 +276,11 @@ export function dataFrameAnalyticsRoutes({ router, mlLicense }: RouteInitializat
tags: ['access:ml:canCreateDataFrameAnalytics'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const results = await context.ml!.mlClient.callAsInternalUser(
'ml.explainDataFrameAnalytics',
{
body: request.body,
}
);
const results = await legacyClient.callAsInternalUser('ml.explainDataFrameAnalytics', {
body: request.body,
});
return response.ok({
body: results,
});
@ -324,7 +310,7 @@ export function dataFrameAnalyticsRoutes({ router, mlLicense }: RouteInitializat
tags: ['access:ml:canDeleteDataFrameAnalytics'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response, context }) => {
try {
const { analyticsId } = request.params;
const { deleteDestIndex, deleteDestIndexPattern } = request.query;
@ -338,7 +324,7 @@ export function dataFrameAnalyticsRoutes({ router, mlLicense }: RouteInitializat
// Check if analyticsId is valid and get destination index
if (deleteDestIndex || deleteDestIndexPattern) {
try {
const dfa = await context.ml!.mlClient.callAsInternalUser('ml.getDataFrameAnalytics', {
const dfa = await legacyClient.callAsInternalUser('ml.getDataFrameAnalytics', {
analyticsId,
});
if (Array.isArray(dfa.data_frame_analytics) && dfa.data_frame_analytics.length > 0) {
@ -351,11 +337,11 @@ export function dataFrameAnalyticsRoutes({ router, mlLicense }: RouteInitializat
// If user checks box to delete the destinationIndex associated with the job
if (destinationIndex && deleteDestIndex) {
// Verify if user has privilege to delete the destination index
const userCanDeleteDestIndex = await userCanDeleteIndex(context, destinationIndex);
const userCanDeleteDestIndex = await userCanDeleteIndex(legacyClient, destinationIndex);
// If user does have privilege to delete the index, then delete the index
if (userCanDeleteDestIndex) {
try {
await context.ml!.mlClient.callAsCurrentUser('indices.delete', {
await legacyClient.callAsCurrentUser('indices.delete', {
index: destinationIndex,
});
destIndexDeleted.success = true;
@ -384,7 +370,7 @@ export function dataFrameAnalyticsRoutes({ router, mlLicense }: RouteInitializat
// Delete the data frame analytics
try {
await context.ml!.mlClient.callAsInternalUser('ml.deleteDataFrameAnalytics', {
await legacyClient.callAsInternalUser('ml.deleteDataFrameAnalytics', {
analyticsId,
});
analyticsJobDeleted.success = true;
@ -427,15 +413,12 @@ export function dataFrameAnalyticsRoutes({ router, mlLicense }: RouteInitializat
tags: ['access:ml:canStartStopDataFrameAnalytics'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const { analyticsId } = request.params;
const results = await context.ml!.mlClient.callAsInternalUser(
'ml.startDataFrameAnalytics',
{
analyticsId,
}
);
const results = await legacyClient.callAsInternalUser('ml.startDataFrameAnalytics', {
analyticsId,
});
return response.ok({
body: results,
});
@ -466,7 +449,7 @@ export function dataFrameAnalyticsRoutes({ router, mlLicense }: RouteInitializat
tags: ['access:ml:canStartStopDataFrameAnalytics'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const options: { analyticsId: string; force?: boolean | undefined } = {
analyticsId: request.params.analyticsId,
@ -477,10 +460,7 @@ export function dataFrameAnalyticsRoutes({ router, mlLicense }: RouteInitializat
options.force = request.url.query.force;
}
const results = await context.ml!.mlClient.callAsInternalUser(
'ml.stopDataFrameAnalytics',
options
);
const results = await legacyClient.callAsInternalUser('ml.stopDataFrameAnalytics', options);
return response.ok({
body: results,
});
@ -510,16 +490,13 @@ export function dataFrameAnalyticsRoutes({ router, mlLicense }: RouteInitializat
tags: ['access:ml:canCreateDataFrameAnalytics'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const { analyticsId } = request.params;
const results = await context.ml!.mlClient.callAsInternalUser(
'ml.updateDataFrameAnalytics',
{
body: request.body,
analyticsId,
}
);
const results = await legacyClient.callAsInternalUser('ml.updateDataFrameAnalytics', {
body: request.body,
analyticsId,
});
return response.ok({
body: results,
});
@ -548,10 +525,10 @@ export function dataFrameAnalyticsRoutes({ router, mlLicense }: RouteInitializat
tags: ['access:ml:canGetDataFrameAnalytics'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const { analyticsId } = request.params;
const { getAnalyticsAuditMessages } = analyticsAuditMessagesProvider(context.ml!.mlClient);
const { getAnalyticsAuditMessages } = analyticsAuditMessagesProvider(legacyClient);
const results = await getAnalyticsAuditMessages(analyticsId);
return response.ok({

View file

@ -4,7 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/
import { RequestHandlerContext } from 'kibana/server';
import { ILegacyScopedClusterClient } from 'kibana/server';
import { wrapError } from '../client/error_wrapper';
import { DataVisualizer } from '../models/data_visualizer';
import { Field, HistogramField } from '../models/data_visualizer/data_visualizer';
@ -17,7 +17,7 @@ import {
import { RouteInitialization } from '../types';
function getOverallStats(
context: RequestHandlerContext,
legacyClient: ILegacyScopedClusterClient,
indexPatternTitle: string,
query: object,
aggregatableFields: string[],
@ -27,7 +27,7 @@ function getOverallStats(
earliestMs: number,
latestMs: number
) {
const dv = new DataVisualizer(context.ml!.mlClient);
const dv = new DataVisualizer(legacyClient);
return dv.getOverallStats(
indexPatternTitle,
query,
@ -41,7 +41,7 @@ function getOverallStats(
}
function getStatsForFields(
context: RequestHandlerContext,
legacyClient: ILegacyScopedClusterClient,
indexPatternTitle: string,
query: any,
fields: Field[],
@ -52,7 +52,7 @@ function getStatsForFields(
interval: number,
maxExamples: number
) {
const dv = new DataVisualizer(context.ml!.mlClient);
const dv = new DataVisualizer(legacyClient);
return dv.getStatsForFields(
indexPatternTitle,
query,
@ -67,13 +67,13 @@ function getStatsForFields(
}
function getHistogramsForFields(
context: RequestHandlerContext,
legacyClient: ILegacyScopedClusterClient,
indexPatternTitle: string,
query: any,
fields: HistogramField[],
samplerShardSize: number
) {
const dv = new DataVisualizer(context.ml!.mlClient);
const dv = new DataVisualizer(legacyClient);
return dv.getHistogramsForFields(indexPatternTitle, query, fields, samplerShardSize);
}
@ -104,7 +104,7 @@ export function dataVisualizerRoutes({ router, mlLicense }: RouteInitialization)
tags: ['access:ml:canAccessML'],
},
},
mlLicense.basicLicenseAPIGuard(async (context, request, response) => {
mlLicense.basicLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const {
params: { indexPatternTitle },
@ -112,7 +112,7 @@ export function dataVisualizerRoutes({ router, mlLicense }: RouteInitialization)
} = request;
const results = await getHistogramsForFields(
context,
legacyClient,
indexPatternTitle,
query,
fields,
@ -151,7 +151,7 @@ export function dataVisualizerRoutes({ router, mlLicense }: RouteInitialization)
tags: ['access:ml:canAccessML'],
},
},
mlLicense.basicLicenseAPIGuard(async (context, request, response) => {
mlLicense.basicLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const {
params: { indexPatternTitle },
@ -168,7 +168,7 @@ export function dataVisualizerRoutes({ router, mlLicense }: RouteInitialization)
} = request;
const results = await getStatsForFields(
context,
legacyClient,
indexPatternTitle,
query,
fields,
@ -216,7 +216,7 @@ export function dataVisualizerRoutes({ router, mlLicense }: RouteInitialization)
tags: ['access:ml:canAccessML'],
},
},
mlLicense.basicLicenseAPIGuard(async (context, request, response) => {
mlLicense.basicLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const {
params: { indexPatternTitle },
@ -232,7 +232,7 @@ export function dataVisualizerRoutes({ router, mlLicense }: RouteInitialization)
} = request;
const results = await getOverallStats(
context,
legacyClient,
indexPatternTitle,
query,
aggregatableFields,

View file

@ -33,9 +33,9 @@ export function dataFeedRoutes({ router, mlLicense }: RouteInitialization) {
tags: ['access:ml:canGetDatafeeds'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const resp = await context.ml!.mlClient.callAsInternalUser('ml.datafeeds');
const resp = await legacyClient.callAsInternalUser('ml.datafeeds');
return response.ok({
body: resp,
@ -65,10 +65,10 @@ export function dataFeedRoutes({ router, mlLicense }: RouteInitialization) {
tags: ['access:ml:canGetDatafeeds'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const datafeedId = request.params.datafeedId;
const resp = await context.ml!.mlClient.callAsInternalUser('ml.datafeeds', { datafeedId });
const resp = await legacyClient.callAsInternalUser('ml.datafeeds', { datafeedId });
return response.ok({
body: resp,
@ -94,9 +94,9 @@ export function dataFeedRoutes({ router, mlLicense }: RouteInitialization) {
tags: ['access:ml:canGetDatafeeds'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const resp = await context.ml!.mlClient.callAsInternalUser('ml.datafeedStats');
const resp = await legacyClient.callAsInternalUser('ml.datafeedStats');
return response.ok({
body: resp,
@ -126,10 +126,10 @@ export function dataFeedRoutes({ router, mlLicense }: RouteInitialization) {
tags: ['access:ml:canGetDatafeeds'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const datafeedId = request.params.datafeedId;
const resp = await context.ml!.mlClient.callAsInternalUser('ml.datafeedStats', {
const resp = await legacyClient.callAsInternalUser('ml.datafeedStats', {
datafeedId,
});
@ -163,10 +163,10 @@ export function dataFeedRoutes({ router, mlLicense }: RouteInitialization) {
tags: ['access:ml:canCreateDatafeed'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const datafeedId = request.params.datafeedId;
const resp = await context.ml!.mlClient.callAsInternalUser('ml.addDatafeed', {
const resp = await legacyClient.callAsInternalUser('ml.addDatafeed', {
datafeedId,
body: request.body,
...getAuthorizationHeader(request),
@ -202,10 +202,10 @@ export function dataFeedRoutes({ router, mlLicense }: RouteInitialization) {
tags: ['access:ml:canUpdateDatafeed'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const datafeedId = request.params.datafeedId;
const resp = await context.ml!.mlClient.callAsInternalUser('ml.updateDatafeed', {
const resp = await legacyClient.callAsInternalUser('ml.updateDatafeed', {
datafeedId,
body: request.body,
...getAuthorizationHeader(request),
@ -241,7 +241,7 @@ export function dataFeedRoutes({ router, mlLicense }: RouteInitialization) {
tags: ['access:ml:canDeleteDatafeed'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const options: { datafeedId: string; force?: boolean } = {
datafeedId: request.params.jobId,
@ -251,7 +251,7 @@ export function dataFeedRoutes({ router, mlLicense }: RouteInitialization) {
options.force = force;
}
const resp = await context.ml!.mlClient.callAsInternalUser('ml.deleteDatafeed', options);
const resp = await legacyClient.callAsInternalUser('ml.deleteDatafeed', options);
return response.ok({
body: resp,
@ -283,12 +283,12 @@ export function dataFeedRoutes({ router, mlLicense }: RouteInitialization) {
tags: ['access:ml:canStartStopDatafeed'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const datafeedId = request.params.datafeedId;
const { start, end } = request.body;
const resp = await context.ml!.mlClient.callAsInternalUser('ml.startDatafeed', {
const resp = await legacyClient.callAsInternalUser('ml.startDatafeed', {
datafeedId,
start,
end,
@ -322,11 +322,11 @@ export function dataFeedRoutes({ router, mlLicense }: RouteInitialization) {
tags: ['access:ml:canStartStopDatafeed'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const datafeedId = request.params.datafeedId;
const resp = await context.ml!.mlClient.callAsInternalUser('ml.stopDatafeed', {
const resp = await legacyClient.callAsInternalUser('ml.stopDatafeed', {
datafeedId,
});
@ -358,10 +358,10 @@ export function dataFeedRoutes({ router, mlLicense }: RouteInitialization) {
tags: ['access:ml:canPreviewDatafeed'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const datafeedId = request.params.datafeedId;
const resp = await context.ml!.mlClient.callAsInternalUser('ml.datafeedPreview', {
const resp = await legacyClient.callAsInternalUser('ml.datafeedPreview', {
datafeedId,
...getAuthorizationHeader(request),
});

View file

@ -4,7 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/
import { RequestHandlerContext } from 'kibana/server';
import { ILegacyScopedClusterClient } from 'kibana/server';
import { wrapError } from '../client/error_wrapper';
import { RouteInitialization } from '../types';
import {
@ -13,14 +13,14 @@ import {
} from './schemas/fields_service_schema';
import { fieldsServiceProvider } from '../models/fields_service';
function getCardinalityOfFields(context: RequestHandlerContext, payload: any) {
const fs = fieldsServiceProvider(context.ml!.mlClient);
function getCardinalityOfFields(legacyClient: ILegacyScopedClusterClient, payload: any) {
const fs = fieldsServiceProvider(legacyClient);
const { index, fieldNames, query, timeFieldName, earliestMs, latestMs } = payload;
return fs.getCardinalityOfFields(index, fieldNames, query, timeFieldName, earliestMs, latestMs);
}
function getTimeFieldRange(context: RequestHandlerContext, payload: any) {
const fs = fieldsServiceProvider(context.ml!.mlClient);
function getTimeFieldRange(legacyClient: ILegacyScopedClusterClient, payload: any) {
const fs = fieldsServiceProvider(legacyClient);
const { index, timeFieldName, query } = payload;
return fs.getTimeFieldRange(index, timeFieldName, query);
}
@ -50,9 +50,9 @@ export function fieldsService({ router, mlLicense }: RouteInitialization) {
tags: ['access:ml:canAccessML'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const resp = await getCardinalityOfFields(context, request.body);
const resp = await getCardinalityOfFields(legacyClient, request.body);
return response.ok({
body: resp,
@ -85,9 +85,9 @@ export function fieldsService({ router, mlLicense }: RouteInitialization) {
tags: ['access:ml:canAccessML'],
},
},
mlLicense.basicLicenseAPIGuard(async (context, request, response) => {
mlLicense.basicLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const resp = await getTimeFieldRange(context, request.body);
const resp = await getTimeFieldRange(legacyClient, request.body);
return response.ok({
body: resp,

View file

@ -5,7 +5,7 @@
*/
import { schema } from '@kbn/config-schema';
import { RequestHandlerContext } from 'kibana/server';
import { ILegacyScopedClusterClient } from 'kibana/server';
import { MAX_FILE_SIZE_BYTES } from '../../common/constants/file_datavisualizer';
import {
InputOverrides,
@ -28,13 +28,17 @@ import {
importFileQuerySchema,
} from './schemas/file_data_visualizer_schema';
function analyzeFiles(context: RequestHandlerContext, data: InputData, overrides: InputOverrides) {
const { analyzeFile } = fileDataVisualizerProvider(context.ml!.mlClient);
function analyzeFiles(
legacyClient: ILegacyScopedClusterClient,
data: InputData,
overrides: InputOverrides
) {
const { analyzeFile } = fileDataVisualizerProvider(legacyClient);
return analyzeFile(data, overrides);
}
function importData(
context: RequestHandlerContext,
legacyClient: ILegacyScopedClusterClient,
id: string,
index: string,
settings: Settings,
@ -42,7 +46,7 @@ function importData(
ingestPipeline: IngestPipelineWrapper,
data: InputData
) {
const { importData: importDataFunc } = importDataProvider(context.ml!.mlClient);
const { importData: importDataFunc } = importDataProvider(legacyClient);
return importDataFunc(id, index, settings, mappings, ingestPipeline, data);
}
@ -74,9 +78,9 @@ export function fileDataVisualizerRoutes({ router, mlLicense }: RouteInitializat
tags: ['access:ml:canFindFileStructure'],
},
},
mlLicense.basicLicenseAPIGuard(async (context, request, response) => {
mlLicense.basicLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const result = await analyzeFiles(context, request.body, request.query);
const result = await analyzeFiles(legacyClient, request.body, request.query);
return response.ok({ body: result });
} catch (e) {
return response.customError(wrapError(e));
@ -109,7 +113,7 @@ export function fileDataVisualizerRoutes({ router, mlLicense }: RouteInitializat
tags: ['access:ml:canFindFileStructure'],
},
},
mlLicense.basicLicenseAPIGuard(async (context, request, response) => {
mlLicense.basicLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const { id } = request.query;
const { index, data, settings, mappings, ingestPipeline } = request.body;
@ -122,7 +126,7 @@ export function fileDataVisualizerRoutes({ router, mlLicense }: RouteInitializat
}
const result = await importData(
context,
legacyClient,
id,
index,
settings,

View file

@ -4,7 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/
import { RequestHandlerContext } from 'kibana/server';
import { ILegacyScopedClusterClient } from 'kibana/server';
import { wrapError } from '../client/error_wrapper';
import { RouteInitialization } from '../types';
import { createFilterSchema, filterIdSchema, updateFilterSchema } from './schemas/filters_schema';
@ -12,33 +12,37 @@ import { FilterManager, FormFilter } from '../models/filter';
// TODO - add function for returning a list of just the filter IDs.
// TODO - add function for returning a list of filter IDs plus item count.
function getAllFilters(context: RequestHandlerContext) {
const mgr = new FilterManager(context.ml!.mlClient);
function getAllFilters(legacyClient: ILegacyScopedClusterClient) {
const mgr = new FilterManager(legacyClient);
return mgr.getAllFilters();
}
function getAllFilterStats(context: RequestHandlerContext) {
const mgr = new FilterManager(context.ml!.mlClient);
function getAllFilterStats(legacyClient: ILegacyScopedClusterClient) {
const mgr = new FilterManager(legacyClient);
return mgr.getAllFilterStats();
}
function getFilter(context: RequestHandlerContext, filterId: string) {
const mgr = new FilterManager(context.ml!.mlClient);
function getFilter(legacyClient: ILegacyScopedClusterClient, filterId: string) {
const mgr = new FilterManager(legacyClient);
return mgr.getFilter(filterId);
}
function newFilter(context: RequestHandlerContext, filter: FormFilter) {
const mgr = new FilterManager(context.ml!.mlClient);
function newFilter(legacyClient: ILegacyScopedClusterClient, filter: FormFilter) {
const mgr = new FilterManager(legacyClient);
return mgr.newFilter(filter);
}
function updateFilter(context: RequestHandlerContext, filterId: string, filter: FormFilter) {
const mgr = new FilterManager(context.ml!.mlClient);
function updateFilter(
legacyClient: ILegacyScopedClusterClient,
filterId: string,
filter: FormFilter
) {
const mgr = new FilterManager(legacyClient);
return mgr.updateFilter(filterId, filter);
}
function deleteFilter(context: RequestHandlerContext, filterId: string) {
const mgr = new FilterManager(context.ml!.mlClient);
function deleteFilter(legacyClient: ILegacyScopedClusterClient, filterId: string) {
const mgr = new FilterManager(legacyClient);
return mgr.deleteFilter(filterId);
}
@ -61,9 +65,9 @@ export function filtersRoutes({ router, mlLicense }: RouteInitialization) {
tags: ['access:ml:canGetFilters'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, response }) => {
try {
const resp = await getAllFilters(context);
const resp = await getAllFilters(legacyClient);
return response.ok({
body: resp,
@ -96,9 +100,9 @@ export function filtersRoutes({ router, mlLicense }: RouteInitialization) {
tags: ['access:ml:canGetFilters'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const resp = await getFilter(context, request.params.filterId);
const resp = await getFilter(legacyClient, request.params.filterId);
return response.ok({
body: resp,
});
@ -130,10 +134,10 @@ export function filtersRoutes({ router, mlLicense }: RouteInitialization) {
tags: ['access:ml:canCreateFilter'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const body = request.body;
const resp = await newFilter(context, body);
const resp = await newFilter(legacyClient, body);
return response.ok({
body: resp,
@ -168,11 +172,11 @@ export function filtersRoutes({ router, mlLicense }: RouteInitialization) {
tags: ['access:ml:canCreateFilter'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const { filterId } = request.params;
const body = request.body;
const resp = await updateFilter(context, filterId, body);
const resp = await updateFilter(legacyClient, filterId, body);
return response.ok({
body: resp,
@ -202,10 +206,10 @@ export function filtersRoutes({ router, mlLicense }: RouteInitialization) {
tags: ['access:ml:canDeleteFilter'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const { filterId } = request.params;
const resp = await deleteFilter(context, filterId);
const resp = await deleteFilter(legacyClient, filterId);
return response.ok({
body: resp,
@ -235,9 +239,9 @@ export function filtersRoutes({ router, mlLicense }: RouteInitialization) {
tags: ['access:ml:canGetFilters'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, response }) => {
try {
const resp = await getAllFilterStats(context);
const resp = await getAllFilterStats(legacyClient);
return response.ok({
body: resp,

View file

@ -31,7 +31,7 @@ export function indicesRoutes({ router, mlLicense }: RouteInitialization) {
tags: ['access:ml:canAccessML'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const {
body: { index, fields: requestFields },
@ -40,7 +40,7 @@ export function indicesRoutes({ router, mlLicense }: RouteInitialization) {
requestFields !== undefined && Array.isArray(requestFields)
? requestFields.join(',')
: '*';
const result = await context.ml!.mlClient.callAsCurrentUser('fieldCaps', { index, fields });
const result = await legacyClient.callAsCurrentUser('fieldCaps', { index, fields });
return response.ok({ body: result });
} catch (e) {
return response.customError(wrapError(e));

View file

@ -37,9 +37,9 @@ export function jobAuditMessagesRoutes({ router, mlLicense }: RouteInitializatio
tags: ['access:ml:canGetJobs'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const { getJobAuditMessages } = jobAuditMessagesProvider(context.ml!.mlClient);
const { getJobAuditMessages } = jobAuditMessagesProvider(legacyClient);
const { jobId } = request.params;
const { from } = request.query;
const resp = await getJobAuditMessages(jobId, from);
@ -72,9 +72,9 @@ export function jobAuditMessagesRoutes({ router, mlLicense }: RouteInitializatio
tags: ['access:ml:canGetJobs'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const { getJobAuditMessages } = jobAuditMessagesProvider(context.ml!.mlClient);
const { getJobAuditMessages } = jobAuditMessagesProvider(legacyClient);
const { from } = request.query;
const resp = await getJobAuditMessages(undefined, from);

View file

@ -48,9 +48,9 @@ export function jobServiceRoutes({ router, mlLicense }: RouteInitialization) {
tags: ['access:ml:canStartStopDatafeed'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const { forceStartDatafeeds } = jobServiceProvider(context.ml!.mlClient);
const { forceStartDatafeeds } = jobServiceProvider(legacyClient);
const { datafeedIds, start, end } = request.body;
const resp = await forceStartDatafeeds(datafeedIds, start, end);
@ -82,9 +82,9 @@ export function jobServiceRoutes({ router, mlLicense }: RouteInitialization) {
tags: ['access:ml:canStartStopDatafeed'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const { stopDatafeeds } = jobServiceProvider(context.ml!.mlClient);
const { stopDatafeeds } = jobServiceProvider(legacyClient);
const { datafeedIds } = request.body;
const resp = await stopDatafeeds(datafeedIds);
@ -116,9 +116,9 @@ export function jobServiceRoutes({ router, mlLicense }: RouteInitialization) {
tags: ['access:ml:canDeleteJob'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const { deleteJobs } = jobServiceProvider(context.ml!.mlClient);
const { deleteJobs } = jobServiceProvider(legacyClient);
const { jobIds } = request.body;
const resp = await deleteJobs(jobIds);
@ -150,9 +150,9 @@ export function jobServiceRoutes({ router, mlLicense }: RouteInitialization) {
tags: ['access:ml:canCloseJob'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const { closeJobs } = jobServiceProvider(context.ml!.mlClient);
const { closeJobs } = jobServiceProvider(legacyClient);
const { jobIds } = request.body;
const resp = await closeJobs(jobIds);
@ -184,9 +184,9 @@ export function jobServiceRoutes({ router, mlLicense }: RouteInitialization) {
tags: ['access:ml:canCloseJob', 'access:ml:canStartStopDatafeed'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const { forceStopAndCloseJob } = jobServiceProvider(context.ml!.mlClient);
const { forceStopAndCloseJob } = jobServiceProvider(legacyClient);
const { jobId } = request.body;
const resp = await forceStopAndCloseJob(jobId);
@ -223,9 +223,9 @@ export function jobServiceRoutes({ router, mlLicense }: RouteInitialization) {
tags: ['access:ml:canGetJobs'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const { jobsSummary } = jobServiceProvider(context.ml!.mlClient);
const { jobsSummary } = jobServiceProvider(legacyClient);
const { jobIds } = request.body;
const resp = await jobsSummary(jobIds);
@ -257,9 +257,9 @@ export function jobServiceRoutes({ router, mlLicense }: RouteInitialization) {
tags: ['access:ml:canGetJobs'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, response }) => {
try {
const { jobsWithTimerange } = jobServiceProvider(context.ml!.mlClient);
const { jobsWithTimerange } = jobServiceProvider(legacyClient);
const resp = await jobsWithTimerange();
return response.ok({
@ -290,9 +290,9 @@ export function jobServiceRoutes({ router, mlLicense }: RouteInitialization) {
tags: ['access:ml:canGetJobs'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const { createFullJobsList } = jobServiceProvider(context.ml!.mlClient);
const { createFullJobsList } = jobServiceProvider(legacyClient);
const { jobIds } = request.body;
const resp = await createFullJobsList(jobIds);
@ -320,9 +320,9 @@ export function jobServiceRoutes({ router, mlLicense }: RouteInitialization) {
tags: ['access:ml:canGetJobs'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, response }) => {
try {
const { getAllGroups } = jobServiceProvider(context.ml!.mlClient);
const { getAllGroups } = jobServiceProvider(legacyClient);
const resp = await getAllGroups();
return response.ok({
@ -353,9 +353,9 @@ export function jobServiceRoutes({ router, mlLicense }: RouteInitialization) {
tags: ['access:ml:canUpdateJob'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const { updateGroups } = jobServiceProvider(context.ml!.mlClient);
const { updateGroups } = jobServiceProvider(legacyClient);
const { jobs } = request.body;
const resp = await updateGroups(jobs);
@ -383,9 +383,9 @@ export function jobServiceRoutes({ router, mlLicense }: RouteInitialization) {
tags: ['access:ml:canGetJobs'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, response }) => {
try {
const { deletingJobTasks } = jobServiceProvider(context.ml!.mlClient);
const { deletingJobTasks } = jobServiceProvider(legacyClient);
const resp = await deletingJobTasks();
return response.ok({
@ -416,9 +416,9 @@ export function jobServiceRoutes({ router, mlLicense }: RouteInitialization) {
tags: ['access:ml:canGetJobs'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const { jobsExist } = jobServiceProvider(context.ml!.mlClient);
const { jobsExist } = jobServiceProvider(legacyClient);
const { jobIds } = request.body;
const resp = await jobsExist(jobIds);
@ -449,12 +449,12 @@ export function jobServiceRoutes({ router, mlLicense }: RouteInitialization) {
tags: ['access:ml:canGetJobs'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response, context }) => {
try {
const { indexPattern } = request.params;
const isRollup = request.query.rollup === 'true';
const savedObjectsClient = context.core.savedObjects.client;
const { newJobCaps } = jobServiceProvider(context.ml!.mlClient);
const { newJobCaps } = jobServiceProvider(legacyClient);
const resp = await newJobCaps(indexPattern, isRollup, savedObjectsClient);
return response.ok({
@ -485,7 +485,7 @@ export function jobServiceRoutes({ router, mlLicense }: RouteInitialization) {
tags: ['access:ml:canGetJobs'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const {
indexPatternTitle,
@ -499,7 +499,7 @@ export function jobServiceRoutes({ router, mlLicense }: RouteInitialization) {
splitFieldValue,
} = request.body;
const { newJobLineChart } = jobServiceProvider(context.ml!.mlClient);
const { newJobLineChart } = jobServiceProvider(legacyClient);
const resp = await newJobLineChart(
indexPatternTitle,
timeField,
@ -540,7 +540,7 @@ export function jobServiceRoutes({ router, mlLicense }: RouteInitialization) {
tags: ['access:ml:canGetJobs'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const {
indexPatternTitle,
@ -553,7 +553,7 @@ export function jobServiceRoutes({ router, mlLicense }: RouteInitialization) {
splitFieldName,
} = request.body;
const { newJobPopulationChart } = jobServiceProvider(context.ml!.mlClient);
const { newJobPopulationChart } = jobServiceProvider(legacyClient);
const resp = await newJobPopulationChart(
indexPatternTitle,
timeField,
@ -589,9 +589,9 @@ export function jobServiceRoutes({ router, mlLicense }: RouteInitialization) {
tags: ['access:ml:canGetJobs'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, response }) => {
try {
const { getAllJobAndGroupIds } = jobServiceProvider(context.ml!.mlClient);
const { getAllJobAndGroupIds } = jobServiceProvider(legacyClient);
const resp = await getAllJobAndGroupIds();
return response.ok({
@ -622,9 +622,9 @@ export function jobServiceRoutes({ router, mlLicense }: RouteInitialization) {
tags: ['access:ml:canCreateJob'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const { getLookBackProgress } = jobServiceProvider(context.ml!.mlClient);
const { getLookBackProgress } = jobServiceProvider(legacyClient);
const { jobId, start, end } = request.body;
const resp = await getLookBackProgress(jobId, start, end);
@ -656,9 +656,9 @@ export function jobServiceRoutes({ router, mlLicense }: RouteInitialization) {
tags: ['access:ml:canCreateJob'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const { validateCategoryExamples } = categorizationExamplesProvider(context.ml!.mlClient);
const { validateCategoryExamples } = categorizationExamplesProvider(legacyClient);
const {
indexPatternTitle,
timeField,
@ -709,9 +709,9 @@ export function jobServiceRoutes({ router, mlLicense }: RouteInitialization) {
tags: ['access:ml:canGetJobs'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const { topCategories } = jobServiceProvider(context.ml!.mlClient);
const { topCategories } = jobServiceProvider(legacyClient);
const { jobId, count } = request.body;
const resp = await topCategories(jobId, count);
@ -743,9 +743,9 @@ export function jobServiceRoutes({ router, mlLicense }: RouteInitialization) {
tags: ['access:ml:canCreateJob', 'access:ml:canStartStopDatafeed'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const { revertModelSnapshot } = jobServiceProvider(context.ml!.mlClient);
const { revertModelSnapshot } = jobServiceProvider(legacyClient);
const {
jobId,
snapshotId,

View file

@ -5,7 +5,7 @@
*/
import Boom from 'boom';
import { RequestHandlerContext } from 'kibana/server';
import { ILegacyScopedClusterClient } from 'kibana/server';
import { TypeOf } from '@kbn/config-schema';
import { AnalysisConfig } from '../../common/types/anomaly_detection_jobs';
import { wrapError } from '../client/error_wrapper';
@ -27,12 +27,12 @@ type CalculateModelMemoryLimitPayload = TypeOf<typeof modelMemoryLimitSchema>;
*/
export function jobValidationRoutes({ router, mlLicense }: RouteInitialization, version: string) {
function calculateModelMemoryLimit(
context: RequestHandlerContext,
legacyClient: ILegacyScopedClusterClient,
payload: CalculateModelMemoryLimitPayload
) {
const { analysisConfig, indexPattern, query, timeFieldName, earliestMs, latestMs } = payload;
return calculateModelMemoryLimitProvider(context.ml!.mlClient)(
return calculateModelMemoryLimitProvider(legacyClient)(
analysisConfig as AnalysisConfig,
indexPattern,
query,
@ -61,10 +61,10 @@ export function jobValidationRoutes({ router, mlLicense }: RouteInitialization,
tags: ['access:ml:canCreateJob'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
let errorResp;
const resp = await estimateBucketSpanFactory(context.ml!.mlClient)(request.body)
const resp = await estimateBucketSpanFactory(legacyClient)(request.body)
// this catch gets triggered when the estimation code runs without error
// but isn't able to come up with a bucket span estimation.
// this doesn't return a HTTP error but an object with an error message
@ -109,9 +109,9 @@ export function jobValidationRoutes({ router, mlLicense }: RouteInitialization,
tags: ['access:ml:canCreateJob'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const resp = await calculateModelMemoryLimit(context, request.body);
const resp = await calculateModelMemoryLimit(legacyClient, request.body);
return response.ok({
body: resp,
@ -141,9 +141,9 @@ export function jobValidationRoutes({ router, mlLicense }: RouteInitialization,
tags: ['access:ml:canCreateJob'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const resp = await validateCardinality(context.ml!.mlClient, request.body);
const resp = await validateCardinality(legacyClient, request.body);
return response.ok({
body: resp,
@ -173,11 +173,11 @@ export function jobValidationRoutes({ router, mlLicense }: RouteInitialization,
tags: ['access:ml:canCreateJob'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
// version corresponds to the version used in documentation links.
const resp = await validateJob(
context.ml!.mlClient,
legacyClient,
request.body,
version,
mlLicense.isSecurityEnabled() === false

View file

@ -6,7 +6,11 @@
import { TypeOf } from '@kbn/config-schema';
import { RequestHandlerContext, KibanaRequest } from 'kibana/server';
import {
ILegacyScopedClusterClient,
KibanaRequest,
SavedObjectsClientContract,
} from 'kibana/server';
import { DatafeedOverride, JobOverride } from '../../common/types/modules';
import { wrapError } from '../client/error_wrapper';
import { DataRecognizer } from '../models/data_recognizer';
@ -19,16 +23,22 @@ import {
import { RouteInitialization } from '../types';
function recognize(
context: RequestHandlerContext,
legacyClient: ILegacyScopedClusterClient,
savedObjectsClient: SavedObjectsClientContract,
request: KibanaRequest,
indexPatternTitle: string
) {
const dr = new DataRecognizer(context.ml!.mlClient, context.core.savedObjects.client, request);
const dr = new DataRecognizer(legacyClient, savedObjectsClient, request);
return dr.findMatches(indexPatternTitle);
}
function getModule(context: RequestHandlerContext, request: KibanaRequest, moduleId: string) {
const dr = new DataRecognizer(context.ml!.mlClient, context.core.savedObjects.client, request);
function getModule(
legacyClient: ILegacyScopedClusterClient,
savedObjectsClient: SavedObjectsClientContract,
request: KibanaRequest,
moduleId: string
) {
const dr = new DataRecognizer(legacyClient, savedObjectsClient, request);
if (moduleId === undefined) {
return dr.listModules();
} else {
@ -37,7 +47,8 @@ function getModule(context: RequestHandlerContext, request: KibanaRequest, modul
}
function setup(
context: RequestHandlerContext,
legacyClient: ILegacyScopedClusterClient,
savedObjectsClient: SavedObjectsClientContract,
request: KibanaRequest,
moduleId: string,
prefix?: string,
@ -52,7 +63,7 @@ function setup(
datafeedOverrides?: DatafeedOverride | DatafeedOverride[],
estimateModelMemory?: boolean
) {
const dr = new DataRecognizer(context.ml!.mlClient, context.core.savedObjects.client, request);
const dr = new DataRecognizer(legacyClient, savedObjectsClient, request);
return dr.setup(
moduleId,
prefix,
@ -70,11 +81,12 @@ function setup(
}
function dataRecognizerJobsExist(
context: RequestHandlerContext,
legacyClient: ILegacyScopedClusterClient,
savedObjectsClient: SavedObjectsClientContract,
request: KibanaRequest,
moduleId: string
) {
const dr = new DataRecognizer(context.ml!.mlClient, context.core.savedObjects.client, request);
const dr = new DataRecognizer(legacyClient, savedObjectsClient, request);
return dr.dataRecognizerJobsExist(moduleId);
}
@ -119,10 +131,15 @@ export function dataRecognizer({ router, mlLicense }: RouteInitialization) {
tags: ['access:ml:canCreateJob'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response, context }) => {
try {
const { indexPatternTitle } = request.params;
const results = await recognize(context, request, indexPatternTitle);
const results = await recognize(
legacyClient,
context.core.savedObjects.client,
request,
indexPatternTitle
);
return response.ok({ body: results });
} catch (e) {
@ -249,7 +266,7 @@ export function dataRecognizer({ router, mlLicense }: RouteInitialization) {
tags: ['access:ml:canGetJobs'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response, context }) => {
try {
let { moduleId } = request.params;
if (moduleId === '') {
@ -257,7 +274,12 @@ export function dataRecognizer({ router, mlLicense }: RouteInitialization) {
// the moduleId will be an empty string.
moduleId = undefined;
}
const results = await getModule(context, request, moduleId);
const results = await getModule(
legacyClient,
context.core.savedObjects.client,
request,
moduleId
);
return response.ok({ body: results });
} catch (e) {
@ -417,7 +439,7 @@ export function dataRecognizer({ router, mlLicense }: RouteInitialization) {
tags: ['access:ml:canCreateJob'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response, context }) => {
try {
const { moduleId } = request.params;
@ -436,7 +458,8 @@ export function dataRecognizer({ router, mlLicense }: RouteInitialization) {
} = request.body as TypeOf<typeof setupModuleBodySchema>;
const result = await setup(
context,
legacyClient,
context.core.savedObjects.client,
request,
moduleId,
prefix,
@ -521,10 +544,15 @@ export function dataRecognizer({ router, mlLicense }: RouteInitialization) {
tags: ['access:ml:canGetJobs'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response, context }) => {
try {
const { moduleId } = request.params;
const result = await dataRecognizerJobsExist(context, request, moduleId);
const result = await dataRecognizerJobsExist(
legacyClient,
context.core.savedObjects.client,
request,
moduleId
);
return response.ok({ body: result });
} catch (e) {

View file

@ -26,13 +26,13 @@ export function notificationRoutes({ router, mlLicense }: RouteInitialization) {
tags: ['access:ml:canAccessML'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, response }) => {
try {
const params = {
includeDefaults: true,
filterPath: '**.xpack.notification',
};
const resp = await context.ml!.mlClient.callAsCurrentUser('cluster.getSettings', params);
const resp = await legacyClient.callAsCurrentUser('cluster.getSettings', params);
return response.ok({
body: resp,

View file

@ -4,7 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/
import { RequestHandlerContext } from 'kibana/server';
import { ILegacyScopedClusterClient } from 'kibana/server';
import { schema } from '@kbn/config-schema';
import { wrapError } from '../client/error_wrapper';
import { RouteInitialization } from '../types';
@ -18,8 +18,8 @@ import {
import { resultsServiceProvider } from '../models/results_service';
import { ML_RESULTS_INDEX_PATTERN } from '../../common/constants/index_patterns';
function getAnomaliesTableData(context: RequestHandlerContext, payload: any) {
const rs = resultsServiceProvider(context.ml!.mlClient);
function getAnomaliesTableData(legacyClient: ILegacyScopedClusterClient, payload: any) {
const rs = resultsServiceProvider(legacyClient);
const {
jobIds,
criteriaFields,
@ -48,25 +48,25 @@ function getAnomaliesTableData(context: RequestHandlerContext, payload: any) {
);
}
function getCategoryDefinition(context: RequestHandlerContext, payload: any) {
const rs = resultsServiceProvider(context.ml!.mlClient);
function getCategoryDefinition(legacyClient: ILegacyScopedClusterClient, payload: any) {
const rs = resultsServiceProvider(legacyClient);
return rs.getCategoryDefinition(payload.jobId, payload.categoryId);
}
function getCategoryExamples(context: RequestHandlerContext, payload: any) {
const rs = resultsServiceProvider(context.ml!.mlClient);
function getCategoryExamples(legacyClient: ILegacyScopedClusterClient, payload: any) {
const rs = resultsServiceProvider(legacyClient);
const { jobId, categoryIds, maxExamples } = payload;
return rs.getCategoryExamples(jobId, categoryIds, maxExamples);
}
function getMaxAnomalyScore(context: RequestHandlerContext, payload: any) {
const rs = resultsServiceProvider(context.ml!.mlClient);
function getMaxAnomalyScore(legacyClient: ILegacyScopedClusterClient, payload: any) {
const rs = resultsServiceProvider(legacyClient);
const { jobIds, earliestMs, latestMs } = payload;
return rs.getMaxAnomalyScore(jobIds, earliestMs, latestMs);
}
function getPartitionFieldsValues(context: RequestHandlerContext, payload: any) {
const rs = resultsServiceProvider(context.ml!.mlClient);
function getPartitionFieldsValues(legacyClient: ILegacyScopedClusterClient, payload: any) {
const rs = resultsServiceProvider(legacyClient);
const { jobId, searchTerm, criteriaFields, earliestMs, latestMs } = payload;
return rs.getPartitionFieldsValues(jobId, searchTerm, criteriaFields, earliestMs, latestMs);
}
@ -94,9 +94,9 @@ export function resultsServiceRoutes({ router, mlLicense }: RouteInitialization)
tags: ['access:ml:canGetJobs'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const resp = await getAnomaliesTableData(context, request.body);
const resp = await getAnomaliesTableData(legacyClient, request.body);
return response.ok({
body: resp,
@ -126,9 +126,9 @@ export function resultsServiceRoutes({ router, mlLicense }: RouteInitialization)
tags: ['access:ml:canGetJobs'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const resp = await getCategoryDefinition(context, request.body);
const resp = await getCategoryDefinition(legacyClient, request.body);
return response.ok({
body: resp,
@ -158,9 +158,9 @@ export function resultsServiceRoutes({ router, mlLicense }: RouteInitialization)
tags: ['access:ml:canGetJobs'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const resp = await getMaxAnomalyScore(context, request.body);
const resp = await getMaxAnomalyScore(legacyClient, request.body);
return response.ok({
body: resp,
@ -190,9 +190,9 @@ export function resultsServiceRoutes({ router, mlLicense }: RouteInitialization)
tags: ['access:ml:canGetJobs'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const resp = await getCategoryExamples(context, request.body);
const resp = await getCategoryExamples(legacyClient, request.body);
return response.ok({
body: resp,
@ -222,9 +222,9 @@ export function resultsServiceRoutes({ router, mlLicense }: RouteInitialization)
tags: ['access:ml:canGetJobs'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const resp = await getPartitionFieldsValues(context, request.body);
const resp = await getPartitionFieldsValues(legacyClient, request.body);
return response.ok({
body: resp,
@ -251,14 +251,14 @@ export function resultsServiceRoutes({ router, mlLicense }: RouteInitialization)
tags: ['access:ml:canGetJobs'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
const body = {
...request.body,
index: ML_RESULTS_INDEX_PATTERN,
};
try {
return response.ok({
body: await context.ml!.mlClient.callAsInternalUser('search', body),
body: await legacyClient.callAsInternalUser('search', body),
});
} catch (error) {
return response.customError(wrapError(error));

View file

@ -7,7 +7,7 @@
import { schema } from '@kbn/config-schema';
import { Request } from 'hapi';
import { RequestHandlerContext } from 'kibana/server';
import { ILegacyScopedClusterClient } from 'kibana/server';
import { wrapError } from '../client/error_wrapper';
import { mlLog } from '../client/log';
import { capabilitiesProvider } from '../lib/capabilities';
@ -21,9 +21,9 @@ export function systemRoutes(
{ router, mlLicense }: RouteInitialization,
{ spaces, cloud, resolveMlCapabilities }: SystemRouteDeps
) {
async function getNodeCount(context: RequestHandlerContext) {
async function getNodeCount(legacyClient: ILegacyScopedClusterClient) {
const filterPath = 'nodes.*.attributes';
const resp = await context.ml!.mlClient.callAsInternalUser('nodes.info', {
const resp = await legacyClient.callAsInternalUser('nodes.info', {
filterPath,
});
@ -58,9 +58,9 @@ export function systemRoutes(
tags: ['access:ml:canAccessML'],
},
},
mlLicense.basicLicenseAPIGuard(async (context, request, response) => {
mlLicense.basicLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const { callAsCurrentUser, callAsInternalUser } = context.ml!.mlClient;
const { callAsCurrentUser, callAsInternalUser } = legacyClient;
let upgradeInProgress = false;
try {
const info = await callAsInternalUser('ml.info');
@ -115,7 +115,7 @@ export function systemRoutes(
path: '/api/ml/ml_capabilities',
validate: false,
},
mlLicense.basicLicenseAPIGuard(async (context, request, response) => {
mlLicense.basicLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
// if spaces is disabled force isMlEnabledInSpace to be true
const { isMlEnabledInSpace } =
@ -129,7 +129,7 @@ export function systemRoutes(
}
const { getCapabilities } = capabilitiesProvider(
context.ml!.mlClient,
legacyClient,
mlCapabilities,
mlLicense,
isMlEnabledInSpace
@ -159,10 +159,10 @@ export function systemRoutes(
},
},
mlLicense.basicLicenseAPIGuard(async (context, request, response) => {
mlLicense.basicLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
return response.ok({
body: await getNodeCount(context),
body: await getNodeCount(legacyClient),
});
} catch (e) {
return response.customError(wrapError(e));
@ -185,9 +185,9 @@ export function systemRoutes(
tags: ['access:ml:canAccessML'],
},
},
mlLicense.basicLicenseAPIGuard(async (context, request, response) => {
mlLicense.basicLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const info = await context.ml!.mlClient.callAsInternalUser('ml.info');
const info = await legacyClient.callAsInternalUser('ml.info');
const cloudId = cloud && cloud.cloudId;
return response.ok({
body: { ...info, cloudId },
@ -216,10 +216,10 @@ export function systemRoutes(
tags: ['access:ml:canGetJobs'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
return response.ok({
body: await context.ml!.mlClient.callAsCurrentUser('search', request.body),
body: await legacyClient.callAsCurrentUser('search', request.body),
});
} catch (error) {
return response.customError(wrapError(error));
@ -243,7 +243,7 @@ export function systemRoutes(
tags: ['access:ml:canGetJobs'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const { index } = request.body;
@ -255,7 +255,7 @@ export function systemRoutes(
ignore: 404,
};
const fieldsResult = await context.ml!.mlClient.callAsCurrentUser('fieldCaps', options);
const fieldsResult = await legacyClient.callAsCurrentUser('fieldCaps', options);
const result = { exists: false };
if (Array.isArray(fieldsResult.indices) && fieldsResult.indices.length !== 0) {