Skip to content

Commit

Permalink
removing placeholder error
Browse files Browse the repository at this point in the history
  • Loading branch information
jgowdyelastic committed Aug 27, 2020
1 parent 8eabebd commit 43ba3e8
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
9 changes: 1 addition & 8 deletions x-pack/plugins/ml/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,6 @@ export class MlServerPlugin implements Plugin<MlPluginSetup, MlPluginStart, Plug
private capabilities: CapabilitiesStart | null = null;
private clusterClient: IClusterClient | null = null;

getClusterClient(): IClusterClient {
if (this.clusterClient === null) {
throw Error('????');
}
return this.clusterClient;
}

constructor(ctx: PluginInitializerContext) {
this.log = ctx.logger.get();
this.version = ctx.env.packageInfo.branch;
Expand Down Expand Up @@ -166,7 +159,7 @@ export class MlServerPlugin implements Plugin<MlPluginSetup, MlPluginStart, Plug
plugins.spaces,
plugins.cloud,
resolveMlCapabilities,
() => this.getClusterClient()
() => this.clusterClient
),
};
}
Expand Down
12 changes: 12 additions & 0 deletions x-pack/plugins/ml/server/shared_services/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

export class MLClusterClientUninitialized extends Error {
constructor(message?: string) {
super(message);
Object.setPrototypeOf(this, new.target.prototype);
}
}
15 changes: 11 additions & 4 deletions x-pack/plugins/ml/server/shared_services/shared_services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
} from './providers/anomaly_detectors';
import { ResolveMlCapabilities, MlCapabilitiesKey } from '../../common/types/capabilities';
import { hasMlCapabilitiesProvider, HasMlCapabilities } from '../lib/capabilities';
import { MLClusterClientUninitialized } from './errors';

export type SharedServices = JobServiceProvider &
AnomalyDetectorsProvider &
Expand Down Expand Up @@ -55,7 +56,7 @@ export function createSharedServices(
spaces: SpacesPluginSetup | undefined,
cloud: CloudSetup,
resolveMlCapabilities: ResolveMlCapabilities,
getClusterClient: () => IClusterClient
getClusterClient: () => IClusterClient | null
): SharedServices {
const getRequestItems = getRequestItemsProvider(resolveMlCapabilities, getClusterClient);
const { isFullLicense, isMinimumLicense } = licenseChecks(mlLicense);
Expand Down Expand Up @@ -96,7 +97,7 @@ export function createSharedServices(

function getRequestItemsProvider(
resolveMlCapabilities: ResolveMlCapabilities,
getClusterClient: () => IClusterClient
getClusterClient: () => IClusterClient | null
) {
return (request: KibanaRequest) => {
const getHasMlCapabilities = hasMlCapabilitiesProvider(resolveMlCapabilities);
Expand All @@ -105,12 +106,18 @@ function getRequestItemsProvider(
// While https://github.com/elastic/kibana/issues/64588 exists we
// will not receive a real request object when being called from an alert.
// instead a dummy request object will be supplied
const clusterClient = getClusterClient();

if (clusterClient === null) {
throw new MLClusterClientUninitialized(`ML's cluster client has not been initialized`);
}

if (request instanceof KibanaRequest) {
hasMlCapabilities = getHasMlCapabilities(request);
scopedClient = getClusterClient().asScoped(request);
scopedClient = clusterClient.asScoped(request);
} else {
hasMlCapabilities = () => Promise.resolve();
const { asInternalUser } = getClusterClient();
const { asInternalUser } = clusterClient;
scopedClient = {
asInternalUser,
asCurrentUser: asInternalUser,
Expand Down

0 comments on commit 43ba3e8

Please sign in to comment.