Skip to content

Commit

Permalink
Merge branch '7.x' into backport/7.x/pr-88675
Browse files Browse the repository at this point in the history
  • Loading branch information
kibanamachine authored Jan 28, 2021
2 parents 5cbfd2b + c7b900b commit a3d593b
Show file tree
Hide file tree
Showing 51 changed files with 763 additions and 391 deletions.
4 changes: 0 additions & 4 deletions docs/developer/plugin-list.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -555,10 +555,6 @@ in their infrastructure.
|NOTE: This plugin contains implementation of URL drilldown. For drilldowns infrastructure code refer to ui_actions_enhanced plugin.
|{kib-repo}blob/{branch}/x-pack/plugins/vis_type_timeseries_enhanced/README.md[visTypeTimeseriesEnhanced]
|The vis_type_timeseries_enhanced plugin is the x-pack counterpart to the OSS vis_type_timeseries plugin.
|{kib-repo}blob/{branch}/x-pack/plugins/watcher/README.md[watcher]
|This plugins adopts some conventions in addition to or in place of conventions in Kibana (at the time of the plugin's creation):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@

## PluginInitializerContext.config property

Accessors for the plugin's configuration

<b>Signature:</b>

```typescript
config: {
legacy: {
globalConfig$: Observable<SharedGlobalConfig>;
get: () => SharedGlobalConfig;
};
create: <T = ConfigSchema>() => Observable<T>;
createIfExists: <T = ConfigSchema>() => Observable<T | undefined>;
get: <T = ConfigSchema>() => T;
};
```
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,29 @@

## PluginInitializerContext.logger property

instance already bound to the plugin's logging context

<b>Signature:</b>

```typescript
logger: LoggerFactory;
```

## Example


```typescript
// plugins/my-plugin/server/plugin.ts
// "id: myPlugin" in `plugins/my-plugin/kibana.yaml`

export class MyPlugin implements Plugin {
constructor(private readonly initContext: PluginInitializerContext) {
this.logger = initContext.logger.get();
// `logger` context: `plugins.myPlugin`
this.mySubLogger = initContext.logger.get('sub'); // or this.logger.get('sub');
// `mySubLogger` context: `plugins.myPlugin.sub`
}
}

```

Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ 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> createIfExists: &lt;T = ConfigSchema&gt;() =&gt; Observable&lt;T &#124; undefined&gt;;</code><br/><code> }</code> | |
| [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> | |
| [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> | |

3 changes: 2 additions & 1 deletion packages/kbn-config/src/config_service.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ const createConfigServiceMock = ({
}: { atPath?: Record<string, any>; getConfig$?: Record<string, any> } = {}) => {
const mocked: jest.Mocked<IConfigService> = {
atPath: jest.fn(),
atPathSync: jest.fn(),
getConfig$: jest.fn(),
optionalAtPath: jest.fn(),
getUsedPaths: jest.fn(),
getUnusedPaths: jest.fn(),
isEnabledAtPath: jest.fn(),
Expand All @@ -27,6 +27,7 @@ const createConfigServiceMock = ({
validate: jest.fn(),
};
mocked.atPath.mockReturnValue(new BehaviorSubject(atPath));
mocked.atPathSync.mockReturnValue(atPath);
mocked.getConfig$.mockReturnValue(new BehaviorSubject(new ObjectToConfigAdapter(getConfig$)));
mocked.getUsedPaths.mockResolvedValue([]);
mocked.getUnusedPaths.mockResolvedValue([]);
Expand Down
112 changes: 66 additions & 46 deletions packages/kbn-config/src/config_service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,27 +105,6 @@ test('re-validate config when updated', async () => {
`);
});

test("returns undefined if fetching optional config at a path that doesn't exist", async () => {
const rawConfig = getRawConfigProvider({});
const configService = new ConfigService(rawConfig, defaultEnv, logger);

const value$ = configService.optionalAtPath('unique-name');
const value = await value$.pipe(first()).toPromise();

expect(value).toBeUndefined();
});

test('returns observable config at optional path if it exists', async () => {
const rawConfig = getRawConfigProvider({ value: 'bar' });
const configService = new ConfigService(rawConfig, defaultEnv, logger);
await configService.setSchema('value', schema.string());

const value$ = configService.optionalAtPath('value');
const value: any = await value$.pipe(first()).toPromise();

expect(value).toBe('bar');
});

test("does not push new configs when reloading if config at path hasn't changed", async () => {
const rawConfig$ = new BehaviorSubject<Record<string, any>>({ key: 'value' });
const rawConfigProvider = rawConfigServiceMock.create({ rawConfig$ });
Expand Down Expand Up @@ -209,34 +188,38 @@ test('flags schema paths as handled when registering a schema', async () => {

test('tracks unhandled paths', async () => {
const initialConfig = {
bar: {
deep1: {
key: '123',
},
deep2: {
key: '321',
},
service: {
string: 'str',
number: 42,
},
foo: 'value',
quux: {
deep1: {
key: 'hello',
},
deep2: {
key: 'world',
},
plugin: {
foo: 'bar',
},
unknown: {
hello: 'dolly',
number: 9000,
},
};

const rawConfigProvider = rawConfigServiceMock.create({ rawConfig: initialConfig });
const configService = new ConfigService(rawConfigProvider, defaultEnv, logger);

configService.atPath('foo');
configService.atPath(['bar', 'deep2']);
await configService.setSchema(
'service',
schema.object({
string: schema.string(),
number: schema.number(),
})
);
await configService.setSchema(
'plugin',
schema.object({
foo: schema.string(),
})
);

const unused = await configService.getUnusedPaths();

expect(unused).toEqual(['bar.deep1.key', 'quux.deep1.key', 'quux.deep2.key']);
expect(unused).toEqual(['unknown.hello', 'unknown.number']);
});

test('correctly passes context', async () => {
Expand Down Expand Up @@ -339,22 +322,18 @@ test('does not throw if schema does not define "enabled" schema', async () => {

const rawConfigProvider = rawConfigServiceMock.create({ rawConfig: initialConfig });
const configService = new ConfigService(rawConfigProvider, defaultEnv, logger);
await expect(
expect(
configService.setSchema(
'pid',
schema.object({
file: schema.string(),
})
)
).resolves.toBeUndefined();
).toBeUndefined();

const value$ = configService.atPath('pid');
const value: any = await value$.pipe(first()).toPromise();
expect(value.enabled).toBe(undefined);

const valueOptional$ = configService.optionalAtPath('pid');
const valueOptional: any = await valueOptional$.pipe(first()).toPromise();
expect(valueOptional.enabled).toBe(undefined);
});

test('treats config as enabled if config path is not present in config', async () => {
Expand Down Expand Up @@ -457,3 +436,44 @@ test('logs deprecation warning during validation', async () => {
]
`);
});

describe('atPathSync', () => {
test('returns the value at path', async () => {
const rawConfig = getRawConfigProvider({ key: 'foo' });
const configService = new ConfigService(rawConfig, defaultEnv, logger);
const stringSchema = schema.string();
await configService.setSchema('key', stringSchema);

await configService.validate();

const value = configService.atPathSync('key');
expect(value).toBe('foo');
});

test('throws if called before `validate`', async () => {
const rawConfig = getRawConfigProvider({ key: 'foo' });
const configService = new ConfigService(rawConfig, defaultEnv, logger);
const stringSchema = schema.string();
await configService.setSchema('key', stringSchema);

expect(() => configService.atPathSync('key')).toThrowErrorMatchingInlineSnapshot(
`"\`atPathSync\` called before config was validated"`
);
});

test('returns the last config value', async () => {
const rawConfig$ = new BehaviorSubject<Record<string, any>>({ key: 'value' });
const rawConfigProvider = rawConfigServiceMock.create({ rawConfig$ });

const configService = new ConfigService(rawConfigProvider, defaultEnv, logger);
await configService.setSchema('key', schema.string());

await configService.validate();

expect(configService.atPathSync('key')).toEqual('value');

rawConfig$.next({ key: 'new-value' });

expect(configService.atPathSync('key')).toEqual('new-value');
});
});
Loading

0 comments on commit a3d593b

Please sign in to comment.