Skip to content

Commit

Permalink
[ML] New Platform Migration - server (#38360)
Browse files Browse the repository at this point in the history
* wip: pull out all server dependencies

* create new platform plugin

* shim newPlatform plugin into legacy init

* update server tests for migration refactor

* cleanup interfaces

* Only add ML links for sample data sets if full license from - 38120

* update test and fix typescript errors

* Remove unused types
  • Loading branch information
alvarezmelissa87 authored Jun 17, 2019
1 parent 4526e2a commit c47a0d5
Show file tree
Hide file tree
Showing 35 changed files with 640 additions and 478 deletions.
166 changes: 0 additions & 166 deletions x-pack/plugins/ml/index.js

This file was deleted.

94 changes: 94 additions & 0 deletions x-pack/plugins/ml/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* 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.
*/

import { resolve } from 'path';
import { i18n } from '@kbn/i18n';
import KbnServer, { Server } from 'src/legacy/server/kbn_server';
import { plugin } from './server/new_platform';
import {
MlInitializerContext,
MlCoreSetup,
MlHttpServiceSetup,
} from './server/new_platform/plugin';
// @ts-ignore: could not find declaration file for module
import mappings from './mappings';

interface MlServer extends Server {
addAppLinksToSampleDataset: () => {};
}

export const ml = (kibana: any) => {
return new kibana.Plugin({
require: ['kibana', 'elasticsearch', 'xpack_main'],
id: 'ml',
configPrefix: 'xpack.ml',
publicDir: resolve(__dirname, 'public'),

uiExports: {
app: {
title: i18n.translate('xpack.ml.mlNavTitle', {
defaultMessage: 'Machine Learning',
}),
description: i18n.translate('xpack.ml.mlNavDescription', {
defaultMessage: 'Machine Learning for the Elastic Stack',
}),
icon: 'plugins/ml/ml.svg',
euiIconType: 'machineLearningApp',
main: 'plugins/ml/app',
},
styleSheetPaths: resolve(__dirname, 'public/index.scss'),
hacks: ['plugins/ml/hacks/toggle_app_link_in_nav'],
savedObjectSchemas: {
'ml-telemetry': {
isNamespaceAgnostic: true,
},
},
mappings,
home: ['plugins/ml/register_feature'],
injectDefaultVars(server: any) {
const config = server.config();
return {
mlEnabled: config.get('xpack.ml.enabled'),
};
},
},

async init(server: MlServer) {
const kbnServer = (server as unknown) as KbnServer;

const initializerContext = ({
legacyConfig: server.config(),
logger: {
get(...contextParts: string[]) {
return kbnServer.newPlatform.coreContext.logger.get('plugins', 'ml', ...contextParts);
},
},
} as unknown) as MlInitializerContext;

const mlHttpService: MlHttpServiceSetup = {
...kbnServer.newPlatform.setup.core.http,
route: server.route.bind(server),
};

const core: MlCoreSetup = {
addAppLinksToSampleDataset: server.addAppLinksToSampleDataset,
injectUiAppVars: server.injectUiAppVars,
http: mlHttpService,
savedObjects: server.savedObjects,
usage: server.usage,
};

const plugins = {
elasticsearch: server.plugins.elasticsearch,
security: server.plugins.security,
xpackMain: server.plugins.xpack_main,
ml: this,
};

plugin(initializerContext).setup(core, plugins);
},
});
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { Server } from 'hapi';
import { ElasticsearchPlugin } from 'src/legacy/core_plugins/elasticsearch';

export function callWithInternalUserFactory(server: Server): any;
export function callWithInternalUserFactory(elasticsearchPlugin: ElasticsearchPlugin): any;
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@

import { once } from 'lodash';

const _callWithInternalUser = once((server) => {
const { callWithInternalUser } = server.plugins.elasticsearch.getCluster('admin');
const _callWithInternalUser = once((elasticsearchPlugin) => {
const { callWithInternalUser } = elasticsearchPlugin.getCluster('admin');
return callWithInternalUser;
});

export const callWithInternalUserFactory = (server) => {
export const callWithInternalUserFactory = (elasticsearchPlugin) => {
return (...args) => {
return _callWithInternalUser(server)(...args);
return _callWithInternalUser(elasticsearchPlugin)(...args);
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,21 @@ import { callWithInternalUserFactory } from './call_with_internal_user_factory';

describe('call_with_internal_user_factory', () => {
describe('callWithInternalUserFactory', () => {
let server: any;
let elasticsearchPlugin: any;
let callWithInternalUser: any;

beforeEach(() => {
callWithInternalUser = jest.fn();
server = {
plugins: {
elasticsearch: {
getCluster: jest.fn(() => ({ callWithInternalUser })),
},
},
elasticsearchPlugin = {
getCluster: jest.fn(() => ({ callWithInternalUser })),
};
});

it('should use internal user "admin"', () => {
const callWithInternalUserInstance = callWithInternalUserFactory(server);
const callWithInternalUserInstance = callWithInternalUserFactory(elasticsearchPlugin);
callWithInternalUserInstance();

expect(server.plugins.elasticsearch.getCluster).toHaveBeenCalledWith('admin');
expect(elasticsearchPlugin.getCluster).toHaveBeenCalledWith('admin');
});
});
});
8 changes: 4 additions & 4 deletions x-pack/plugins/ml/server/client/call_with_request_factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
import { once } from 'lodash';
import { elasticsearchJsPlugin } from './elasticsearch_ml';

const callWithRequest = once((server) => {
const callWithRequest = once((elasticsearchPlugin) => {
const config = { plugins: [ elasticsearchJsPlugin ] };
const cluster = server.plugins.elasticsearch.createCluster('ml', config);
const cluster = elasticsearchPlugin.createCluster('ml', config);

return cluster.callWithRequest;
});

export const callWithRequestFactory = (server, request) => {
export const callWithRequestFactory = (elasticsearchPlugin, request) => {
return (...args) => {
return callWithRequest(server)(request, ...args);
return callWithRequest(elasticsearchPlugin)(request, ...args);
};
};
4 changes: 2 additions & 2 deletions x-pack/plugins/ml/server/client/log.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@

export let mlLog = () => {};

export function initMlServerLog(server) {
mlLog = (level, message) => server.log(['ml', level], message);
export function initMlServerLog(log) {
mlLog = (level, message) => log(['ml', level], message);
}
22 changes: 9 additions & 13 deletions x-pack/plugins/ml/server/lib/__tests__/security_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,29 @@ import {

describe('ML - security utils', () => {

function mockServerFactory(isAvailable = true, isEnabled = true) {
function mockXpackMainPluginFactory(isAvailable = true, isEnabled = true) {
return {
plugins: {
xpack_main: {
info: {
isAvailable: () => isAvailable,
feature: () => ({
isEnabled: () => isEnabled
})
}
}
info: {
isAvailable: () => isAvailable,
feature: () => ({
isEnabled: () => isEnabled
})
}
};
}

describe('isSecurityDisabled', () => {

it('returns not disabled for given mock server object #1', () => {
expect(isSecurityDisabled(mockServerFactory())).to.be(false);
expect(isSecurityDisabled(mockXpackMainPluginFactory())).to.be(false);
});

it('returns not disabled for given mock server object #2', () => {
expect(isSecurityDisabled(mockServerFactory(false))).to.be(false);
expect(isSecurityDisabled(mockXpackMainPluginFactory(false))).to.be(false);
});

it('returns disabled for given mock server object #3', () => {
expect(isSecurityDisabled(mockServerFactory(true, false))).to.be(true);
expect(isSecurityDisabled(mockXpackMainPluginFactory(true, false))).to.be(true);
});

});
Expand Down
Loading

0 comments on commit c47a0d5

Please sign in to comment.