Skip to content

Commit

Permalink
[EPM] Use NP registerFeature (#49625)
Browse files Browse the repository at this point in the history
* Use NP registerFeature

* 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`
  • Loading branch information
John Schulz authored Oct 30, 2019
1 parent c6b9b7e commit 3b3eca1
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 64 deletions.
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,
};
};

0 comments on commit 3b3eca1

Please sign in to comment.