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

[Workspace] introducing workspace level ui settings #6510

Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
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
1 change: 1 addition & 0 deletions src/core/types/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export interface WorkspaceAttribute {
color?: string;
icon?: string;
reserved?: boolean;
uiSettings?: Record<string, any>;
}

export interface WorkspaceAttributeWithPermission extends WorkspaceAttribute {
Expand Down
10 changes: 6 additions & 4 deletions src/plugins/workspace/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export const DEFAULT_SELECTED_FEATURES_IDS = [WORKSPACE_UPDATE_APP_ID, WORKSPACE
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',
Expand All @@ -28,10 +29,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;
2 changes: 1 addition & 1 deletion src/plugins/workspace/server/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('Workspace server plugin', () => {
},
}
`);
expect(setupMock.savedObjects.addClientWrapper).toBeCalledTimes(3);
expect(setupMock.savedObjects.addClientWrapper).toBeCalledTimes(4);
});

it('#proxyWorkspaceTrafficToRealHandler', async () => {
Expand Down
13 changes: 13 additions & 0 deletions src/plugins/workspace/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, WorkspacePluginSetup, WorkspacePluginStart } from './types';
import { WorkspaceClient } from './workspace_client';
Expand All @@ -36,6 +38,7 @@ import {
SavedObjectsPermissionControlContract,
} from './permission_control/client';
import { WorkspaceIdConsumerWrapper } from './saved_objects/workspace_id_consumer_wrapper';
import { WorkspaceUiSettingsClientWrapper } from './saved_objects/workspace_ui_settings_client_wrapper';

export class WorkspacePlugin implements Plugin<WorkspacePluginSetup, WorkspacePluginStart> {
private readonly logger: Logger;
Expand All @@ -44,6 +47,7 @@ export class WorkspacePlugin implements Plugin<WorkspacePluginSetup, WorkspacePl
private permissionControl?: SavedObjectsPermissionControlContract;
private readonly globalConfig$: Observable<SharedGlobalConfig>;
private workspaceSavedObjectsClientWrapper?: WorkspaceSavedObjectsClientWrapper;
private workspaceUiSettingsClientWrapper?: WorkspaceUiSettingsClientWrapper;

private proxyWorkspaceTrafficToRealHandler(setupDeps: CoreSetup) {
/**
Expand Down Expand Up @@ -90,6 +94,14 @@ export class WorkspacePlugin implements Plugin<WorkspacePluginSetup, WorkspacePl
);
this.proxyWorkspaceTrafficToRealHandler(core);

const workspaceUiSettingsClientWrapper = new WorkspaceUiSettingsClientWrapper();
this.workspaceUiSettingsClientWrapper = workspaceUiSettingsClientWrapper;
core.savedObjects.addClientWrapper(
PRIORITY_FOR_WORKSPACE_UI_SETTINGS_WRAPPER,
WORKSPACE_UI_SETTINGS_CLIENT_WRAPPER_ID,
workspaceUiSettingsClientWrapper.wrapperFactory
);

core.savedObjects.addClientWrapper(
PRIORITY_FOR_WORKSPACE_ID_CONSUMER_WRAPPER,
WORKSPACE_ID_CONSUMER_WRAPPER_ID,
Expand Down Expand Up @@ -139,6 +151,7 @@ export class WorkspacePlugin implements Plugin<WorkspacePluginSetup, WorkspacePl
this.client?.setSavedObjects(core.savedObjects);
this.workspaceConflictControl?.setSerializer(core.savedObjects.createSerializer());
this.workspaceSavedObjectsClientWrapper?.setScopedClient(core.savedObjects.getScopedClient);
this.workspaceUiSettingsClientWrapper?.setScopedClient(core.savedObjects.getScopedClient);

return {
client: this.client as IWorkspaceClientImpl,
Expand Down
Copy link
Contributor

Choose a reason for hiding this comment

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

could you please resolve the license header issue as it suggested

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks, updated ;)

Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

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';

Check failure on line 9 in src/plugins/workspace/server/saved_objects/integration_tests/workspace_ui_settings_wrapper.test.ts

View workflow job for this annotation

GitHub Actions / Build and Verify on Linux (ciGroup1)

Unexpected path "../../../../../core/server/http/http_server.mocks" imported in restricted zone. Plugins may only import from top-level public and server modules in core

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');
});
});
4 changes: 4 additions & 0 deletions src/plugins/workspace/server/saved_objects/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ export const workspace: SavedObjectsType = {
reserved: {
type: 'boolean',
},
uiSettings: {
dynamic: false,
properties: {},
},
},
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import { httpServerMock, savedObjectsClientMock, coreMock } from '../../../../core/server/mocks';
import { WorkspaceUiSettingsClientWrapper } from './workspace_ui_settings_client_wrapper';
import { WORKSPACE_TYPE } from '../../../../core/server';

import * as utils from '../../../../core/server/utils';

jest.mock('../../../../core/server/utils');

describe('WorkspaceUiSettingsClientWrapper', () => {
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',
},
{}
);
});
});
Loading
Loading