Skip to content

Commit

Permalink
Merge branch 'main' into reset-preconfig
Browse files Browse the repository at this point in the history
  • Loading branch information
juliaElastic authored Jul 14, 2023
2 parents 2cfe0ab + 4105619 commit bba8a70
Show file tree
Hide file tree
Showing 41 changed files with 606 additions and 177 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ export class UiSettingsApi {

try {
this.sendInProgress = true;
const path = scope === 'namespace' ? '/api/kibana/settings' : '/api/kibana/global_settings';
const path =
scope === 'namespace' ? '/internal/kibana/settings' : '/internal/kibana/global_settings';
changes.callback(
undefined,
await this.sendRequest('POST', path, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@ export function registerRoutes(router: InternalUiSettingsRouter) {
registerSetRoute(router);
registerSetManyRoute(router);
}

export { registerInternalRoutes } from './internal';
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { schema } from '@kbn/config-schema';

import { SavedObjectsErrorHelpers } from '@kbn/core-saved-objects-server';
import { IUiSettingsClient } from '@kbn/core-ui-settings-server';
import { KibanaRequest, KibanaResponseFactory } from '@kbn/core-http-server';
import type { InternalUiSettingsRouter } from '../../internal_types';
import { CannotOverrideError } from '../../ui_settings_errors';
import { InternalUiSettingsRequestHandlerContext } from '../../internal_types';

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

export function registerInternalDeleteRoute(router: InternalUiSettingsRouter) {
const deleteFromRequest = async (
uiSettingsClient: IUiSettingsClient,
context: InternalUiSettingsRequestHandlerContext,
request: KibanaRequest<Readonly<{} & { key: string }>, unknown, unknown, 'delete'>,
response: KibanaResponseFactory
) => {
try {
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;
}
};
router.delete(
{ path: '/internal/kibana/settings/{key}', validate, options: { access: 'internal' } },
async (context, request, response) => {
const uiSettingsClient = (await context.core).uiSettings.client;
return await deleteFromRequest(uiSettingsClient, context, request, response);
}
);
router.delete(
{ path: '/internal/kibana/global_settings/{key}', validate, options: { access: 'internal' } },
async (context, request, response) => {
const uiSettingsClient = (await context.core).uiSettings.globalClient;
return await deleteFromRequest(uiSettingsClient, context, request, response);
}
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { SavedObjectsErrorHelpers } from '@kbn/core-saved-objects-server';
import { IUiSettingsClient } from '@kbn/core-ui-settings-server';
import { KibanaRequest, KibanaResponseFactory } from '@kbn/core-http-server';
import { InternalUiSettingsRequestHandlerContext } from '../../internal_types';
import type { InternalUiSettingsRouter } from '../../internal_types';

export function registerInternalGetRoute(router: InternalUiSettingsRouter) {
const getFromRequest = async (
uiSettingsClient: IUiSettingsClient,
context: InternalUiSettingsRequestHandlerContext,
request: KibanaRequest<unknown, unknown, unknown, 'get'>,
response: KibanaResponseFactory
) => {
try {
return response.ok({
body: {
settings: await uiSettingsClient.getUserProvided(),
},
});
} catch (error) {
if (SavedObjectsErrorHelpers.isSavedObjectsClientError(error)) {
return response.customError({
body: error,
statusCode: error.output.statusCode,
});
}

throw error;
}
};
router.get(
{ path: '/internal/kibana/settings', validate: false, options: { access: 'internal' } },
async (context, request, response) => {
const uiSettingsClient = (await context.core).uiSettings.client;
return await getFromRequest(uiSettingsClient, context, request, response);
}
);
router.get(
{ path: '/internal/kibana/global_settings', validate: false, options: { access: 'internal' } },
async (context, request, response) => {
const uiSettingsClient = (await context.core).uiSettings.globalClient;
return await getFromRequest(uiSettingsClient, context, request, response);
}
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import type { InternalUiSettingsRouter } from '../../internal_types';
import { registerInternalDeleteRoute } from './delete';
import { registerInternalGetRoute } from './get';
import { registerInternalSetManyRoute } from './set_many';
import { registerInternalSetRoute } from './set';

export function registerInternalRoutes(router: InternalUiSettingsRouter) {
registerInternalGetRoute(router);
registerInternalDeleteRoute(router);
registerInternalSetRoute(router);
registerInternalSetManyRoute(router);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { schema, ValidationError } from '@kbn/config-schema';
import { KibanaRequest, KibanaResponseFactory } from '@kbn/core-http-server';
import { SavedObjectsErrorHelpers } from '@kbn/core-saved-objects-server';
import { IUiSettingsClient } from '@kbn/core-ui-settings-server';
import type {
InternalUiSettingsRequestHandlerContext,
InternalUiSettingsRouter,
} from '../../internal_types';
import { CannotOverrideError } from '../../ui_settings_errors';

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

export function registerInternalSetRoute(router: InternalUiSettingsRouter) {
const setFromRequest = async (
uiSettingsClient: IUiSettingsClient,
context: InternalUiSettingsRequestHandlerContext,
request: KibanaRequest<
Readonly<{} & { key: string }>,
unknown,
Readonly<{ value?: any } & {}>,
'post'
>,
response: KibanaResponseFactory
) => {
try {
const { key } = request.params;
const { value } = request.body;

await uiSettingsClient.set(key, value);

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 || error instanceof ValidationError) {
return response.badRequest({ body: error });
}

throw error;
}
};
router.post(
{ path: '/internal/kibana/settings/{key}', validate, options: { access: 'internal' } },
async (context, request, response) => {
const uiSettingsClient = (await context.core).uiSettings.client;
return await setFromRequest(uiSettingsClient, context, request, response);
}
);
router.post(
{ path: '/internal/kibana/global_settings/{key}', validate, options: { access: 'internal' } },
async (context, request, response) => {
const uiSettingsClient = (await context.core).uiSettings.globalClient;
return await setFromRequest(uiSettingsClient, context, request, response);
}
);
}
Loading

0 comments on commit bba8a70

Please sign in to comment.