Skip to content

Commit

Permalink
[Workspace]Move set default data source before workspace update (#7636)
Browse files Browse the repository at this point in the history
* Move set default data source before workspace update

Signed-off-by: Lin Wang <wonglam@amazon.com>

* Changeset file for PR #7636 created/updated

* Add comment for move set default data source before workspace update

Signed-off-by: Lin Wang <wonglam@amazon.com>

---------

Signed-off-by: Lin Wang <wonglam@amazon.com>
Co-authored-by: opensearch-changeset-bot[bot] <154024398+opensearch-changeset-bot[bot]@users.noreply.github.com>
(cherry picked from commit 761b2ff)
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
1 parent d7f3765 commit 8541642
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 6 deletions.
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
23 changes: 17 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,22 @@ export class WorkspaceClient implements IWorkspaceClientImpl {
}
}

/**
* When the workspace owner unassign themselves, ensure the default data source is set before
* updating the workspace permissions. This prevents a lack of write permission on saved objects
* after the user is removed from the workspace.
**/
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 +292,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

0 comments on commit 8541642

Please sign in to comment.