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

[Backport 2.x] [Workspace] Fix: optimization on handling invalid workspace id in workspace_ui_settings wrapper #6672

Merged
merged 1 commit into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions changelogs/fragments/6669.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
fix:
- Optimization on handling invalid workspace id in workspace_ui_settings wrapper ([#6669](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6669))
2 changes: 1 addition & 1 deletion src/plugins/workspace/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export class WorkspacePlugin implements Plugin<WorkspacePluginSetup, WorkspacePl
);
this.proxyWorkspaceTrafficToRealHandler(core);

const workspaceUiSettingsClientWrapper = new WorkspaceUiSettingsClientWrapper();
const workspaceUiSettingsClientWrapper = new WorkspaceUiSettingsClientWrapper(this.logger);
this.workspaceUiSettingsClientWrapper = workspaceUiSettingsClientWrapper;
core.savedObjects.addClientWrapper(
PRIORITY_FOR_WORKSPACE_UI_SETTINGS_WRAPPER,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

import { loggerMock } from '@osd/logging/target/mocks';
import { httpServerMock, savedObjectsClientMock, coreMock } from '../../../../core/server/mocks';
import { WorkspaceUiSettingsClientWrapper } from './workspace_ui_settings_client_wrapper';
import { WORKSPACE_TYPE } from '../../../../core/server';
Expand All @@ -17,6 +18,7 @@ describe('WorkspaceUiSettingsClientWrapper', () => {
const getClientMock = jest.fn().mockReturnValue(clientMock);
const requestHandlerContext = coreMock.createRequestHandlerContext();
const requestMock = httpServerMock.createOpenSearchDashboardsRequest();
const logger = loggerMock.create();

clientMock.get.mockImplementation(async (type, id) => {
if (type === 'config') {
Expand All @@ -43,7 +45,7 @@ describe('WorkspaceUiSettingsClientWrapper', () => {
return Promise.reject();
});

const wrapper = new WorkspaceUiSettingsClientWrapper();
const wrapper = new WorkspaceUiSettingsClientWrapper(logger);
wrapper.setScopedClient(getClientMock);

return {
Expand All @@ -53,6 +55,7 @@ describe('WorkspaceUiSettingsClientWrapper', () => {
typeRegistry: requestHandlerContext.savedObjects.typeRegistry,
}),
clientMock,
logger,
};
};

Expand Down Expand Up @@ -136,4 +139,40 @@ describe('WorkspaceUiSettingsClientWrapper', () => {
{}
);
});

it('should not throw error if the workspace id is not valid', async () => {
const invalidWorkspaceId = 'invalid-workspace-id';
// Currently in a workspace
jest
.spyOn(utils, 'getWorkspaceState')
.mockReturnValue({ requestWorkspaceId: invalidWorkspaceId });

const { wrappedClient, clientMock, logger } = createWrappedClient();
clientMock.get.mockImplementation(async (type, id) => {
if (type === 'config') {
return Promise.resolve({
id,
references: [],
type: 'config',
attributes: {
defaultIndex: 'default-index-global',
},
});
}
return Promise.reject('not found');
});

const config = await wrappedClient.get('config', '3.0.0');
expect(config).toEqual({
attributes: {
defaultIndex: 'default-index-global',
},
id: '3.0.0',
references: [],
type: 'config',
});
expect(logger.error).toBeCalledWith(
`Unable to get workspaceObject with id: ${invalidWorkspaceId}`
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ import {
SavedObjectsClientContract,
} from '../../../../core/server';
import { WORKSPACE_UI_SETTINGS_CLIENT_WRAPPER_ID } from '../../common/constants';
import { Logger } from '../../../../core/server';

/**
* This saved object client wrapper offers methods to get and update UI settings considering
* the context of the current workspace.
*/
export class WorkspaceUiSettingsClientWrapper {
constructor(private readonly logger: Logger) {}
private getScopedClient?: SavedObjectsServiceStart['getScopedClient'];

/**
Expand Down Expand Up @@ -61,13 +63,19 @@ export class WorkspaceUiSettingsClientWrapper {
options
);

const workspaceObject = await this.getWorkspaceTypeEnabledClient(
wrapperOptions.request
).get<WorkspaceAttribute>(WORKSPACE_TYPE, requestWorkspaceId);
let workspaceObject: SavedObject<WorkspaceAttribute> | null = null;

try {
workspaceObject = await this.getWorkspaceTypeEnabledClient(wrapperOptions.request).get<
WorkspaceAttribute
>(WORKSPACE_TYPE, requestWorkspaceId);
} catch (e) {
this.logger.error(`Unable to get workspaceObject with id: ${requestWorkspaceId}`);
}

configObject.attributes = {
...configObject.attributes,
...workspaceObject.attributes.uiSettings,
...(workspaceObject ? workspaceObject.attributes.uiSettings : {}),
};

return configObject as SavedObject<T>;
Expand Down Expand Up @@ -111,7 +119,9 @@ export class WorkspaceUiSettingsClientWrapper {
options
);

configObject.attributes = workspaceUpdateResult.attributes.uiSettings;
if (workspaceUpdateResult.attributes.uiSettings) {
configObject.attributes = workspaceUpdateResult.attributes.uiSettings;
}

return configObject as SavedObjectsUpdateResponse<T>;
}
Expand Down
Loading