Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[EPM] Use NP registerFeature #49625

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 7 additions & 54 deletions x-pack/legacy/plugins/integrations_manager/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,46 +6,16 @@

import { resolve } from 'path';
import JoiNamespace from 'joi';
import { BehaviorSubject } from 'rxjs';
import { Legacy } from 'kibana';
import { LegacyPluginInitializer, LegacyPluginOptions } from 'src/legacy/types';
import { Feature } from '../../../plugins/features/server/feature';
import { PLUGIN } from './common/constants';
import manifest from './kibana.json';
import { CoreSetup, Plugin as ServerPlugin, EPMPluginInitializerContext } from './server/plugin';
import { mappings, savedObjectSchemas } from './server/saved_objects';
import { getConfigSchema } from './server/config';
import { Plugin, createSetupShim } from './server/plugin';
import { mappings, savedObjectSchemas } from './server/saved_objects';

const ROOT = `plugins/${PLUGIN.ID}`;

const feature: Feature = {
id: PLUGIN.ID,
name: PLUGIN.TITLE,
icon: PLUGIN.ICON,
navLinkId: PLUGIN.ID,
app: [PLUGIN.ID, 'kibana'],
catalogue: [PLUGIN.ID],
privileges: {
all: {
api: [PLUGIN.ID],
catalogue: [PLUGIN.ID],
savedObject: {
all: [],
read: [],
},
ui: ['show', 'save'],
},
read: {
api: [PLUGIN.ID],
catalogue: [PLUGIN.ID],
savedObject: {
all: [],
read: [],
},
ui: ['show'],
},
},
};

const pluginOptions: LegacyPluginOptions = {
id: PLUGIN.ID,
require: manifest.requiredPlugins,
Expand All @@ -71,27 +41,10 @@ const pluginOptions: LegacyPluginOptions = {
},
deprecations: undefined,
preInit: undefined,
// yes, any. See https://github.com/elastic/kibana/blob/master/x-pack/legacy/plugins/infra/server/lib/adapters/configuration/kibana_configuration_adapter.ts#L49-L58
// for a way around it, but this is Legacy Platform and I'm not sure these hoops are worth jumping through.
init(server: any) {
server.plugins.xpack_main.registerFeature(feature);

const getConfig$ = () =>
new BehaviorSubject(server.config().get(PLUGIN.CONFIG_PREFIX)).asObservable();

const initializerContext: EPMPluginInitializerContext = {
config: {
create: getConfig$,
createIfExists: getConfig$,
},
};

const coreSetup: CoreSetup = {
elasticsearch: server.newPlatform.setup.core.elasticsearch,
hapiServer: server.newPlatform.__internals.hapiServer,
};

new ServerPlugin(initializerContext).setup(coreSetup);
init(server: Legacy.Server) {
const { initializerContext, coreSetup, pluginsSetup } = createSetupShim(server);
const plugin = new Plugin(initializerContext);
plugin.setup(coreSetup, pluginsSetup);
},
postInit: undefined,
isEnabled: false,
Expand Down
36 changes: 36 additions & 0 deletions x-pack/legacy/plugins/integrations_manager/server/feature.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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 { PLUGIN } from '../common/constants';
import { Feature } from '../../../../plugins/features/server';

export const feature: Feature = {
id: PLUGIN.ID,
name: PLUGIN.TITLE,
icon: PLUGIN.ICON,
navLinkId: PLUGIN.ID,
app: [PLUGIN.ID, 'kibana'],
catalogue: [PLUGIN.ID],
privileges: {
all: {
api: [PLUGIN.ID],
catalogue: [PLUGIN.ID],
savedObject: {
all: [],
read: [],
},
ui: ['show', 'save'],
},
read: {
api: [PLUGIN.ID],
catalogue: [PLUGIN.ID],
savedObject: {
all: [],
read: [],
},
ui: ['show'],
},
},
};
26 changes: 16 additions & 10 deletions x-pack/legacy/plugins/integrations_manager/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,21 @@
*/

import { Observable } from 'rxjs';
import {
CoreSetup as _CoreSetup,
CoreStart,
IClusterClient,
PluginInitializerContext,
} from 'src/core/server';
import { EPMConfigSchema, epmConfigStore } from './config';
import { CoreSetup, CoreStart, IClusterClient, PluginInitializerContext } from 'src/core/server';
import { PLUGIN } from '../common/constants';
import { Server } from '../common/types';
import { EPMConfigSchema, epmConfigStore } from './config';
import { feature } from './feature';
import { fetchList } from './registry';
import { routes } from './routes';
import { PluginSetupContract } from '../../../../plugins/features/server';

export { createSetupShim } from './shim';

export type EPMPluginInitializerContext = Pick<PluginInitializerContext, 'config'>;

export interface CoreSetup {
elasticsearch: _CoreSetup['elasticsearch'];
export interface EPMCoreSetup {
elasticsearch: CoreSetup['elasticsearch'];
hapiServer: Server;
}

Expand All @@ -30,6 +29,10 @@ export interface PluginContext {
esClient: IClusterClient;
}

export interface PluginsSetup {
features: PluginSetupContract;
}

export class Plugin {
public config$: Observable<EPMConfigSchema>;

Expand All @@ -39,7 +42,7 @@ export class Plugin {
epmConfigStore.updateConfig(configValue);
});
}
public setup(core: CoreSetup) {
public setup(core: EPMCoreSetup, plugins: PluginsSetup) {
const { elasticsearch, hapiServer } = core;
const pluginContext: PluginContext = {
esClient: elasticsearch.createClient(PLUGIN.ID),
Expand All @@ -61,6 +64,9 @@ export class Plugin {
// map routes to handlers
hapiServer.route(routesWithContext);

// register the plugin
plugins.features.registerFeature(feature);

// the JS API for other consumers
return {
getList: fetchList,
Expand Down
42 changes: 42 additions & 0 deletions x-pack/legacy/plugins/integrations_manager/server/shim.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* 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 { Legacy } from 'kibana';
import { BehaviorSubject } from 'rxjs';
import { PLUGIN } from '../common/constants';
import { EPMCoreSetup, EPMPluginInitializerContext, PluginsSetup } from './plugin';

// yes, any. See https://github.com/elastic/kibana/blob/master/x-pack/legacy/plugins/infra/server/lib/adapters/configuration/kibana_configuration_adapter.ts#L49-L58
// for a way around it, but this is Legacy Platform and I'm not sure these hoops are worth jumping through.
export const createSetupShim = (server: any) => {
const newPlatform: Legacy.Server['newPlatform'] = server.newPlatform;
const npSetup = newPlatform.setup;
const getConfig$ = () =>
new BehaviorSubject(server.config().get(PLUGIN.CONFIG_PREFIX)).asObservable();

const initializerContext: EPMPluginInitializerContext = {
config: {
create: getConfig$,
createIfExists: getConfig$,
},
};

const coreSetup: EPMCoreSetup = {
elasticsearch: npSetup.core.elasticsearch,
hapiServer: newPlatform.__internals.hapiServer,
};

const pluginsSetup = {
// @ts-ignore: New Platform not typed
features: npSetup.plugins.features as PluginsSetup['features'],
};

return {
initializerContext,
coreSetup,
pluginsSetup,
};
};