diff --git a/x-pack/legacy/plugins/infra/index.ts b/x-pack/legacy/plugins/infra/index.ts index c6f13272edca389..09812fd339e4cca 100644 --- a/x-pack/legacy/plugins/infra/index.ts +++ b/x-pack/legacy/plugins/infra/index.ts @@ -7,9 +7,12 @@ import { i18n } from '@kbn/i18n'; import JoiNamespace from 'joi'; import { resolve } from 'path'; - -import { getConfigSchema, initServerWithKibana } from './server/kibana.index'; +import { PluginInitializerContext } from 'src/core/server'; +import KbnServer from 'src/legacy/server/kbn_server'; +import { Observable } from 'rxjs'; +import { getConfigSchema } from './server/kibana.index'; import { savedObjectMappings } from './server/saved_objects'; +import { plugin } from './server/new_platform_index'; const APP_ID = 'infra'; const logsSampleDataLinkLabel = i18n.translate('xpack.infra.sampleDataLinkLabel', { @@ -71,7 +74,27 @@ export function infra(kibana: any) { return getConfigSchema(Joi); }, init(server: any) { - initServerWithKibana(server); + // convert hapi instance to KbnServer + // `kbnServer.server` is the same hapi instance + // `kbnServer.newPlatform` has important values + const kbnServer = (server as unknown) as KbnServer; + const { core } = kbnServer.newPlatform.setup; + + const getConfig$ = () => + new Observable(observer => { + observer.next(server.config().get('xpack.infra')); + }); + + const initContext = { + config: { + create: getConfig$, + createIfExists: getConfig$, + }, + } as PluginInitializerContext; + + plugin(initContext).setup(core); + + // NP_TODO: How do we move this to new platform? server.addAppLinksToSampleDataset('logs', [ { path: `/app/${APP_ID}#/logs`, diff --git a/x-pack/legacy/plugins/infra/server/kibana.index.ts b/x-pack/legacy/plugins/infra/server/kibana.index.ts index ac22bf71f6f17a7..73aa61f66422e4f 100644 --- a/x-pack/legacy/plugins/infra/server/kibana.index.ts +++ b/x-pack/legacy/plugins/infra/server/kibana.index.ts @@ -10,15 +10,17 @@ import JoiNamespace from 'joi'; import { initInfraServer } from './infra_server'; import { compose } from './lib/compose/kibana'; import { UsageCollector } from './usage/usage_collector'; +import { InternalCoreSetup } from '../../../../../src/core/server'; export interface KbnServer extends Server { usage: any; } -export const initServerWithKibana = (kbnServer: KbnServer) => { +export const initServerWithKibana = (core: InternalCoreSetup) => { const libs = compose(kbnServer); initInfraServer(libs); + // NP_TODO how do we replace this? kbnServer.expose( 'defineInternalSourceConfiguration', libs.sources.defineInternalSourceConfiguration.bind(libs.sources) diff --git a/x-pack/legacy/plugins/infra/server/new_platform_index.ts b/x-pack/legacy/plugins/infra/server/new_platform_index.ts new file mode 100644 index 000000000000000..8c8a3f2a2030487 --- /dev/null +++ b/x-pack/legacy/plugins/infra/server/new_platform_index.ts @@ -0,0 +1,15 @@ +/* + * 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 { PluginInitializerContext } from 'src/core/server'; +import { InfraServerPlugin, config, InfraConfig } from './new_platform_plugin'; + +// NP_TODO: kibana NP needs "config" to be exported from here, I think? +export { config, InfraConfig }; + +export function plugin(context: PluginInitializerContext) { + return new InfraServerPlugin(context); +} diff --git a/x-pack/legacy/plugins/infra/server/new_platform_plugin.ts b/x-pack/legacy/plugins/infra/server/new_platform_plugin.ts new file mode 100644 index 000000000000000..6777402ba155f5d --- /dev/null +++ b/x-pack/legacy/plugins/infra/server/new_platform_plugin.ts @@ -0,0 +1,52 @@ +/* + * 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 { InternalCoreSetup, PluginInitializerContext } from 'src/core/server'; +import { Observable } from 'rxjs'; +import { schema, TypeOf } from '@kbn/config-schema'; +import { initServerWithKibana } from './kibana.index'; + +export const config = { + schema: schema.object({ + enabled: schema.boolean(), + query: schema.object({ + partitionSize: schema.maybe(schema.number()), + partitionFactor: schema.maybe(schema.number()), + }), + }), +}; + +export type InfraConfig = TypeOf; + +const DEFAULT_CONFIG: InfraConfig = { + enabled: true, + query: { + partitionSize: 75, + partitionFactor: 1.2, + }, +}; + +export class InfraServerPlugin { + public config$: Observable; + public config: InfraConfig = DEFAULT_CONFIG; + + constructor(context: PluginInitializerContext) { + this.config$ = context.config.create(); + this.config$.subscribe(configValue => { + this.config = { + ...DEFAULT_CONFIG, + enabled: configValue.enabled, + query: { + ...DEFAULT_CONFIG.query, + ...configValue.query, + }, + }; + }); + } + + setup(core: InternalCoreSetup) { + initServerWithKibana(core); + } +}