Skip to content

Commit

Permalink
add sync accessor for legacy config
Browse files Browse the repository at this point in the history
  • Loading branch information
pgayvallet committed Jan 28, 2021
1 parent cafc7a6 commit b833984
Show file tree
Hide file tree
Showing 9 changed files with 169 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Accessors for the plugin's configuration
config: {
legacy: {
globalConfig$: Observable<SharedGlobalConfig>;
get: () => SharedGlobalConfig;
};
create: <T = ConfigSchema>() => Observable<T>;
get: <T = ConfigSchema>() => T;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export interface PluginInitializerContext<ConfigSchema = unknown>

| Property | Type | Description |
| --- | --- | --- |
| [config](./kibana-plugin-core-server.plugininitializercontext.config.md) | <code>{</code><br/><code> legacy: {</code><br/><code> globalConfig$: Observable&lt;SharedGlobalConfig&gt;;</code><br/><code> };</code><br/><code> create: &lt;T = ConfigSchema&gt;() =&gt; Observable&lt;T&gt;;</code><br/><code> get: &lt;T = ConfigSchema&gt;() =&gt; T;</code><br/><code> }</code> | Accessors for the plugin's configuration |
| [config](./kibana-plugin-core-server.plugininitializercontext.config.md) | <code>{</code><br/><code> legacy: {</code><br/><code> globalConfig$: Observable&lt;SharedGlobalConfig&gt;;</code><br/><code> get: () =&gt; SharedGlobalConfig;</code><br/><code> };</code><br/><code> create: &lt;T = ConfigSchema&gt;() =&gt; Observable&lt;T&gt;;</code><br/><code> get: &lt;T = ConfigSchema&gt;() =&gt; T;</code><br/><code> }</code> | Accessors for the plugin's configuration |
| [env](./kibana-plugin-core-server.plugininitializercontext.env.md) | <code>{</code><br/><code> mode: EnvironmentMode;</code><br/><code> packageInfo: Readonly&lt;PackageInfo&gt;;</code><br/><code> instanceUuid: string;</code><br/><code> }</code> | |
| [logger](./kibana-plugin-core-server.plugininitializercontext.logger.md) | <code>LoggerFactory</code> | instance already bound to the plugin's logging context |
| [opaqueId](./kibana-plugin-core-server.plugininitializercontext.opaqueid.md) | <code>PluginOpaqueId</code> | |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export class LegacyObjectToConfigAdapter extends ObjectToConfigAdapter {
};
}

