From e1940699f1936994f95aa26d8e1cf6d455148bdf Mon Sep 17 00:00:00 2001 From: John Schulz Date: Wed, 23 Oct 2019 15:08:45 -0400 Subject: [PATCH 1/2] Use NP registerFeature --- x-pack/legacy/plugins/integrations_manager/index.ts | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/x-pack/legacy/plugins/integrations_manager/index.ts b/x-pack/legacy/plugins/integrations_manager/index.ts index e0079e04afab5a..3c39ddd0537c37 100644 --- a/x-pack/legacy/plugins/integrations_manager/index.ts +++ b/x-pack/legacy/plugins/integrations_manager/index.ts @@ -74,7 +74,14 @@ const pluginOptions: LegacyPluginOptions = { // 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 { newPlatform } = server; + const npSetup = newPlatform.setup; + const featuresPlugin = npSetup.plugins.features; + + if (!featuresPlugin) { + throw new Error('New Platform XPack Features plugin is not available.'); + } + featuresPlugin.registerFeature(feature); const getConfig$ = () => new BehaviorSubject(server.config().get(PLUGIN.CONFIG_PREFIX)).asObservable(); @@ -87,8 +94,8 @@ const pluginOptions: LegacyPluginOptions = { }; const coreSetup: CoreSetup = { - elasticsearch: server.newPlatform.setup.core.elasticsearch, - hapiServer: server.newPlatform.__internals.hapiServer, + elasticsearch: npSetup.core.elasticsearch, + hapiServer: newPlatform.__internals.hapiServer, }; new ServerPlugin(initializerContext).setup(coreSetup); From 27257cc7b529ba1d5d1a70dcafd7837fcb8961bf Mon Sep 17 00:00:00 2001 From: John Schulz Date: Thu, 24 Oct 2019 14:05:26 -0400 Subject: [PATCH 2/2] Added features plugin to deps. Used it in setup. Moved `registerFeature` from `init` to plugin setup. Moved rest of init into `createSetupShim`. `init` is now ```ts init(server: Legacy.Server) { const { initializerContext, coreSetup, pluginsSetup } = createSetupShim(server); new Plugin(initializerContext).setup(coreSetup, pluginsSetup); }, ``` Moved feature (argument for `registerFeature`) to separate file. Renamed plugin-specific `CoreSetup` to `EPMCoreSetup` --- .../plugins/integrations_manager/index.ts | 68 ++----------------- .../integrations_manager/server/feature.ts | 36 ++++++++++ .../integrations_manager/server/plugin.ts | 26 ++++--- .../integrations_manager/server/shim.ts | 42 ++++++++++++ 4 files changed, 101 insertions(+), 71 deletions(-) create mode 100644 x-pack/legacy/plugins/integrations_manager/server/feature.ts create mode 100644 x-pack/legacy/plugins/integrations_manager/server/shim.ts diff --git a/x-pack/legacy/plugins/integrations_manager/index.ts b/x-pack/legacy/plugins/integrations_manager/index.ts index 3c39ddd0537c37..4ebe74b10560e4 100644 --- a/x-pack/legacy/plugins/integrations_manager/index.ts +++ b/x-pack/legacy/plugins/integrations_manager/index.ts @@ -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, @@ -71,34 +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) { - const { newPlatform } = server; - const npSetup = newPlatform.setup; - const featuresPlugin = npSetup.plugins.features; - - if (!featuresPlugin) { - throw new Error('New Platform XPack Features plugin is not available.'); - } - featuresPlugin.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: npSetup.core.elasticsearch, - hapiServer: 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, diff --git a/x-pack/legacy/plugins/integrations_manager/server/feature.ts b/x-pack/legacy/plugins/integrations_manager/server/feature.ts new file mode 100644 index 00000000000000..1ab7c637545234 --- /dev/null +++ b/x-pack/legacy/plugins/integrations_manager/server/feature.ts @@ -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'], + }, + }, +}; diff --git a/x-pack/legacy/plugins/integrations_manager/server/plugin.ts b/x-pack/legacy/plugins/integrations_manager/server/plugin.ts index 124236eb5d035d..ec6b37162c494a 100644 --- a/x-pack/legacy/plugins/integrations_manager/server/plugin.ts +++ b/x-pack/legacy/plugins/integrations_manager/server/plugin.ts @@ -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; -export interface CoreSetup { - elasticsearch: _CoreSetup['elasticsearch']; +export interface EPMCoreSetup { + elasticsearch: CoreSetup['elasticsearch']; hapiServer: Server; } @@ -30,6 +29,10 @@ export interface PluginContext { esClient: IClusterClient; } +export interface PluginsSetup { + features: PluginSetupContract; +} + export class Plugin { public config$: Observable; @@ -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), @@ -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, diff --git a/x-pack/legacy/plugins/integrations_manager/server/shim.ts b/x-pack/legacy/plugins/integrations_manager/server/shim.ts new file mode 100644 index 00000000000000..b97075048b02b0 --- /dev/null +++ b/x-pack/legacy/plugins/integrations_manager/server/shim.ts @@ -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, + }; +};