Skip to content

Commit 01d8f00

Browse files
[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 <[email protected]>
1 parent 6bea373 commit 01d8f00

18 files changed

+369
-339
lines changed

x-pack/plugins/ml/server/lib/license/ml_server_license.ts

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,47 @@
66
import {
77
KibanaRequest,
88
KibanaResponseFactory,
9-
RequestHandler,
109
RequestHandlerContext,
10+
ILegacyScopedClusterClient,
11+
IScopedClusterClient,
12+
RequestHandler,
1113
} from 'kibana/server';
1214

1315
import { MlLicense } from '../../../common/license';
1416

17+
type Handler = (handlerParams: {
18+
legacyClient: ILegacyScopedClusterClient;
19+
client: IScopedClusterClient;
20+
request: KibanaRequest<any, any, any, any>;
21+
response: KibanaResponseFactory;
22+
context: RequestHandlerContext;
23+
}) => ReturnType<RequestHandler>;
24+
1525
export class MlServerLicense extends MlLicense {
16-
public fullLicenseAPIGuard(handler: RequestHandler<any, any, any>) {
26+
public fullLicenseAPIGuard(handler: Handler) {
1727
return guard(() => this.isFullLicense(), handler);
1828
}
19-
public basicLicenseAPIGuard(handler: RequestHandler<any, any, any>) {
29+
public basicLicenseAPIGuard(handler: Handler) {
2030
return guard(() => this.isMinimumLicense(), handler);
2131
}
2232
}
2333

24-
function guard(check: () => boolean, handler: RequestHandler<any, any, any>) {
34+
function guard(check: () => boolean, handler: Handler) {
2535
return (
2636
context: RequestHandlerContext,
27-
request: KibanaRequest,
37+
request: KibanaRequest<any, any, any, any>,
2838
response: KibanaResponseFactory
2939
) => {
3040
if (check() === false) {
3141
return response.forbidden();
3242
}
33-
return handler(context, request, response);
43+
44+
return handler({
45+
legacyClient: context.ml!.mlClient,
46+
client: context.core.elasticsearch.client,
47+
request,
48+
response,
49+
context,
50+
});
3451
};
3552
}

x-pack/plugins/ml/server/routes/annotations.ts

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ export function annotationRoutes(
5858
tags: ['access:ml:canGetAnnotations'],
5959
},
6060
},
61-
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
61+
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
6262
try {
63-
const { getAnnotations } = annotationServiceProvider(context.ml!.mlClient);
63+
const { getAnnotations } = annotationServiceProvider(legacyClient);
6464
const resp = await getAnnotations(request.body);
6565

6666
return response.ok({
@@ -91,16 +91,14 @@ export function annotationRoutes(
9191
tags: ['access:ml:canCreateAnnotation'],
9292
},
9393
},
94-
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
94+
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
9595
try {
96-
const annotationsFeatureAvailable = await isAnnotationsFeatureAvailable(
97-
context.ml!.mlClient
98-
);
96+
const annotationsFeatureAvailable = await isAnnotationsFeatureAvailable(legacyClient);
9997
if (annotationsFeatureAvailable === false) {
10098
throw getAnnotationsFeatureUnavailableErrorMessage();
10199
}
102100

103-
const { indexAnnotation } = annotationServiceProvider(context.ml!.mlClient);
101+
const { indexAnnotation } = annotationServiceProvider(legacyClient);
104102

105103
const currentUser =
106104
securityPlugin !== undefined ? securityPlugin.authc.getCurrentUser(request) : {};
@@ -136,17 +134,15 @@ export function annotationRoutes(
136134
tags: ['access:ml:canDeleteAnnotation'],
137135
},
138136
},
139-
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
137+
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
140138
try {
141-
const annotationsFeatureAvailable = await isAnnotationsFeatureAvailable(
142-
context.ml!.mlClient
143-
);
139+
const annotationsFeatureAvailable = await isAnnotationsFeatureAvailable(legacyClient);
144140
if (annotationsFeatureAvailable === false) {
145141
throw getAnnotationsFeatureUnavailableErrorMessage();
146142
}
147143

148144
const annotationId = request.params.annotationId;
149-
const { deleteAnnotation } = annotationServiceProvider(context.ml!.mlClient);
145+
const { deleteAnnotation } = annotationServiceProvider(legacyClient);
150146
const resp = await deleteAnnotation(annotationId);
151147

152148
return response.ok({

x-pack/plugins/ml/server/routes/anomaly_detectors.ts

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ export function jobRoutes({ router, mlLicense }: RouteInitialization) {
4343
tags: ['access:ml:canGetJobs'],
4444
},
4545
},
46-
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
46+
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, response }) => {
4747
try {
48-
const results = await context.ml!.mlClient.callAsInternalUser('ml.jobs');
48+
const results = await legacyClient.callAsInternalUser('ml.jobs');
4949
return response.ok({
5050
body: results,
5151
});
@@ -74,10 +74,10 @@ export function jobRoutes({ router, mlLicense }: RouteInitialization) {
7474
tags: ['access:ml:canGetJobs'],
7575
},
7676
},
77-
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
77+
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
7878
try {
7979
const { jobId } = request.params;
80-
const results = await context.ml!.mlClient.callAsInternalUser('ml.jobs', { jobId });
80+
const results = await legacyClient.callAsInternalUser('ml.jobs', { jobId });
8181
return response.ok({
8282
body: results,
8383
});
@@ -105,9 +105,9 @@ export function jobRoutes({ router, mlLicense }: RouteInitialization) {
105105
tags: ['access:ml:canGetJobs'],
106106
},
107107
},
108-
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
108+
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, response }) => {
109109
try {
110-
const results = await context.ml!.mlClient.callAsInternalUser('ml.jobStats');
110+
const results = await legacyClient.callAsInternalUser('ml.jobStats');
111111
return response.ok({
112112
body: results,
113113
});
@@ -136,10 +136,10 @@ export function jobRoutes({ router, mlLicense }: RouteInitialization) {
136136
tags: ['access:ml:canGetJobs'],
137137
},
138138
},
139-
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
139+
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
140140
try {
141141
const { jobId } = request.params;
142-
const results = await context.ml!.mlClient.callAsInternalUser('ml.jobStats', { jobId });
142+
const results = await legacyClient.callAsInternalUser('ml.jobStats', { jobId });
143143
return response.ok({
144144
body: results,
145145
});
@@ -172,10 +172,10 @@ export function jobRoutes({ router, mlLicense }: RouteInitialization) {
172172
tags: ['access:ml:canCreateJob'],
173173
},
174174
},
175-
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
175+
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
176176
try {
177177
const { jobId } = request.params;
178-
const results = await context.ml!.mlClient.callAsInternalUser('ml.addJob', {
178+
const results = await legacyClient.callAsInternalUser('ml.addJob', {
179179
jobId,
180180
body: request.body,
181181
});
@@ -209,10 +209,10 @@ export function jobRoutes({ router, mlLicense }: RouteInitialization) {
209209
tags: ['access:ml:canUpdateJob'],
210210
},
211211
},
212-
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
212+
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
213213
try {
214214
const { jobId } = request.params;
215-
const results = await context.ml!.mlClient.callAsInternalUser('ml.updateJob', {
215+
const results = await legacyClient.callAsInternalUser('ml.updateJob', {
216216
jobId,
217217
body: request.body,
218218
});
@@ -244,10 +244,10 @@ export function jobRoutes({ router, mlLicense }: RouteInitialization) {
244244
tags: ['access:ml:canOpenJob'],
245245
},
246246
},
247-
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
247+
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
248248
try {
249249
const { jobId } = request.params;
250-
const results = await context.ml!.mlClient.callAsInternalUser('ml.openJob', {
250+
const results = await legacyClient.callAsInternalUser('ml.openJob', {
251251
jobId,
252252
});
253253
return response.ok({
@@ -278,7 +278,7 @@ export function jobRoutes({ router, mlLicense }: RouteInitialization) {
278278
tags: ['access:ml:canCloseJob'],
279279
},
280280
},
281-
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
281+
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
282282
try {
283283
const options: { jobId: string; force?: boolean } = {
284284
jobId: request.params.jobId,
@@ -287,7 +287,7 @@ export function jobRoutes({ router, mlLicense }: RouteInitialization) {
287287
if (force !== undefined) {
288288
options.force = force;
289289
}
290-
const results = await context.ml!.mlClient.callAsInternalUser('ml.closeJob', options);
290+
const results = await legacyClient.callAsInternalUser('ml.closeJob', options);
291291
return response.ok({
292292
body: results,
293293
});
@@ -316,7 +316,7 @@ export function jobRoutes({ router, mlLicense }: RouteInitialization) {
316316
tags: ['access:ml:canDeleteJob'],
317317
},
318318
},
319-
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
319+
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
320320
try {
321321
const options: { jobId: string; force?: boolean } = {
322322
jobId: request.params.jobId,
@@ -325,7 +325,7 @@ export function jobRoutes({ router, mlLicense }: RouteInitialization) {
325325
if (force !== undefined) {
326326
options.force = force;
327327
}
328-
const results = await context.ml!.mlClient.callAsInternalUser('ml.deleteJob', options);
328+
const results = await legacyClient.callAsInternalUser('ml.deleteJob', options);
329329
return response.ok({
330330
body: results,
331331
});
@@ -352,9 +352,9 @@ export function jobRoutes({ router, mlLicense }: RouteInitialization) {
352352
tags: ['access:ml:canCreateJob'],
353353
},
354354
},
355-
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
355+
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
356356
try {
357-
const results = await context.ml!.mlClient.callAsInternalUser('ml.validateDetector', {
357+
const results = await legacyClient.callAsInternalUser('ml.validateDetector', {
358358
body: request.body,
359359
});
360360
return response.ok({
@@ -387,11 +387,11 @@ export function jobRoutes({ router, mlLicense }: RouteInitialization) {
387387
tags: ['access:ml:canForecastJob'],
388388
},
389389
},
390-
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
390+
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
391391
try {
392392
const jobId = request.params.jobId;
393393
const duration = request.body.duration;
394-
const results = await context.ml!.mlClient.callAsInternalUser('ml.forecast', {
394+
const results = await legacyClient.callAsInternalUser('ml.forecast', {
395395
jobId,
396396
duration,
397397
});
@@ -428,9 +428,9 @@ export function jobRoutes({ router, mlLicense }: RouteInitialization) {
428428
tags: ['access:ml:canGetJobs'],
429429
},
430430
},
431-
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
431+
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
432432
try {
433-
const results = await context.ml!.mlClient.callAsInternalUser('ml.records', {
433+
const results = await legacyClient.callAsInternalUser('ml.records', {
434434
jobId: request.params.jobId,
435435
body: request.body,
436436
});
@@ -467,9 +467,9 @@ export function jobRoutes({ router, mlLicense }: RouteInitialization) {
467467
tags: ['access:ml:canGetJobs'],
468468
},
469469
},
470-
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
470+
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
471471
try {
472-
const results = await context.ml!.mlClient.callAsInternalUser('ml.buckets', {
472+
const results = await legacyClient.callAsInternalUser('ml.buckets', {
473473
jobId: request.params.jobId,
474474
timestamp: request.params.timestamp,
475475
body: request.body,
@@ -507,9 +507,9 @@ export function jobRoutes({ router, mlLicense }: RouteInitialization) {
507507
tags: ['access:ml:canGetJobs'],
508508
},
509509
},
510-
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
510+
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
511511
try {
512-
const results = await context.ml!.mlClient.callAsInternalUser('ml.overallBuckets', {
512+
const results = await legacyClient.callAsInternalUser('ml.overallBuckets', {
513513
jobId: request.params.jobId,
514514
top_n: request.body.topN,
515515
bucket_span: request.body.bucketSpan,
@@ -544,9 +544,9 @@ export function jobRoutes({ router, mlLicense }: RouteInitialization) {
544544
tags: ['access:ml:canGetJobs'],
545545
},
546546
},
547-
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
547+
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
548548
try {
549-
const results = await context.ml!.mlClient.callAsInternalUser('ml.categories', {
549+
const results = await legacyClient.callAsInternalUser('ml.categories', {
550550
jobId: request.params.jobId,
551551
categoryId: request.params.categoryId,
552552
});
@@ -578,9 +578,9 @@ export function jobRoutes({ router, mlLicense }: RouteInitialization) {
578578
tags: ['access:ml:canGetJobs'],
579579
},
580580
},
581-
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
581+
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
582582
try {
583-
const results = await context.ml!.mlClient.callAsInternalUser('ml.modelSnapshots', {
583+
const results = await legacyClient.callAsInternalUser('ml.modelSnapshots', {
584584
jobId: request.params.jobId,
585585
});
586586
return response.ok({
@@ -611,9 +611,9 @@ export function jobRoutes({ router, mlLicense }: RouteInitialization) {
611611
tags: ['access:ml:canGetJobs'],
612612
},
613613
},
614-
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
614+
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
615615
try {
616-
const results = await context.ml!.mlClient.callAsInternalUser('ml.modelSnapshots', {
616+
const results = await legacyClient.callAsInternalUser('ml.modelSnapshots', {
617617
jobId: request.params.jobId,
618618
snapshotId: request.params.snapshotId,
619619
});
@@ -647,9 +647,9 @@ export function jobRoutes({ router, mlLicense }: RouteInitialization) {
647647
tags: ['access:ml:canCreateJob'],
648648
},
649649
},
650-
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
650+
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
651651
try {
652-
const results = await context.ml!.mlClient.callAsInternalUser('ml.updateModelSnapshot', {
652+
const results = await legacyClient.callAsInternalUser('ml.updateModelSnapshot', {
653653
jobId: request.params.jobId,
654654
snapshotId: request.params.snapshotId,
655655
body: request.body,
@@ -682,9 +682,9 @@ export function jobRoutes({ router, mlLicense }: RouteInitialization) {
682682
tags: ['access:ml:canCreateJob'],
683683
},
684684
},
685-
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
685+
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
686686
try {
687-
const results = await context.ml!.mlClient.callAsInternalUser('ml.deleteModelSnapshot', {
687+
const results = await legacyClient.callAsInternalUser('ml.deleteModelSnapshot', {
688688
jobId: request.params.jobId,
689689
snapshotId: request.params.snapshotId,
690690
});

0 commit comments

Comments
 (0)