private static transformPlugins(configValue: LegacyVars) {
private static transformPlugins(configValue: LegacyVars = {}) {
// These properties are the only ones we use from the existing `plugins` config node
// since `scanDirs` isn't respected by new platform plugin discovery.
return {
Expand Down
5 changes: 4 additions & 1 deletion src/core/server/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,10 @@ export function pluginInitializerContextConfigMock<T>(config: T) {
};

const mock: jest.Mocked<PluginInitializerContext<T>['config']> = {
legacy: { globalConfig$: of(globalConfig) },
legacy: {
globalConfig$: of(globalConfig),
get: () => globalConfig,
},
create: jest.fn().mockReturnValue(of(config)),
get: jest.fn().mockReturnValue(config),
};
Expand Down
82 changes: 82 additions & 0 deletions src/core/server/plugins/legacy_config.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* and the Server Side Public License, v 1; you may not use this file except in
* compliance with, at your election, the Elastic License or the Server Side
* Public License, v 1.
*/

import { take } from 'rxjs/operators';
import { ConfigService, Env } from '@kbn/config';
import { getEnvOptions, rawConfigServiceMock } from '../config/mocks';
import { getGlobalConfig, getGlobalConfig$ } from './legacy_config';
import { REPO_ROOT } from '@kbn/utils';
import { loggingSystemMock } from '../logging/logging_system.mock';
import { duration } from 'moment';
import { fromRoot } from '../utils';
import { ByteSizeValue } from '@kbn/config-schema';
import { Server } from '../server';

describe('Legacy config', () => {
let env: Env;
let logger: ReturnType<typeof loggingSystemMock.create>;

beforeEach(() => {
env = Env.createDefault(REPO_ROOT, getEnvOptions());
logger = loggingSystemMock.create();
});

const createConfigService = (rawConfig: Record<string, any> = {}): ConfigService => {
const rawConfigService = rawConfigServiceMock.create({ rawConfig });
const server = new Server(rawConfigService, env, logger);
server.setupCoreConfig();
return server.configService;
};

describe('getGlobalConfig', () => {
it('should return the global config', async () => {
const configService = createConfigService();
await configService.validate();

const legacyConfig = getGlobalConfig(configService);

expect(legacyConfig).toStrictEqual({
kibana: {
index: '.kibana',
autocompleteTerminateAfter: duration(100000),
autocompleteTimeout: duration(1000),
},
elasticsearch: {
shardTimeout: duration(30, 's'),
requestTimeout: duration(30, 's'),
pingTimeout: duration(30, 's'),
},
path: { data: fromRoot('data') },
savedObjects: { maxImportPayloadBytes: new ByteSizeValue(26214400) },
});
});
});

describe('getGlobalConfig$', () => {
it('should return an observable for the global config', async () => {
const configService = createConfigService();

const legacyConfig = await getGlobalConfig$(configService).pipe(take(1)).toPromise();

expect(legacyConfig).toStrictEqual({
kibana: {
index: '.kibana',
autocompleteTerminateAfter: duration(100000),
autocompleteTimeout: duration(1000),
},
elasticsearch: {
shardTimeout: duration(30, 's'),
requestTimeout: duration(30, 's'),
pingTimeout: duration(30, 's'),
},
path: { data: fromRoot('data') },
savedObjects: { maxImportPayloadBytes: new ByteSizeValue(26214400) },
});
});
});
});
69 changes: 69 additions & 0 deletions src/core/server/plugins/legacy_config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* and the Server Side Public License, v 1; you may not use this file except in
* compliance with, at your election, the Elastic License or the Server Side
* Public License, v 1.
*/

import { map, shareReplay } from 'rxjs/operators';
import { combineLatest, Observable } from 'rxjs';
import { PathConfigType, config as pathConfig } from '@kbn/utils';
import { pick, deepFreeze } from '@kbn/std';
import { IConfigService } from '@kbn/config';

import { SharedGlobalConfig, SharedGlobalConfigKeys } from './types';
import { KibanaConfigType, config as kibanaConfig } from '../kibana_config';
import {
ElasticsearchConfigType,
config as elasticsearchConfig,
} from '../elasticsearch/elasticsearch_config';
import { SavedObjectsConfigType, savedObjectsConfig } from '../saved_objects/saved_objects_config';

const createGlobalConfig = ({
kibana,
elasticsearch,
path,
savedObjects,
}: {
kibana: KibanaConfigType;
elasticsearch: ElasticsearchConfigType;
path: PathConfigType;
savedObjects: SavedObjectsConfigType;
}): SharedGlobalConfig => {
return deepFreeze({
kibana: pick(kibana, SharedGlobalConfigKeys.kibana),
elasticsearch: pick(elasticsearch, SharedGlobalConfigKeys.elasticsearch),
path: pick(path, SharedGlobalConfigKeys.path),
savedObjects: pick(savedObjects, SharedGlobalConfigKeys.savedObjects),
});
};

export const getGlobalConfig = (configService: IConfigService): SharedGlobalConfig => {
return createGlobalConfig({
kibana: configService.atPathSync<KibanaConfigType>(kibanaConfig.path),
elasticsearch: configService.atPathSync<ElasticsearchConfigType>(elasticsearchConfig.path),
path: configService.atPathSync<PathConfigType>(pathConfig.path),
savedObjects: configService.atPathSync<SavedObjectsConfigType>(savedObjectsConfig.path),
});
};

export const getGlobalConfig$ = (configService: IConfigService): Observable<SharedGlobalConfig> => {
return combineLatest([
configService.atPath<KibanaConfigType>(kibanaConfig.path),
configService.atPath<ElasticsearchConfigType>(elasticsearchConfig.path),
configService.atPath<PathConfigType>(pathConfig.path),
configService.atPath<SavedObjectsConfigType>(savedObjectsConfig.path),
]).pipe(
map(
([kibana, elasticsearch, path, savedObjects]) =>
createGlobalConfig({
kibana,
elasticsearch,
path,
savedObjects,
}),
shareReplay(1)
)
);
};
36 changes: 5 additions & 31 deletions src/core/server/plugins/plugin_context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,14 @@
* Public License, v 1.
*/

import { map, shareReplay } from 'rxjs/operators';
import { combineLatest } from 'rxjs';
import { PathConfigType, config as pathConfig } from '@kbn/utils';
import { pick, deepFreeze } from '@kbn/std';
import { shareReplay } from 'rxjs/operators';
import type { RequestHandlerContext } from 'src/core/server';
import { CoreContext } from '../core_context';
import { PluginWrapper } from './plugin';
import { PluginsServiceSetupDeps, PluginsServiceStartDeps } from './plugins_service';
import {
PluginInitializerContext,
PluginManifest,
PluginOpaqueId,
SharedGlobalConfigKeys,
} from './types';
import { KibanaConfigType, config as kibanaConfig } from '../kibana_config';
import {
ElasticsearchConfigType,
config as elasticsearchConfig,
} from '../elasticsearch/elasticsearch_config';
import { PluginInitializerContext, PluginManifest, PluginOpaqueId } from './types';
import { IRouter, RequestHandlerContextProvider } from '../http';
import { SavedObjectsConfigType, savedObjectsConfig } from '../saved_objects/saved_objects_config';
import { getGlobalConfig, getGlobalConfig$ } from './legacy_config';
import { CoreSetup, CoreStart } from '..';

export interface InstanceInfo {
Expand Down Expand Up @@ -78,21 +65,8 @@ export function createPluginInitializerContext(
*/
config: {
legacy: {
globalConfig$: combineLatest([
coreContext.configService.atPath<KibanaConfigType>(kibanaConfig.path),
coreContext.configService.atPath<ElasticsearchConfigType>(elasticsearchConfig.path),
coreContext.configService.atPath<PathConfigType>(pathConfig.path),
coreContext.configService.atPath<SavedObjectsConfigType>(savedObjectsConfig.path),
]).pipe(
map(([kibana, elasticsearch, path, savedObjects]) =>
deepFreeze({
kibana: pick(kibana, SharedGlobalConfigKeys.kibana),
elasticsearch: pick(elasticsearch, SharedGlobalConfigKeys.elasticsearch),
path: pick(path, SharedGlobalConfigKeys.path),
savedObjects: pick(savedObjects, SharedGlobalConfigKeys.savedObjects),
})
)
),
globalConfig$: getGlobalConfig$(coreContext.configService),
get: () => getGlobalConfig(coreContext.configService),
},

/**
Expand Down
5 changes: 4 additions & 1 deletion src/core/server/plugins/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,10 @@ export interface PluginInitializerContext<ConfigSchema = unknown> {
* @remarks Naming not final here, it may be renamed in a near future
* @deprecated Accessing configuration values outside of the plugin's config scope is highly discouraged
*/
legacy: { globalConfig$: Observable<SharedGlobalConfig> };
legacy: {
globalConfig$: Observable<SharedGlobalConfig>;
get: () => SharedGlobalConfig;
};
/**
* Return an observable of the plugin's configuration
*
Expand Down
3 changes: 2 additions & 1 deletion src/core/server/server.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1844,6 +1844,7 @@ export interface PluginInitializerContext<ConfigSchema = unknown> {
config: {
legacy: {
globalConfig$: Observable<SharedGlobalConfig>;
get: () => SharedGlobalConfig;
};
create: <T = ConfigSchema>() => Observable<T>;
get: <T = ConfigSchema>() => T;
Expand Down Expand Up @@ -3138,6 +3139,6 @@ export const validBodyOutput: readonly ["data", "stream"];
// src/core/server/plugins/types.ts:263:3 - (ae-forgotten-export) The symbol "KibanaConfigType" needs to be exported by the entry point index.d.ts
// src/core/server/plugins/types.ts:263:3 - (ae-forgotten-export) The symbol "SharedGlobalConfigKeys" needs to be exported by the entry point index.d.ts
// src/core/server/plugins/types.ts:266:3 - (ae-forgotten-export) The symbol "SavedObjectsConfigType" needs to be exported by the entry point index.d.ts
// src/core/server/plugins/types.ts:368:5 - (ae-unresolved-link) The @link reference could not be resolved: The package "kibana" does not have an export "create"
// src/core/server/plugins/types.ts:371:5 - (ae-unresolved-link) The @link reference could not be resolved: The package "kibana" does not have an export "create"

```

0 comments on commit b833984

Please sign in to comment.