From 8159a7d67dccecb70ca4e57ca1f9b4c104b09036 Mon Sep 17 00:00:00 2001 From: Jason Rhodes Date: Tue, 10 Sep 2019 21:41:53 -0400 Subject: [PATCH] Removes the configuration adapter --- x-pack/legacy/plugins/infra/index.ts | 5 +- .../plugins/infra/server/kibana.index.ts | 8 +- .../adapters/configuration/adapter_types.ts | 19 ----- .../lib/adapters/configuration/index.ts | 7 -- .../inmemory_configuration_adapter.ts | 16 ---- .../kibana_configuration_adapter.test.ts | 40 ---------- .../kibana_configuration_adapter.ts | 73 ------------------- .../infra/server/lib/compose/kibana.ts | 15 ++-- .../plugins/infra/server/lib/infra_types.ts | 4 +- .../infra/server/lib/sources/sources.test.ts | 25 +++---- .../infra/server/lib/sources/sources.ts | 7 +- .../server/new_platform_config.schema.ts | 18 +++++ .../infra/server/new_platform_index.ts | 3 +- .../infra/server/new_platform_plugin.ts | 16 +--- 14 files changed, 54 insertions(+), 202 deletions(-) delete mode 100644 x-pack/legacy/plugins/infra/server/lib/adapters/configuration/adapter_types.ts delete mode 100644 x-pack/legacy/plugins/infra/server/lib/adapters/configuration/index.ts delete mode 100644 x-pack/legacy/plugins/infra/server/lib/adapters/configuration/inmemory_configuration_adapter.ts delete mode 100644 x-pack/legacy/plugins/infra/server/lib/adapters/configuration/kibana_configuration_adapter.test.ts delete mode 100644 x-pack/legacy/plugins/infra/server/lib/adapters/configuration/kibana_configuration_adapter.ts create mode 100644 x-pack/legacy/plugins/infra/server/new_platform_config.schema.ts diff --git a/x-pack/legacy/plugins/infra/index.ts b/x-pack/legacy/plugins/infra/index.ts index 09812fd339e4cca..40fdf81d08823d6 100644 --- a/x-pack/legacy/plugins/infra/index.ts +++ b/x-pack/legacy/plugins/infra/index.ts @@ -82,7 +82,10 @@ export function infra(kibana: any) { const getConfig$ = () => new Observable(observer => { - observer.next(server.config().get('xpack.infra')); + server + .config() + .get('xpack.infra') + .then((value: T) => observer.next(value)); }); const initContext = { diff --git a/x-pack/legacy/plugins/infra/server/kibana.index.ts b/x-pack/legacy/plugins/infra/server/kibana.index.ts index 73aa61f66422e4f..291535bbb66610a 100644 --- a/x-pack/legacy/plugins/infra/server/kibana.index.ts +++ b/x-pack/legacy/plugins/infra/server/kibana.index.ts @@ -11,13 +11,17 @@ import { initInfraServer } from './infra_server'; import { compose } from './lib/compose/kibana'; import { UsageCollector } from './usage/usage_collector'; import { InternalCoreSetup } from '../../../../../src/core/server'; +import { InfraConfig } from './new_platform_config.schema'; export interface KbnServer extends Server { usage: any; } -export const initServerWithKibana = (core: InternalCoreSetup) => { - const libs = compose(kbnServer); +export const initServerWithKibana = (core: InternalCoreSetup, config: InfraConfig) => { + const libs = compose( + core, + config + ); initInfraServer(libs); // NP_TODO how do we replace this? diff --git a/x-pack/legacy/plugins/infra/server/lib/adapters/configuration/adapter_types.ts b/x-pack/legacy/plugins/infra/server/lib/adapters/configuration/adapter_types.ts deleted file mode 100644 index b0856cf3da361cf..000000000000000 --- a/x-pack/legacy/plugins/infra/server/lib/adapters/configuration/adapter_types.ts +++ /dev/null @@ -1,19 +0,0 @@ -/* - * 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. - */ - -export interface InfraConfigurationAdapter< - Configuration extends InfraBaseConfiguration = InfraBaseConfiguration -> { - get(): Promise; -} - -export interface InfraBaseConfiguration { - enabled: boolean; - query: { - partitionSize: number; - partitionFactor: number; - }; -} diff --git a/x-pack/legacy/plugins/infra/server/lib/adapters/configuration/index.ts b/x-pack/legacy/plugins/infra/server/lib/adapters/configuration/index.ts deleted file mode 100644 index 4e09b5d0e9e2dfa..000000000000000 --- a/x-pack/legacy/plugins/infra/server/lib/adapters/configuration/index.ts +++ /dev/null @@ -1,7 +0,0 @@ -/* - * 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. - */ - -export * from './adapter_types'; diff --git a/x-pack/legacy/plugins/infra/server/lib/adapters/configuration/inmemory_configuration_adapter.ts b/x-pack/legacy/plugins/infra/server/lib/adapters/configuration/inmemory_configuration_adapter.ts deleted file mode 100644 index 472fa729395656e..000000000000000 --- a/x-pack/legacy/plugins/infra/server/lib/adapters/configuration/inmemory_configuration_adapter.ts +++ /dev/null @@ -1,16 +0,0 @@ -/* - * 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 { InfraBaseConfiguration, InfraConfigurationAdapter } from './adapter_types'; - -export class InfraInmemoryConfigurationAdapter - implements InfraConfigurationAdapter { - constructor(private readonly configuration: Configuration) {} - - public async get() { - return this.configuration; - } -} diff --git a/x-pack/legacy/plugins/infra/server/lib/adapters/configuration/kibana_configuration_adapter.test.ts b/x-pack/legacy/plugins/infra/server/lib/adapters/configuration/kibana_configuration_adapter.test.ts deleted file mode 100644 index 4d87878e9aa873b..000000000000000 --- a/x-pack/legacy/plugins/infra/server/lib/adapters/configuration/kibana_configuration_adapter.test.ts +++ /dev/null @@ -1,40 +0,0 @@ -/* - * 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 { InfraKibanaConfigurationAdapter } from './kibana_configuration_adapter'; - -describe('the InfraKibanaConfigurationAdapter', () => { - test('queries the xpack.infra configuration of the server', async () => { - const mockConfig = { - get: jest.fn(), - }; - - const configurationAdapter = new InfraKibanaConfigurationAdapter({ - config: () => mockConfig, - }); - - await configurationAdapter.get(); - - expect(mockConfig.get).toBeCalledWith('xpack.infra'); - }); - - test('applies the query defaults', async () => { - const configurationAdapter = new InfraKibanaConfigurationAdapter({ - config: () => ({ - get: () => ({}), - }), - }); - - const configuration = await configurationAdapter.get(); - - expect(configuration).toMatchObject({ - query: { - partitionSize: expect.any(Number), - partitionFactor: expect.any(Number), - }, - }); - }); -}); diff --git a/x-pack/legacy/plugins/infra/server/lib/adapters/configuration/kibana_configuration_adapter.ts b/x-pack/legacy/plugins/infra/server/lib/adapters/configuration/kibana_configuration_adapter.ts deleted file mode 100644 index d3699a4820cf06e..000000000000000 --- a/x-pack/legacy/plugins/infra/server/lib/adapters/configuration/kibana_configuration_adapter.ts +++ /dev/null @@ -1,73 +0,0 @@ -/* - * 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 Joi from 'joi'; - -import { InfraBaseConfiguration, InfraConfigurationAdapter } from './adapter_types'; - -export class InfraKibanaConfigurationAdapter implements InfraConfigurationAdapter { - private readonly server: ServerWithConfig; - - constructor(server: any) { - if (!isServerWithConfig(server)) { - throw new Error('Failed to find configuration on server.'); - } - - this.server = server; - } - - public async get() { - const config = this.server.config(); - - if (!isKibanaConfiguration(config)) { - throw new Error('Failed to access configuration of server.'); - } - - const configuration = config.get('xpack.infra') || {}; - const configurationWithDefaults: InfraBaseConfiguration = { - enabled: true, - query: { - partitionSize: 75, - partitionFactor: 1.2, - ...(configuration.query || {}), - }, - ...configuration, - }; - - // we assume this to be the configuration because Kibana would have already validated it - return configurationWithDefaults; - } -} - -interface ServerWithConfig { - config(): any; -} - -function isServerWithConfig(maybeServer: any): maybeServer is ServerWithConfig { - return ( - Joi.validate( - maybeServer, - Joi.object({ - config: Joi.func().required(), - }).unknown() - ).error === null - ); -} - -interface KibanaConfiguration { - get(key: string): any; -} - -function isKibanaConfiguration(maybeConfiguration: any): maybeConfiguration is KibanaConfiguration { - return ( - Joi.validate( - maybeConfiguration, - Joi.object({ - get: Joi.func().required(), - }).unknown() - ).error === null - ); -} diff --git a/x-pack/legacy/plugins/infra/server/lib/compose/kibana.ts b/x-pack/legacy/plugins/infra/server/lib/compose/kibana.ts index 51537c3c9fde930..b0df1aba61fb4d1 100644 --- a/x-pack/legacy/plugins/infra/server/lib/compose/kibana.ts +++ b/x-pack/legacy/plugins/infra/server/lib/compose/kibana.ts @@ -3,10 +3,6 @@ * or more contributor license agreements. Licensed under the Elastic License; * you may not use this file except in compliance with the Elastic License. */ - -import { Server } from 'hapi'; - -import { InfraKibanaConfigurationAdapter } from '../adapters/configuration/kibana_configuration_adapter'; import { FrameworkFieldsAdapter } from '../adapters/fields/framework_fields_adapter'; import { InfraKibanaBackendFrameworkAdapter } from '../adapters/framework/kibana_framework_adapter'; import { InfraKibanaLogEntriesAdapter } from '../adapters/log_entries/kibana_log_entries_adapter'; @@ -20,12 +16,13 @@ import { InfraLogAnalysis } from '../log_analysis'; import { InfraSnapshot } from '../snapshot'; import { InfraSourceStatus } from '../source_status'; import { InfraSources } from '../sources'; +import { InfraConfig } from '../../new_platform_config.schema'; +import { InternalCoreSetup } from '../../../../../../../src/core/server'; -export function compose(server: Server): InfraBackendLibs { - const configuration = new InfraKibanaConfigurationAdapter(server); - const framework = new InfraKibanaBackendFrameworkAdapter(server); +export function compose(core: InternalCoreSetup, config: InfraConfig) { + const framework = new InfraKibanaBackendFrameworkAdapter(core); const sources = new InfraSources({ - configuration, + config, savedObjects: framework.getSavedObjectsService(), }); const sourceStatus = new InfraSourceStatus(new InfraElasticsearchSourceStatusAdapter(framework), { @@ -46,7 +43,7 @@ export function compose(server: Server): InfraBackendLibs { }; const libs: InfraBackendLibs = { - configuration, + configuration: config, // NP_TODO: Do we ever use this anywhere? framework, logAnalysis, snapshot, diff --git a/x-pack/legacy/plugins/infra/server/lib/infra_types.ts b/x-pack/legacy/plugins/infra/server/lib/infra_types.ts index b436bb7e4fe58d9..aee2371e5390506 100644 --- a/x-pack/legacy/plugins/infra/server/lib/infra_types.ts +++ b/x-pack/legacy/plugins/infra/server/lib/infra_types.ts @@ -5,7 +5,6 @@ */ import { InfraSourceConfiguration } from '../../public/graphql/types'; -import { InfraConfigurationAdapter } from './adapters/configuration'; import { InfraBackendFrameworkAdapter, InfraFrameworkRequest } from './adapters/framework'; import { InfraFieldsDomain } from './domains/fields_domain'; import { InfraLogEntriesDomain } from './domains/log_entries_domain'; @@ -14,6 +13,7 @@ import { InfraLogAnalysis } from './log_analysis/log_analysis'; import { InfraSnapshot } from './snapshot'; import { InfraSources } from './sources'; import { InfraSourceStatus } from './source_status'; +import { InfraConfig } from '../new_platform_config.schema'; export interface InfraDomainLibs { fields: InfraFieldsDomain; @@ -22,7 +22,7 @@ export interface InfraDomainLibs { } export interface InfraBackendLibs extends InfraDomainLibs { - configuration: InfraConfigurationAdapter; + configuration: InfraConfig; framework: InfraBackendFrameworkAdapter; logAnalysis: InfraLogAnalysis; snapshot: InfraSnapshot; diff --git a/x-pack/legacy/plugins/infra/server/lib/sources/sources.test.ts b/x-pack/legacy/plugins/infra/server/lib/sources/sources.test.ts index 2374a83a642dfc8..08a18987b48e9ac 100644 --- a/x-pack/legacy/plugins/infra/server/lib/sources/sources.test.ts +++ b/x-pack/legacy/plugins/infra/server/lib/sources/sources.test.ts @@ -3,15 +3,13 @@ * or more contributor license agreements. Licensed under the Elastic License; * you may not use this file except in compliance with the Elastic License. */ - -import { InfraInmemoryConfigurationAdapter } from '../adapters/configuration/inmemory_configuration_adapter'; import { InfraSources } from './sources'; describe('the InfraSources lib', () => { describe('getSourceConfiguration method', () => { test('returns a source configuration if it exists', async () => { const sourcesLib = new InfraSources({ - configuration: createMockStaticConfiguration({}), + config: createMockStaticConfiguration({}), savedObjects: createMockSavedObjectsService({ id: 'TEST_ID', version: 'foo', @@ -52,7 +50,7 @@ describe('the InfraSources lib', () => { test('adds missing attributes from the static configuration to a source configuration', async () => { const sourcesLib = new InfraSources({ - configuration: createMockStaticConfiguration({ + config: createMockStaticConfiguration({ default: { metricAlias: 'METRIC_ALIAS', logAlias: 'LOG_ALIAS', @@ -98,7 +96,7 @@ describe('the InfraSources lib', () => { test('adds missing attributes from the default configuration to a source configuration', async () => { const sourcesLib = new InfraSources({ - configuration: createMockStaticConfiguration({}), + config: createMockStaticConfiguration({}), savedObjects: createMockSavedObjectsService({ id: 'TEST_ID', version: 'foo', @@ -129,15 +127,14 @@ describe('the InfraSources lib', () => { }); }); -const createMockStaticConfiguration = (sources: any) => - new InfraInmemoryConfigurationAdapter({ - enabled: true, - query: { - partitionSize: 1, - partitionFactor: 1, - }, - sources, - }); +const createMockStaticConfiguration = (sources: any) => ({ + enabled: true, + query: { + partitionSize: 1, + partitionFactor: 1, + }, + sources, +}); const createMockSavedObjectsService = (savedObject?: any) => ({ getScopedSavedObjectsClient() { diff --git a/x-pack/legacy/plugins/infra/server/lib/sources/sources.ts b/x-pack/legacy/plugins/infra/server/lib/sources/sources.ts index d6c72b53afbd34a..fb721201c2a713b 100644 --- a/x-pack/legacy/plugins/infra/server/lib/sources/sources.ts +++ b/x-pack/legacy/plugins/infra/server/lib/sources/sources.ts @@ -12,7 +12,6 @@ import { identity, constant } from 'fp-ts/lib/function'; import { pipe } from 'fp-ts/lib/pipeable'; import { map, fold } from 'fp-ts/lib/Either'; import { Pick3 } from '../../../common/utility_types'; -import { InfraConfigurationAdapter } from '../adapters/configuration'; import { InfraFrameworkRequest, internalInfraFrameworkRequest } from '../adapters/framework'; import { defaultSourceConfiguration } from './defaults'; import { NotFoundError } from './errors'; @@ -25,9 +24,10 @@ import { SourceConfigurationSavedObjectRuntimeType, StaticSourceConfigurationRuntimeType, } from './types'; +import { InfraConfig } from '../../new_platform_config.schema'; interface Libs { - configuration: InfraConfigurationAdapter; + config: InfraConfig; savedObjects: Pick & Pick3; } @@ -187,7 +187,6 @@ export class InfraSources { } private async getStaticDefaultSourceConfiguration() { - const staticConfiguration = await this.libs.configuration.get(); const staticSourceConfiguration = pipe( runtimeTypes .type({ @@ -195,7 +194,7 @@ export class InfraSources { default: StaticSourceConfigurationRuntimeType, }), }) - .decode(staticConfiguration), + .decode(this.libs.config), map(({ sources: { default: defaultConfiguration } }) => defaultConfiguration), fold(constant({}), identity) ); diff --git a/x-pack/legacy/plugins/infra/server/new_platform_config.schema.ts b/x-pack/legacy/plugins/infra/server/new_platform_config.schema.ts new file mode 100644 index 000000000000000..715e9a9fff38ec3 --- /dev/null +++ b/x-pack/legacy/plugins/infra/server/new_platform_config.schema.ts @@ -0,0 +1,18 @@ +/* + * 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 { schema, TypeOf } from '@kbn/config-schema'; + +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; diff --git a/x-pack/legacy/plugins/infra/server/new_platform_index.ts b/x-pack/legacy/plugins/infra/server/new_platform_index.ts index 8c8a3f2a2030487..904d015f11f43fb 100644 --- a/x-pack/legacy/plugins/infra/server/new_platform_index.ts +++ b/x-pack/legacy/plugins/infra/server/new_platform_index.ts @@ -5,7 +5,8 @@ */ import { PluginInitializerContext } from 'src/core/server'; -import { InfraServerPlugin, config, InfraConfig } from './new_platform_plugin'; +import { InfraServerPlugin } from './new_platform_plugin'; +import { config, InfraConfig } from './new_platform_config.schema'; // NP_TODO: kibana NP needs "config" to be exported from here, I think? export { config, InfraConfig }; diff --git a/x-pack/legacy/plugins/infra/server/new_platform_plugin.ts b/x-pack/legacy/plugins/infra/server/new_platform_plugin.ts index 6777402ba155f5d..c90bad910b3b514 100644 --- a/x-pack/legacy/plugins/infra/server/new_platform_plugin.ts +++ b/x-pack/legacy/plugins/infra/server/new_platform_plugin.ts @@ -5,20 +5,8 @@ */ 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; +import { InfraConfig } from './new_platform_config.schema'; const DEFAULT_CONFIG: InfraConfig = { enabled: true, @@ -47,6 +35,6 @@ export class InfraServerPlugin { } setup(core: InternalCoreSetup) { - initServerWithKibana(core); + initServerWithKibana(core, this.config); } }