Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide uiSettings service in NP #48413

Merged
merged 23 commits into from
Oct 28, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/core/server/http/router/response_adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,11 @@ export class HapiResponseAdapter {
});

error.output.payload.message = getErrorMessage(payload);
error.output.payload.attributes = getErrorAttributes(payload);

const attributes = getErrorAttributes(payload);
if (attributes) {
error.output.payload.attributes = attributes;
}

const headers = kibanaResponse.options.headers;
if (headers) {
Expand Down
11 changes: 10 additions & 1 deletion src/core/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ import { PluginsServiceSetup, PluginsServiceStart, PluginOpaqueId } from './plug
import { ContextSetup } from './context';
import { SavedObjectsServiceStart } from './saved_objects';

import { InternalUiSettingsServiceSetup } from './ui_settings';
import {
InternalUiSettingsServiceSetup,
IUiSettingsClient,
UiSettingsServiceSetup,
} from './ui_settings';
import { SavedObjectsClientContract } from './saved_objects/types';

export { bootstrap } from './bootstrap';
Expand Down Expand Up @@ -211,6 +215,9 @@ export interface RequestHandlerContext {
dataClient: IScopedClusterClient;
adminClient: IScopedClusterClient;
};
uiSettings: {
client: IUiSettingsClient;
};
};
}

