remove legacy uiSettings mixin (#76255)

* remove legacy uiSettings mixin

* adapt ui_render mixin to use core APIs
This commit is contained in:
Pierre Gayvallet 2020-08-31 16:59:24 +02:00 committed by GitHub
parent 137aadb830
commit dc37cca082
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 8 additions and 402 deletions

View file

@ -73,9 +73,9 @@ export function getServices() {
httpServerMock.createKibanaRequest()
);
const uiSettings = kbnServer.server.uiSettingsServiceFactory({
savedObjectsClient,
});
const uiSettings = kbnServer.newPlatform.start.core.uiSettings.asScopedToClient(
savedObjectsClient
);
services = {
kbnServer,

View file

@ -45,7 +45,6 @@ import { LegacyConfig, ILegacyService, ILegacyInternals } from '../../core/serve
import { UiPlugins } from '../../core/server/plugins';
import { CallClusterWithRequest, ElasticsearchPlugin } from '../core_plugins/elasticsearch';
import { UsageCollectionSetup } from '../../plugins/usage_collection/server';
import { UiSettingsServiceFactoryOptions } from '../../legacy/ui/ui_settings/ui_settings_service_factory';
import { HomeServerPluginSetup } from '../../plugins/home/server';
// lot of legacy code was assuming this type only had these two methods
@ -78,7 +77,6 @@ declare module 'hapi' {
name: string,
factoryFn: (request: Request) => Record<string, any>
) => void;
uiSettingsServiceFactory: (options?: UiSettingsServiceFactoryOptions) => IUiSettingsClient;
logWithMetadata: (tags: string[], message: string, meta: Record<string, any>) => void;
newPlatform: KbnServer['newPlatform'];
}

View file

@ -19,10 +19,8 @@
import { uiAppsMixin } from './ui_apps';
import { uiRenderMixin } from './ui_render';
import { uiSettingsMixin } from './ui_settings';
export async function uiMixin(kbnServer) {
await kbnServer.mixin(uiAppsMixin);
await kbnServer.mixin(uiSettingsMixin);
await kbnServer.mixin(uiRenderMixin);
}

View file

@ -21,6 +21,7 @@ import { createHash } from 'crypto';
import Boom from 'boom';
import { i18n } from '@kbn/i18n';
import * as UiSharedDeps from '@kbn/ui-shared-deps';
import { KibanaRequest } from '../../../core/server';
import { AppBootstrap } from './bootstrap';
import { getApmConfig } from '../apm';
@ -79,7 +80,10 @@ export function uiRenderMixin(kbnServer, server, config) {
auth: authEnabled ? { mode: 'try' } : false,
},
async handler(request, h) {
const uiSettings = request.getUiSettingsService();
const soClient = kbnServer.newPlatform.start.core.savedObjects.getScopedClient(
KibanaRequest.from(request)
);
const uiSettings = kbnServer.newPlatform.start.core.uiSettings.asScopedToClient(soClient);
const darkMode =
!authEnabled || request.auth.isAuthenticated

View file

@ -1,20 +0,0 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
export { uiSettingsMixin } from './ui_settings_mixin';

View file

@ -1,179 +0,0 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import sinon from 'sinon';
import expect from '@kbn/expect';
import { savedObjectsClientMock } from '../../../../core/server/mocks';
import * as uiSettingsServiceFactoryNS from '../ui_settings_service_factory';
import * as getUiSettingsServiceForRequestNS from '../ui_settings_service_for_request';
// @ts-ignore
import { uiSettingsMixin } from '../ui_settings_mixin';
interface Decorators {
server: { [name: string]: any };
request: { [name: string]: any };
}
const uiSettingDefaults = {
application: {
defaultProperty1: 'value1',
},
};
describe('uiSettingsMixin()', () => {
const sandbox = sinon.createSandbox();
function setup() {
// maps of decorations passed to `server.decorate()`
const decorations: Decorators = {
server: {},
request: {},
};
// mock hapi server
const server = {
log: sinon.stub(),
route: sinon.stub(),
addMemoizedFactoryToRequest(name: string, factory: (...args: any[]) => any) {
this.decorate('request', name, function (this: typeof server) {
return factory(this);
});
},
decorate: sinon.spy((type: keyof Decorators, name: string, value: any) => {
decorations[type][name] = value;
}),
newPlatform: {
setup: {
core: {
uiSettings: {
register: sinon.stub(),
},
},
},
},
};
// "promise" returned from kbnServer.ready()
const readyPromise = {
then: sinon.stub(),
};
const kbnServer = {
server,
uiExports: { uiSettingDefaults },
ready: sinon.stub().returns(readyPromise),
};
uiSettingsMixin(kbnServer, server);
return {
kbnServer,
server,
decorations,
readyPromise,
};
}
afterEach(() => sandbox.restore());
it('passes uiSettingsDefaults to the new platform', () => {
const { server } = setup();
sinon.assert.calledOnce(server.newPlatform.setup.core.uiSettings.register);
sinon.assert.calledWithExactly(
server.newPlatform.setup.core.uiSettings.register,
uiSettingDefaults
);
});
describe('server.uiSettingsServiceFactory()', () => {
it('decorates server with "uiSettingsServiceFactory"', () => {
const { decorations } = setup();
expect(decorations.server).to.have.property('uiSettingsServiceFactory').a('function');
const uiSettingsServiceFactoryStub = sandbox.stub(
uiSettingsServiceFactoryNS,
'uiSettingsServiceFactory'
);
sinon.assert.notCalled(uiSettingsServiceFactoryStub);
decorations.server.uiSettingsServiceFactory();
sinon.assert.calledOnce(uiSettingsServiceFactoryStub);
});
it('passes `server` and `options` argument to factory', () => {
const { decorations, server } = setup();
expect(decorations.server).to.have.property('uiSettingsServiceFactory').a('function');
const uiSettingsServiceFactoryStub = sandbox.stub(
uiSettingsServiceFactoryNS,
'uiSettingsServiceFactory'
);
sinon.assert.notCalled(uiSettingsServiceFactoryStub);
const savedObjectsClient = savedObjectsClientMock.create();
decorations.server.uiSettingsServiceFactory({
savedObjectsClient,
});
sinon.assert.calledOnce(uiSettingsServiceFactoryStub);
sinon.assert.calledWithExactly(uiSettingsServiceFactoryStub, server as any, {
savedObjectsClient,
});
});
});
describe('request.getUiSettingsService()', () => {
it('exposes "getUiSettingsService" on requests', () => {
const { decorations } = setup();
expect(decorations.request).to.have.property('getUiSettingsService').a('function');
const getUiSettingsServiceForRequestStub = sandbox.stub(
getUiSettingsServiceForRequestNS,
'getUiSettingsServiceForRequest'
);
sinon.assert.notCalled(getUiSettingsServiceForRequestStub);
decorations.request.getUiSettingsService();
sinon.assert.calledOnce(getUiSettingsServiceForRequestStub);
});
it('passes request to getUiSettingsServiceForRequest', () => {
const { server, decorations } = setup();
expect(decorations.request).to.have.property('getUiSettingsService').a('function');
const getUiSettingsServiceForRequestStub = sandbox.stub(
getUiSettingsServiceForRequestNS,
'getUiSettingsServiceForRequest'
);
sinon.assert.notCalled(getUiSettingsServiceForRequestStub);
const request = {};
decorations.request.getUiSettingsService.call(request);
sinon.assert.calledWith(getUiSettingsServiceForRequestStub, server as any, request as any);
});
});
describe('server.uiSettings()', () => {
it('throws an error, links to pr', () => {
const { decorations } = setup();
expect(decorations.server).to.have.property('uiSettings').a('function');
expect(() => {
decorations.server.uiSettings();
}).to.throwError('http://github.com' as any); // incorrect typings
});
});
});

View file

@ -1,62 +0,0 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
/**
* The UiExports class accepts consumer objects that it consults while
* trying to consume all of the `uiExport` declarations provided by
* plugins.
*
* UiExportConsumer is instantiated and passed to UiExports, then for
* every `uiExport` declaration the `exportConsumer(type)` method is
* with the key of the declaration. If this consumer knows how to handle
* that key we return a function that will be called with the plugins
* and values of all declarations using that key.
*
* With this, the consumer merges all of the declarations into the
* _uiSettingDefaults map, ensuring that there are not collisions along
* the way.
*
* @class UiExportsConsumer
*/
export class UiExportsConsumer {
_uiSettingDefaults = {};
exportConsumer(type) {
switch (type) {
case 'uiSettingDefaults':
return (plugin, settingDefinitions) => {
Object.keys(settingDefinitions).forEach((key) => {
if (key in this._uiSettingDefaults) {
throw new Error(`uiSettingDefaults for key "${key}" are already defined`);
}
this._uiSettingDefaults[key] = settingDefinitions[key];
});
};
}
}
/**
* Get the map of uiSettingNames to "default" specifications
* @return {Object<String,UiSettingsDefault>}
*/
getUiSettingDefaults() {
return this._uiSettingDefaults;
}
}

View file

@ -1,55 +0,0 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import { uiSettingsServiceFactory } from './ui_settings_service_factory';
import { getUiSettingsServiceForRequest } from './ui_settings_service_for_request';
export function uiSettingsMixin(kbnServer, server) {
const { uiSettingDefaults = {} } = kbnServer.uiExports;
const mergedUiSettingDefaults = Object.keys(uiSettingDefaults).reduce((acc, currentKey) => {
const defaultSetting = uiSettingDefaults[currentKey];
const updatedDefaultSetting = {
...defaultSetting,
};
if (typeof defaultSetting.options === 'function') {
updatedDefaultSetting.options = defaultSetting.options(server);
}
if (typeof defaultSetting.value === 'function') {
updatedDefaultSetting.value = defaultSetting.value(server);
}
acc[currentKey] = updatedDefaultSetting;
return acc;
}, {});
server.newPlatform.setup.core.uiSettings.register(mergedUiSettingDefaults);
server.decorate('server', 'uiSettingsServiceFactory', (options = {}) => {
return uiSettingsServiceFactory(server, options);
});
server.addMemoizedFactoryToRequest('getUiSettingsService', (request) => {
return getUiSettingsServiceForRequest(server, request);
});
server.decorate('server', 'uiSettings', () => {
throw new Error(`
server.uiSettings has been removed, see https://github.com/elastic/kibana/pull/12243.
`);
});
}

View file

@ -1,36 +0,0 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import { Legacy } from 'kibana';
import { IUiSettingsClient, SavedObjectsClientContract } from 'src/core/server';
export interface UiSettingsServiceFactoryOptions {
savedObjectsClient: SavedObjectsClientContract;
}
/**
* Create an instance of UiSettingsClient that will use the
* passed `savedObjectsClient` to communicate with elasticsearch
*
* @return {IUiSettingsClient}
*/
export function uiSettingsServiceFactory(
server: Legacy.Server,
options: UiSettingsServiceFactoryOptions
): IUiSettingsClient {
return server.newPlatform.start.core.uiSettings.asScopedToClient(options.savedObjectsClient);
}

View file

@ -1,42 +0,0 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import { Legacy } from 'kibana';
import { IUiSettingsClient } from 'src/core/server';
import { uiSettingsServiceFactory } from './ui_settings_service_factory';
/**
* Get/create an instance of UiSettingsService bound to a specific request.
* Each call is cached (keyed on the request object itself) and subsequent
* requests will get the first UiSettingsService instance even if the `options`
* have changed.
*
* @param {Hapi.Server} server
* @param {Hapi.Request} request
* @param {Object} [options={}]
* @return {IUiSettingsClient}
*/
export function getUiSettingsServiceForRequest(
server: Legacy.Server,
request: Legacy.Request
): IUiSettingsClient {
const savedObjectsClient = request.getSavedObjectsClient();
return uiSettingsServiceFactory(server, { savedObjectsClient });
}