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] Unassign data source before deleteByWorkspace #7284

Merged
merged 1 commit into from
Jul 18, 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/7279.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
fix:
- Unassign data source before deleteByWorkspace ([#7279](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/7279))
16 changes: 16 additions & 0 deletions src/plugins/workspace/server/workspace_client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,4 +180,20 @@ describe('#WorkspaceClient', () => {

expect(mockCheckAndSetDefaultDataSource).toHaveBeenCalledWith(uiSettingsClient, ['id1'], true);
});

it('delete# should unassign data source before deleting related saved objects', async () => {
const client = new WorkspaceClient(coreSetup, logger);
await client.setup(coreSetup);
client?.setSavedObjects(savedObjects);
client?.setUiSettings(uiSettings);

await client.delete(mockRequestDetail, mockWorkspaceId);

expect(deleteFromWorkspaces).toHaveBeenCalledWith(DATA_SOURCE_SAVED_OBJECT_TYPE, 'id1', [
mockWorkspaceId,
]);
expect(deleteFromWorkspaces).toHaveBeenCalledWith(DATA_SOURCE_SAVED_OBJECT_TYPE, 'id2', [
mockWorkspaceId,
]);
});
});
15 changes: 15 additions & 0 deletions src/plugins/workspace/server/workspace_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,21 @@ export class WorkspaceClient implements IWorkspaceClientImpl {
}),
};
}

// When workspace is to be deleted, unassign all assigned data source before deleting saved object by workspace.
const selectedDataSources = await getDataSourcesList(savedObjectClient, [id]);
if (selectedDataSources.length > 0) {
const promises = [];
for (const dataSource of selectedDataSources) {
promises.push(
savedObjectClient.deleteFromWorkspaces(DATA_SOURCE_SAVED_OBJECT_TYPE, dataSource.id, [
id,
])
);
}
await Promise.all(promises);
}

await savedObjectClient.deleteByWorkspace(id);
// delete workspace itself at last, deleteByWorkspace depends on the workspace to do permission check
await savedObjectClient.delete(WORKSPACE_TYPE, id);
Expand Down
Loading