From c4ad56adbfc67278a2dc1425a878d4652eb0bd3b Mon Sep 17 00:00:00 2001 From: Yulong Ruan Date: Fri, 12 Apr 2024 16:45:52 +0800 Subject: [PATCH 1/6] feat: workspace level ui settings When getting ui settings within a workspace, it will combine the workspace ui settings with the global ui settings and workspace ui settings have higher priority if the same setting was defined in both places When updating ui settings within a workspace, it will update the workspace ui settings, the global ui settings will remain unchanged. Signed-off-by: Yulong Ruan --- src/core/types/workspace.ts | 1 + src/plugins/workspace/common/constants.ts | 10 +- src/plugins/workspace/server/plugin.ts | 14 ++ .../server/saved_objects/workspace.ts | 4 + ...rkspace_ui_settings_client_wrapper.test.ts | 134 ++++++++++++++++++ .../workspace_ui_settings_client_wrapper.ts | 122 ++++++++++++++++ 6 files changed, 281 insertions(+), 4 deletions(-) create mode 100644 src/plugins/workspace/server/saved_objects/workspace_ui_settings_client_wrapper.test.ts create mode 100644 src/plugins/workspace/server/saved_objects/workspace_ui_settings_client_wrapper.ts diff --git a/src/core/types/workspace.ts b/src/core/types/workspace.ts index 7cdc3f92382b..dd174ecf0e10 100644 --- a/src/core/types/workspace.ts +++ b/src/core/types/workspace.ts @@ -22,4 +22,5 @@ export interface WorkspaceAttributeWithPermission extends WorkspaceAttribute { export interface WorkspaceObject extends WorkspaceAttributeWithPermission { readonly?: boolean; + uiSettings?: Record; } diff --git a/src/plugins/workspace/common/constants.ts b/src/plugins/workspace/common/constants.ts index 9e749e275828..e5d1cd03a166 100644 --- a/src/plugins/workspace/common/constants.ts +++ b/src/plugins/workspace/common/constants.ts @@ -13,6 +13,7 @@ export const WORKSPACE_FATAL_ERROR_APP_ID = 'workspace_fatal_error'; export const WORKSPACE_SAVED_OBJECTS_CLIENT_WRAPPER_ID = 'workspace'; export const WORKSPACE_CONFLICT_CONTROL_SAVED_OBJECTS_CLIENT_WRAPPER_ID = 'workspace_conflict_control'; +export const WORKSPACE_UI_SETTINGS_CLIENT_WRAPPER_ID = 'workspace_ui_settings'; export enum WorkspacePermissionMode { Read = 'read', @@ -25,10 +26,11 @@ export const WORKSPACE_ID_CONSUMER_WRAPPER_ID = 'workspace_id_consumer'; /** * The priority for these wrappers matters: - * 1. WORKSPACE_ID_CONSUMER should be placed before the other two wrappers(smaller than the other two wrappers) as it cost little - * and will append the essential workspaces field into the options, which will be honored by permission control wrapper and conflict wrapper. + * 1. WORKSPACE_ID_CONSUMER wrapper should be the first wrapper to execute, as it will add the `workspaces` field + * to `options` based on the request, which will be honored by permission control wrapper and conflict wrapper. * 2. The order of permission wrapper and conflict wrapper does not matter as no dependency between these two wrappers. */ -export const PRIORITY_FOR_WORKSPACE_ID_CONSUMER_WRAPPER = -2; -export const PRIORITY_FOR_PERMISSION_CONTROL_WRAPPER = 0; +export const PRIORITY_FOR_WORKSPACE_ID_CONSUMER_WRAPPER = -3; +export const PRIORITY_FOR_WORKSPACE_UI_SETTINGS_WRAPPER = -2; export const PRIORITY_FOR_WORKSPACE_CONFLICT_CONTROL_WRAPPER = -1; +export const PRIORITY_FOR_PERMISSION_CONTROL_WRAPPER = 0; diff --git a/src/plugins/workspace/server/plugin.ts b/src/plugins/workspace/server/plugin.ts index a1f8f7cc3ece..3b045c732e0f 100644 --- a/src/plugins/workspace/server/plugin.ts +++ b/src/plugins/workspace/server/plugin.ts @@ -20,6 +20,8 @@ import { PRIORITY_FOR_WORKSPACE_CONFLICT_CONTROL_WRAPPER, PRIORITY_FOR_WORKSPACE_ID_CONSUMER_WRAPPER, PRIORITY_FOR_PERMISSION_CONTROL_WRAPPER, + WORKSPACE_UI_SETTINGS_CLIENT_WRAPPER_ID, + PRIORITY_FOR_WORKSPACE_UI_SETTINGS_WRAPPER, } from '../common/constants'; import { IWorkspaceClientImpl, @@ -46,6 +48,7 @@ import { updateDashboardAdminStateForRequest, } from './utils'; import { WorkspaceIdConsumerWrapper } from './saved_objects/workspace_id_consumer_wrapper'; +import { WorkspaceUiSettingsClientWrapper } from './saved_objects/workspace_ui_settings_client_wrapper'; export class WorkspacePlugin implements Plugin { private readonly logger: Logger; @@ -54,6 +57,7 @@ export class WorkspacePlugin implements Plugin; private workspaceSavedObjectsClientWrapper?: WorkspaceSavedObjectsClientWrapper; + private workspaceUiSettingsClientWrapper?: WorkspaceUiSettingsClientWrapper; private proxyWorkspaceTrafficToRealHandler(setupDeps: CoreSetup) { /** @@ -131,6 +135,7 @@ export class WorkspacePlugin implements Plugin { + const createWrappedClient = () => { + const clientMock = savedObjectsClientMock.create(); + const getClientMock = jest.fn().mockReturnValue(clientMock); + const requestHandlerContext = coreMock.createRequestHandlerContext(); + const requestMock = httpServerMock.createOpenSearchDashboardsRequest(); + + clientMock.get.mockImplementation(async (type, id) => { + if (type === 'config') { + return Promise.resolve({ + id, + references: [], + type: 'config', + attributes: { + defaultIndex: 'default-index-global', + }, + }); + } else if (type === WORKSPACE_TYPE) { + return Promise.resolve({ + id, + references: [], + type: WORKSPACE_TYPE, + attributes: { + uiSettings: { + defaultIndex: 'default-index-workspace', + }, + }, + }); + } + return Promise.reject(); + }); + + const wrapper = new WorkspaceUiSettingsClientWrapper(); + wrapper.setScopedClient(getClientMock); + + return { + wrappedClient: wrapper.wrapperFactory({ + client: clientMock, + request: requestMock, + typeRegistry: requestHandlerContext.savedObjects.typeRegistry, + }), + clientMock, + }; + }; + + it('should return workspace ui settings if in a workspace', async () => { + // Currently in a workspace + jest.spyOn(utils, 'getWorkspaceState').mockReturnValue({ requestWorkspaceId: 'workspace-id' }); + + const { wrappedClient } = createWrappedClient(); + + const result = await wrappedClient.get('config', '3.0.0'); + expect(result).toEqual({ + id: '3.0.0', + references: [], + type: 'config', + attributes: { + defaultIndex: 'default-index-workspace', + }, + }); + }); + + it('should return global ui settings if NOT in a workspace', async () => { + // Currently NOT in a workspace + jest.spyOn(utils, 'getWorkspaceState').mockReturnValue({}); + + const { wrappedClient } = createWrappedClient(); + + const result = await wrappedClient.get('config', '3.0.0'); + expect(result).toEqual({ + id: '3.0.0', + references: [], + type: 'config', + attributes: { + defaultIndex: 'default-index-global', + }, + }); + }); + + it('should update workspace ui settings', async () => { + // Currently in a workspace + jest.spyOn(utils, 'getWorkspaceState').mockReturnValue({ requestWorkspaceId: 'workspace-id' }); + + const { wrappedClient, clientMock } = createWrappedClient(); + + clientMock.update.mockResolvedValue({ + id: 'workspace-id', + references: [], + type: WORKSPACE_TYPE, + attributes: { + uiSettings: { + defaultIndex: 'new-index-id', + }, + }, + }); + + await wrappedClient.update('config', '3.0.0', { defaultIndex: 'new-index-id' }); + + expect(clientMock.update).toHaveBeenCalledWith( + WORKSPACE_TYPE, + 'workspace-id', + { + uiSettings: { defaultIndex: 'new-index-id' }, + }, + {} + ); + }); + + it('should update global ui settings', async () => { + // Currently NOT in a workspace + jest.spyOn(utils, 'getWorkspaceState').mockReturnValue({}); + + const { wrappedClient, clientMock } = createWrappedClient(); + + await wrappedClient.update('config', '3.0.0', { defaultIndex: 'new-index-id' }); + + expect(clientMock.update).toHaveBeenCalledWith( + 'config', + '3.0.0', + { + defaultIndex: 'new-index-id', + }, + {} + ); + }); +}); diff --git a/src/plugins/workspace/server/saved_objects/workspace_ui_settings_client_wrapper.ts b/src/plugins/workspace/server/saved_objects/workspace_ui_settings_client_wrapper.ts new file mode 100644 index 000000000000..8c8dd01b499b --- /dev/null +++ b/src/plugins/workspace/server/saved_objects/workspace_ui_settings_client_wrapper.ts @@ -0,0 +1,122 @@ +import { getWorkspaceState } from '../../../../core/server/utils'; +import { + SavedObject, + SavedObjectsBaseOptions, + SavedObjectsClientWrapperFactory, + SavedObjectsUpdateOptions, + SavedObjectsUpdateResponse, + SavedObjectsServiceStart, + WORKSPACE_TYPE, + WorkspaceAttribute, + OpenSearchDashboardsRequest, + SavedObjectsClientContract, +} from '../../../../core/server'; +import { WORKSPACE_UI_SETTINGS_CLIENT_WRAPPER_ID } from '../../common/constants'; + +/** + * This saved object client wrapper offers methods to get and update UI settings considering + * the context of the current workspace. + */ +export class WorkspaceUiSettingsClientWrapper { + private getScopedClient?: SavedObjectsServiceStart['getScopedClient']; + + /** + * WORKSPACE_TYPE is a hidden type, regular saved object client won't return hidden types. + * To access workspace uiSettings which is defined as a property of workspace object, the + * WORKSPACE_TYPE needs to be excluded. + */ + private getWorkspaceTypeEnabledClient(request: OpenSearchDashboardsRequest) { + return this.getScopedClient?.(request, { + includedHiddenTypes: [WORKSPACE_TYPE], + excludedWrappers: [WORKSPACE_UI_SETTINGS_CLIENT_WRAPPER_ID], + }) as SavedObjectsClientContract; + } + + public setScopedClient(getScopedClient: SavedObjectsServiceStart['getScopedClient']) { + this.getScopedClient = getScopedClient; + } + + public wrapperFactory: SavedObjectsClientWrapperFactory = (wrapperOptions) => { + const getUiSettingsWithWorkspace = async ( + type: string, + id: string, + options: SavedObjectsBaseOptions = {} + ): Promise> => { + const { requestWorkspaceId } = getWorkspaceState(wrapperOptions.request); + + /** + * When getting ui settings within a workspace, it will combine the workspace ui settings with + * the global ui settings and workspace ui settings have higher priority if the same setting + * was defined in both places + */ + if (type === 'config' && requestWorkspaceId) { + const configObject = await wrapperOptions.client.get>( + 'config', + id, + options + ); + + const workspaceObject = await this.getWorkspaceTypeEnabledClient( + wrapperOptions.request + ).get(WORKSPACE_TYPE, requestWorkspaceId); + + configObject.attributes = { + ...configObject.attributes, + ...workspaceObject.attributes.uiSettings, + }; + + return configObject as SavedObject; + } + + return wrapperOptions.client.get(type, id, options); + }; + + const updateUiSettingsWithWorkspace = async ( + type: string, + id: string, + attributes: Partial, + options: SavedObjectsUpdateOptions = {} + ): Promise> => { + const { requestWorkspaceId } = getWorkspaceState(wrapperOptions.request); + + /** + * When updating ui settings within a workspace, it will update the workspace ui settings, + * the global ui settings will remain unchanged. + */ + if (type === 'config' && requestWorkspaceId) { + const configObject = await wrapperOptions.client.get>( + 'config', + id, + options + ); + const savedObjectsClient = this.getWorkspaceTypeEnabledClient(wrapperOptions.request); + + const workspaceObject = await savedObjectsClient.get( + WORKSPACE_TYPE, + requestWorkspaceId + ); + + const workspaceUpdateResult = await savedObjectsClient.update( + WORKSPACE_TYPE, + requestWorkspaceId, + { + ...workspaceObject.attributes, + uiSettings: { ...workspaceObject.attributes.uiSettings, ...attributes }, + }, + options + ); + + configObject.attributes = { ...workspaceUpdateResult.attributes.uiSettings }; + + return configObject as SavedObjectsUpdateResponse; + } + return wrapperOptions.client.update(type, id, attributes, options); + }; + + return { + ...wrapperOptions.client, + get: getUiSettingsWithWorkspace, + update: updateUiSettingsWithWorkspace, + }; + }; +} From f0d2cb6544d8db8254a8610ffeb4ab5c4212a9ea Mon Sep 17 00:00:00 2001 From: Yulong Ruan Date: Fri, 12 Apr 2024 16:58:14 +0800 Subject: [PATCH 2/6] fix WorkspaceAttribute type Signed-off-by: Yulong Ruan --- src/core/types/workspace.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/types/workspace.ts b/src/core/types/workspace.ts index dd174ecf0e10..fd1cab5b63e7 100644 --- a/src/core/types/workspace.ts +++ b/src/core/types/workspace.ts @@ -13,6 +13,7 @@ export interface WorkspaceAttribute { color?: string; icon?: string; reserved?: boolean; + uiSettings?: Record; defaultVISTheme?: string; } @@ -22,5 +23,4 @@ export interface WorkspaceAttributeWithPermission extends WorkspaceAttribute { export interface WorkspaceObject extends WorkspaceAttributeWithPermission { readonly?: boolean; - uiSettings?: Record; } From cf49d79a787ddad054cc0b54e9b3e75a82adabe8 Mon Sep 17 00:00:00 2001 From: Yulong Ruan Date: Mon, 15 Apr 2024 15:59:57 +0800 Subject: [PATCH 3/6] update based on PR comments Signed-off-by: Yulong Ruan --- .../workspace_ui_settings_client_wrapper.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/plugins/workspace/server/saved_objects/workspace_ui_settings_client_wrapper.ts b/src/plugins/workspace/server/saved_objects/workspace_ui_settings_client_wrapper.ts index 8c8dd01b499b..cb947df55fdd 100644 --- a/src/plugins/workspace/server/saved_objects/workspace_ui_settings_client_wrapper.ts +++ b/src/plugins/workspace/server/saved_objects/workspace_ui_settings_client_wrapper.ts @@ -115,6 +115,17 @@ export class WorkspaceUiSettingsClientWrapper { return { ...wrapperOptions.client, + checkConflicts: wrapperOptions.client.checkConflicts, + errors: wrapperOptions.client.errors, + addToNamespaces: wrapperOptions.client.addToNamespaces, + deleteFromNamespaces: wrapperOptions.client.deleteFromNamespaces, + find: wrapperOptions.client.find, + bulkGet: wrapperOptions.client.bulkGet, + create: wrapperOptions.client.create, + bulkCreate: wrapperOptions.client.bulkCreate, + delete: wrapperOptions.client.delete, + bulkUpdate: wrapperOptions.client.bulkUpdate, + deleteByWorkspace: wrapperOptions.client.deleteByWorkspace, get: getUiSettingsWithWorkspace, update: updateUiSettingsWithWorkspace, }; From 8ee9467073c6ba04202aaa9ed7b0900d8175f36f Mon Sep 17 00:00:00 2001 From: Yulong Ruan Date: Tue, 16 Apr 2024 15:39:21 +0800 Subject: [PATCH 4/6] add integration tests for workspace ui settings wrapper Signed-off-by: Yulong Ruan --- .../workspace_ui_settings_wrapper.test.ts | 88 +++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 src/plugins/workspace/server/saved_objects/integration_tests/workspace_ui_settings_wrapper.test.ts diff --git a/src/plugins/workspace/server/saved_objects/integration_tests/workspace_ui_settings_wrapper.test.ts b/src/plugins/workspace/server/saved_objects/integration_tests/workspace_ui_settings_wrapper.test.ts new file mode 100644 index 000000000000..bf459c31806f --- /dev/null +++ b/src/plugins/workspace/server/saved_objects/integration_tests/workspace_ui_settings_wrapper.test.ts @@ -0,0 +1,88 @@ +import { IUiSettingsClient, WorkspaceAttribute } from 'src/core/server'; + +import * as osdTestServer from '../../../../../core/test_helpers/osd_server'; +import { httpServerMock } from '../../../../../core/server/http/http_server.mocks'; + +describe('workspace ui settings saved object client wrapper', () => { + let opensearchServer: osdTestServer.TestOpenSearchUtils; + let osd: osdTestServer.TestOpenSearchDashboardsUtils; + let globalUiSettingsClient: IUiSettingsClient; + let testWorkspace: WorkspaceAttribute = { + id: '', + name: '', + }; + + beforeAll(async () => { + const servers = osdTestServer.createTestServers({ + adjustTimeout: (t: number) => jest.setTimeout(t), + settings: { + osd: { + workspace: { + enabled: true, + }, + savedObjects: { + permission: { + enabled: true, + }, + }, + migrations: { + skip: false, + }, + }, + }, + }); + opensearchServer = await servers.startOpenSearch(); + osd = await servers.startOpenSearchDashboards(); + + const savedObjectsClient = osd.coreStart.savedObjects.getScopedClient( + httpServerMock.createOpenSearchDashboardsRequest() + ); + globalUiSettingsClient = osd.coreStart.uiSettings.asScopedToClient(savedObjectsClient); + + const res = await osdTestServer.request.post(osd.root, '/api/workspaces').send({ + attributes: { name: 'test workspace' }, + }); + testWorkspace = res.body.result; + }, 30000); + + afterAll(async () => { + await opensearchServer.stop(); + await osd.stop(); + }, 30000); + + beforeEach(async () => { + await globalUiSettingsClient.set('defaultIndex', 'global-index'); + }); + + it('should get and update workspace ui settings when currently in a workspace', async () => { + const workspaceScopedSavedObjectsClient = osd.coreStart.savedObjects.getScopedClient( + httpServerMock.createOpenSearchDashboardsRequest({ + opensearchDashboardsRequestState: { requestWorkspaceId: testWorkspace.id }, + }) + ); + const workspaceScopedUiSettingsClient = osd.coreStart.uiSettings.asScopedToClient( + workspaceScopedSavedObjectsClient + ); + + expect(await globalUiSettingsClient.get('defaultIndex')).toBe('global-index'); + + // workspace defaultIndex is not set, it will use the global value + expect(await workspaceScopedUiSettingsClient.get('defaultIndex')).toBe('global-index'); + + // update ui settings in a workspace + await workspaceScopedUiSettingsClient.set('defaultIndex', 'workspace-index'); + + // global ui settings remain unchanged + expect(await globalUiSettingsClient.get('defaultIndex')).toBe('global-index'); + + // workspace ui settings updated to the new value + expect(await workspaceScopedUiSettingsClient.get('defaultIndex')).toBe('workspace-index'); + }); + + it('should get and update global ui settings when currently not in a workspace', async () => { + expect(await globalUiSettingsClient.get('defaultIndex')).toBe('global-index'); + + await globalUiSettingsClient.set('defaultIndex', 'global-index-new'); + expect(await globalUiSettingsClient.get('defaultIndex')).toBe('global-index-new'); + }); +}); From 1dfd7cc42e06c29e1c6ff34d64aafd3b0fde5a2b Mon Sep 17 00:00:00 2001 From: Yulong Ruan Date: Tue, 16 Apr 2024 17:26:19 +0800 Subject: [PATCH 5/6] Update src/plugins/workspace/server/saved_objects/workspace_ui_settings_client_wrapper.ts Co-authored-by: SuZhou-Joe --- .../saved_objects/workspace_ui_settings_client_wrapper.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/plugins/workspace/server/saved_objects/workspace_ui_settings_client_wrapper.ts b/src/plugins/workspace/server/saved_objects/workspace_ui_settings_client_wrapper.ts index cb947df55fdd..e57fc2e73649 100644 --- a/src/plugins/workspace/server/saved_objects/workspace_ui_settings_client_wrapper.ts +++ b/src/plugins/workspace/server/saved_objects/workspace_ui_settings_client_wrapper.ts @@ -106,7 +106,7 @@ export class WorkspaceUiSettingsClientWrapper { options ); - configObject.attributes = { ...workspaceUpdateResult.attributes.uiSettings }; + configObject.attributes = workspaceUpdateResult.attributes.uiSettings; return configObject as SavedObjectsUpdateResponse; } From 0e49d7efb479c2695c32fd138555ca511ab7bfbb Mon Sep 17 00:00:00 2001 From: Yulong Ruan Date: Tue, 16 Apr 2024 23:20:47 +0800 Subject: [PATCH 6/6] fix failed tests Signed-off-by: Yulong Ruan --- src/plugins/workspace/server/plugin.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/plugins/workspace/server/plugin.test.ts b/src/plugins/workspace/server/plugin.test.ts index f3f13fa27513..bb23cb335ce1 100644 --- a/src/plugins/workspace/server/plugin.test.ts +++ b/src/plugins/workspace/server/plugin.test.ts @@ -34,7 +34,7 @@ describe('Workspace server plugin', () => { }, } `); - expect(setupMock.savedObjects.addClientWrapper).toBeCalledTimes(3); + expect(setupMock.savedObjects.addClientWrapper).toBeCalledTimes(4); }); it('#proxyWorkspaceTrafficToRealHandler', async () => {