Expand All @@ -226,6 +233,8 @@ export interface CoreSetup {
elasticsearch: ElasticsearchServiceSetup;
/** {@link HttpServiceSetup} */
http: HttpServiceSetup;
/** {@link UiSettingsServiceSetup} */
uiSettings: UiSettingsServiceSetup;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/core/server/legacy/legacy_service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ beforeEach(() => {
core: {
context: contextServiceMock.createSetupContract(),
elasticsearch: { legacy: {} } as any,
uiSettings: uiSettingsServiceMock.createSetup(),
uiSettings: uiSettingsServiceMock.createSetupContract(),
http: {
...httpServiceMock.createSetupContract(),
auth: {
Expand Down
3 changes: 3 additions & 0 deletions src/core/server/legacy/legacy_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,9 @@ export class LegacyService implements CoreService<LegacyServiceSetup> {
basePath: setupDeps.core.http.basePath,
isTlsEnabled: setupDeps.core.http.isTlsEnabled,
},
uiSettings: {
setDefaults: setupDeps.core.uiSettings.setDefaults,
},
};
const coreStart: CoreStart = {};

Expand Down
16 changes: 16 additions & 0 deletions src/core/server/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { loggingServiceMock } from './logging/logging_service.mock';
import { elasticsearchServiceMock } from './elasticsearch/elasticsearch_service.mock';
import { httpServiceMock } from './http/http_service.mock';
import { contextServiceMock } from './context/context_service.mock';
import { uiSettingsServiceMock } from './ui_settings/ui_settings_service.mock';

export { httpServerMock } from './http/http_server.mocks';
export { sessionStorageMock } from './http/cookie_session_storage.mocks';
Expand Down Expand Up @@ -79,10 +80,14 @@ function createCoreSetupMock() {
};
httpMock.createRouter.mockImplementation(() => httpService.createRouter(''));

const uiSettingsMock = {
setDefaults: uiSettingsServiceMock.createSetupContract().setDefaults,
};
const mock: MockedKeys<CoreSetup> = {
context: contextServiceMock.createSetupContract(),
elasticsearch: elasticsearchServiceMock.createSetupContract(),
http: httpMock,
uiSettings: uiSettingsMock,
};

return mock;
Expand All @@ -94,8 +99,19 @@ function createCoreStartMock() {
return mock;
}

function createInternalCoreSetupMock() {
const setupDeps = {
context: contextServiceMock.createSetupContract(),
elasticsearch: elasticsearchServiceMock.createSetupContract(),
http: httpServiceMock.createSetupContract(),
uiSettings: uiSettingsServiceMock.createSetupContract(),
};
return setupDeps;
}

export const coreMock = {
createSetup: createCoreSetupMock,
createStart: createCoreStartMock,
createInternalSetup: createInternalCoreSetupMock,
createPluginInitializerContext: pluginInitializerContextMock,
};
10 changes: 2 additions & 8 deletions src/core/server/plugins/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,13 @@ import { schema } from '@kbn/config-schema';
import { Env } from '../config';
import { getEnvOptions } from '../config/__mocks__/env';
import { CoreContext } from '../core_context';
import { coreMock } from '../mocks';
import { configServiceMock } from '../config/config_service.mock';
import { elasticsearchServiceMock } from '../elasticsearch/elasticsearch_service.mock';
import { httpServiceMock } from '../http/http_service.mock';
import { loggingServiceMock } from '../logging/logging_service.mock';

import { PluginWrapper } from './plugin';
import { PluginManifest } from './types';
import { createPluginInitializerContext, createPluginSetupContext } from './plugin_context';
import { contextServiceMock } from '../context/context_service.mock';

const mockPluginInitializer = jest.fn();
const logger = loggingServiceMock.create();
Expand Down Expand Up @@ -68,11 +66,7 @@ configService.atPath.mockReturnValue(new BehaviorSubject({ initialize: true }));
let coreId: symbol;
let env: Env;
let coreContext: CoreContext;
const setupDeps = {
context: contextServiceMock.createSetupContract(),
elasticsearch: elasticsearchServiceMock.createSetupContract(),
http: httpServiceMock.createSetupContract(),
};
const setupDeps = coreMock.createInternalSetup();
beforeEach(() => {
coreId = Symbol('core');
env = Env.createDefault(getEnvOptions());
Expand Down
3 changes: 3 additions & 0 deletions src/core/server/plugins/plugin_context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ export function createPluginSetupContext<TPlugin, TPluginDependencies>(
basePath: deps.http.basePath,
isTlsEnabled: deps.http.isTlsEnabled,
},
uiSettings: {
setDefaults: deps.uiSettings.setDefaults,
},
};
}

Expand Down
10 changes: 2 additions & 8 deletions src/core/server/plugins/plugins_service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,13 @@ import { schema } from '@kbn/config-schema';

import { Config, ConfigService, Env, ObjectToConfigAdapter } from '../config';
import { getEnvOptions } from '../config/__mocks__/env';
import { elasticsearchServiceMock } from '../elasticsearch/elasticsearch_service.mock';
import { httpServiceMock } from '../http/http_service.mock';
import { coreMock } from '../mocks';
import { loggingServiceMock } from '../logging/logging_service.mock';
import { PluginDiscoveryError } from './discovery';
import { PluginWrapper } from './plugin';
import { PluginsService } from './plugins_service';
import { PluginsSystem } from './plugins_system';
import { config } from './plugins_config';
import { contextServiceMock } from '../context/context_service.mock';

const MockPluginsSystem: jest.Mock<PluginsSystem> = PluginsSystem as any;

Expand All @@ -42,11 +40,7 @@ let configService: ConfigService;
let coreId: symbol;
let env: Env;
let mockPluginSystem: jest.Mocked<PluginsSystem>;
const setupDeps = {
context: contextServiceMock.createSetupContract(),
elasticsearch: elasticsearchServiceMock.createSetupContract(),
http: httpServiceMock.createSetupContract(),
};
const setupDeps = coreMock.createInternalSetup();
const logger = loggingServiceMock.create();

['path-1', 'path-2', 'path-3', 'path-4', 'path-5'].forEach(path => {
Expand Down
11 changes: 3 additions & 8 deletions src/core/server/plugins/plugins_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,14 @@ import { Observable } from 'rxjs';
import { filter, first, map, mergeMap, tap, toArray } from 'rxjs/operators';
import { CoreService } from '../../types';
import { CoreContext } from '../core_context';
import { InternalElasticsearchServiceSetup } from '../elasticsearch';
import { InternalHttpServiceSetup } from '../http';

import { Logger } from '../logging';
import { discover, PluginDiscoveryError, PluginDiscoveryErrorType } from './discovery';
import { PluginWrapper } from './plugin';
import { DiscoveredPlugin, DiscoveredPluginInternal, PluginName } from './types';
import { PluginsConfig, PluginsConfigType } from './plugins_config';
import { PluginsSystem } from './plugins_system';
import { ContextSetup } from '../context';
import { InternalCoreSetup } from '..';

/** @public */
export interface PluginsServiceSetup {
Expand All @@ -46,11 +45,7 @@ export interface PluginsServiceStart {
}

/** @internal */
export interface PluginsServiceSetupDeps {
context: ContextSetup;
elasticsearch: InternalElasticsearchServiceSetup;
http: InternalHttpServiceSetup;
}
export type PluginsServiceSetupDeps = InternalCoreSetup;

/** @internal */
export interface PluginsServiceStartDeps {} // eslint-disable-line @typescript-eslint/no-empty-interface
Expand Down
13 changes: 5 additions & 8 deletions src/core/server/plugins/plugins_system.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ import { Env } from '../config';
import { getEnvOptions } from '../config/__mocks__/env';
import { CoreContext } from '../core_context';
import { configServiceMock } from '../config/config_service.mock';
import { elasticsearchServiceMock } from '../elasticsearch/elasticsearch_service.mock';
import { httpServiceMock } from '../http/http_service.mock';
import { loggingServiceMock } from '../logging/logging_service.mock';

import { PluginWrapper } from './plugin';
import { PluginName } from './types';
import { PluginsSystem } from './plugins_system';
import { contextServiceMock } from '../context/context_service.mock';

import { coreMock } from '../mocks';

const logger = loggingServiceMock.create();
function createPlugin(
Expand Down Expand Up @@ -67,11 +67,8 @@ const configService = configServiceMock.create();
configService.atPath.mockReturnValue(new BehaviorSubject({ initialize: true }));
let env: Env;
let coreContext: CoreContext;
const setupDeps = {
context: contextServiceMock.createSetupContract(),
elasticsearch: elasticsearchServiceMock.createSetupContract(),
http: httpServiceMock.createSetupContract(),
};
const setupDeps = coreMock.createInternalSetup();

beforeEach(() => {
env = Env.createDefault(getEnvOptions());

Expand Down
24 changes: 14 additions & 10 deletions src/core/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { take } from 'rxjs/operators';
import { Type } from '@kbn/config-schema';

import { ConfigService, Env, Config, ConfigPath } from './config';
import { ElasticsearchService, ElasticsearchServiceSetup } from './elasticsearch';
import { ElasticsearchService } from './elasticsearch';
import { HttpService, InternalHttpServiceSetup } from './http';
import { LegacyService } from './legacy';
import { Logger, LoggerFactory } from './logging';
Expand All @@ -39,7 +39,7 @@ import { config as uiSettingsConfig } from './ui_settings';
import { mapToObject } from '../utils/';
import { ContextService } from './context';
import { SavedObjectsServiceSetup } from './saved_objects/saved_objects_service';
import { RequestHandlerContext } from '.';
import { RequestHandlerContext, InternalCoreSetup } from '.';

const coreId = Symbol('core');

Expand Down Expand Up @@ -97,7 +97,7 @@ export class Server {
http: httpSetup,
});

const coreSetup = {
const coreSetup: InternalCoreSetup = {
context: contextServiceSetup,
elasticsearch: elasticsearchServiceSetup,
http: httpSetup,
Expand All @@ -116,7 +116,7 @@ export class Server {
legacy: legacySetup,
});

this.registerCoreContext({ ...coreSetup, savedObjects: savedObjectsSetup });
this.registerCoreContext(coreSetup, savedObjectsSetup);

return coreSetup;
}
Expand Down Expand Up @@ -158,27 +158,31 @@ export class Server {
);
}

private registerCoreContext(coreSetup: {
http: InternalHttpServiceSetup;
elasticsearch: ElasticsearchServiceSetup;
savedObjects: SavedObjectsServiceSetup;
}) {
private registerCoreContext(
coreSetup: InternalCoreSetup,
savedObjects: SavedObjectsServiceSetup
) {
coreSetup.http.registerRouteHandlerContext(
coreId,
'core',
async (context, req): Promise<RequestHandlerContext['core']> => {
const adminClient = await coreSetup.elasticsearch.adminClient$.pipe(take(1)).toPromise();
const dataClient = await coreSetup.elasticsearch.dataClient$.pipe(take(1)).toPromise();
const savedObjectsClient = savedObjects.clientProvider.getClient(req);

return {
savedObjects: {
// Note: the client provider doesn't support new ES clients
// emitted from adminClient$
client: coreSetup.savedObjects.clientProvider.getClient(req),
client: savedObjectsClient,
},
elasticsearch: {
adminClient: adminClient.asScoped(req),
dataClient: dataClient.asScoped(req),
},
uiSettings: {
client: coreSetup.uiSettings.asScopedToClient(savedObjectsClient),
Copy link
Contributor Author

@mshustov mshustov Oct 16, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: There are several reasons to pass savedObjectsClient here:

  • Consistency. All context services use the same version of the client.
  • There is no way for uiSettings to declare a dependency on the saved object service at the moment. The current dependency graph is uiSettings -- used by --> legacy -- used by --> savedObjects.
    We can work around it exposing register via start contract as well, but it's not something we want to support in the long term.

},
};
}
);
Expand Down
13 changes: 6 additions & 7 deletions src/core/server/ui_settings/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,15 @@
* under the License.
*/

export {
IUiSettingsClient,
UiSettingsClient,
UiSettingsServiceOptions,
} from './ui_settings_client';
export { UiSettingsClient, UiSettingsServiceOptions } from './ui_settings_client';

export { config } from './ui_settings_config';
export { UiSettingsService } from './ui_settings_service';

export {
UiSettingsServiceSetup,
IUiSettingsClient,
UiSettingsParams,
UiSettingsService,
InternalUiSettingsServiceSetup,
UiSettingsType,
} from './ui_settings_service';
} from './types';
61 changes: 61 additions & 0 deletions src/core/server/ui_settings/routes/delete.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* 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 { schema } from '@kbn/config-schema';

import { IRouter } from '../../http';
import { SavedObjectsErrorHelpers } from '../../saved_objects';
import { CannotOverrideError } from '../ui_settings_errors';

const validate = {
params: schema.object({
key: schema.string(),
}),
};

export function registerDeleteRoute(router: IRouter) {
router.delete(
{ path: '/api/kibana/settings/{key}', validate },
async (context, request, response) => {
try {
const uiSettingsClient = context.core.uiSettings.client;

await uiSettingsClient.remove(request.params.key);

return response.ok({
body: {
settings: await uiSettingsClient.getUserProvided(),
},
});
} catch (error) {
if (SavedObjectsErrorHelpers.isSavedObjectsClientError(error)) {
return response.customError({
body: error,
statusCode: error.output.statusCode,
});
}

if (error instanceof CannotOverrideError) {
return response.badRequest({ body: error });
}

throw error;
}
}
);
}
Loading