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]Move set default data source before workspace update #7636

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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/7636.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
fix:
- [Workspace] Move set default source order to avoid dev server crash ([#7636](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/7636))
18 changes: 18 additions & 0 deletions src/plugins/workspace/server/workspace_client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,24 @@ describe('#WorkspaceClient', () => {
expect(mockCheckAndSetDefaultDataSource).toHaveBeenCalledWith(uiSettingsClient, ['id1'], true);
});

it('update# should log error when failed set default data source', async () => {
const client = new WorkspaceClient(coreSetup, logger);
await client.setup(coreSetup);
client?.setSavedObjects(savedObjects);
client?.setUiSettings(uiSettings);
mockCheckAndSetDefaultDataSource.mockRejectedValueOnce(new Error());

await client.update(mockRequestDetail, mockWorkspaceId, {
name: mockWorkspaceName,
permissions: {},
dataSources: ['id1'],
});

expect(logger.error).toHaveBeenLastCalledWith(
'Set default data source error during workspace updating'
);
});

it('delete# should unassign data source before deleting related saved objects', async () => {
const client = new WorkspaceClient(coreSetup, logger);
await client.setup(coreSetup);
Expand Down
18 changes: 12 additions & 6 deletions src/plugins/workspace/server/workspace_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ export class WorkspaceClient implements IWorkspaceClientImpl {
const { permissions, dataSources: newDataSources, ...attributes } = payload;
try {
const client = this.getSavedObjectClientsFromRequestDetail(requestDetail);
const workspaceInDB: SavedObject<WorkspaceAttribute> = await client.get(WORKSPACE_TYPE, id);
let workspaceInDB: SavedObject<WorkspaceAttribute> = await client.get(WORKSPACE_TYPE, id);
if (workspaceInDB.attributes.name !== attributes.name) {
const existingWorkspaceRes = await this.getScopedClientWithoutPermission(
requestDetail
Expand Down Expand Up @@ -265,6 +265,17 @@ export class WorkspaceClient implements IWorkspaceClientImpl {
}
}

if (newDataSources && this.uiSettings && client) {
const uiSettingsClient = this.uiSettings.asScopedToClient(client);
try {
await checkAndSetDefaultDataSource(uiSettingsClient, newDataSources, true);
// Doc version may changed after default data source updated.
workspaceInDB = await client.get(WORKSPACE_TYPE, id);
} catch (error) {
this.logger.error('Set default data source error during workspace updating');
}
}

await client.create<Omit<WorkspaceAttribute, 'id'>>(
WORKSPACE_TYPE,
{ ...workspaceInDB.attributes, ...attributes },
Expand All @@ -276,11 +287,6 @@ export class WorkspaceClient implements IWorkspaceClientImpl {
}
);

if (newDataSources && this.uiSettings && client) {
const uiSettingsClient = this.uiSettings.asScopedToClient(client);
checkAndSetDefaultDataSource(uiSettingsClient, newDataSources, true);
}

return {
success: true,
result: true,
Expand Down
Loading