Skip to content

Commit

Permalink
delete saved object when deleting workspace
Browse files Browse the repository at this point in the history
Signed-off-by: Hailong Cui <ihailong@amazon.com>
  • Loading branch information
Hailong-am committed Aug 11, 2023
1 parent f1cb690 commit 110ce63
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 13 deletions.
1 change: 1 addition & 0 deletions src/core/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ export {
SavedObjectsShareObjects,
SavedObjectsAddToWorkspacesOptions,
SavedObjectsAddToWorkspacesResponse,
SavedObjectsDeleteByWorkspaceOptions,
WORKSPACE_TYPE,
Permissions,
ACL,
Expand Down
17 changes: 6 additions & 11 deletions src/core/server/saved_objects/service/lib/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ import {
SavedObjectsCheckConflictsObject,
SavedObjectsCheckConflictsResponse,
SavedObjectsCreateOptions,
SavedObjectsDeleteByWorkspaceOptions,
SavedObjectsDeleteFromNamespacesOptions,
SavedObjectsDeleteFromNamespacesResponse,
SavedObjectsDeleteFromWorkspacesOptions,
Expand All @@ -89,7 +90,7 @@ import {
FIND_DEFAULT_PER_PAGE,
SavedObjectsUtils,
} from './utils';
import { MANAGEMENT_WORKSPACE, PUBLIC_WORKSPACE } from '../../../../utils/constants';
import { PUBLIC_WORKSPACE } from '../../../../utils/constants';

// BEWARE: The SavedObjectClient depends on the implementation details of the SavedObjectsRepository
// so any breaking changes to this repository are considered breaking changes to the SavedObjectsClient.
Expand Down Expand Up @@ -131,12 +132,6 @@ export interface SavedObjectsDeleteByNamespaceOptions
refresh?: boolean;
}

export interface SavedObjectsDeleteByWorkspaceOptions
extends Omit<SavedObjectsBaseOptions, 'namespace'> {
/** The OpenSearch supports only boolean flag for this operation */
refresh?: boolean;
}

const DEFAULT_REFRESH_SETTING = 'wait_for';

/**
Expand Down Expand Up @@ -753,7 +748,7 @@ export class SavedObjectsRepository {
}

/**
* Deletes all objects from the provided namespace.
* Deletes all objects from the provided workspace.
*
* @param {string} workspace
* @param options SavedObjectsDeleteByWorkspaceOptions
Expand All @@ -763,8 +758,8 @@ export class SavedObjectsRepository {
workspace: string,
options: SavedObjectsDeleteByWorkspaceOptions = {}
): Promise<any> {
if (!workspace || workspace === PUBLIC_WORKSPACE || workspace === MANAGEMENT_WORKSPACE) {
throw new TypeError(`namespace is required, and must not be reserved workspace`);
if (!workspace || workspace === '*') {
throw new TypeError(`workspace is required, and must be a string that is not equal to '*'`);
}

const allTypes = Object.keys(getRootPropertiesObjects(this._mappings));
Expand All @@ -790,7 +785,7 @@ export class SavedObjectsRepository {
},
conflicts: 'proceed',
...getSearchDsl(this._mappings, this._registry, {
workspaces: workspace ? [workspace] : undefined,
workspaces: [workspace],
type: allTypes,
}),
},
Expand Down
17 changes: 17 additions & 0 deletions src/core/server/saved_objects/service/saved_objects_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,11 @@ export interface SavedObjectsDeleteFromWorkspacesOptions
refresh?: MutatingOperationRefreshSetting;
}

export interface SavedObjectsDeleteByWorkspaceOptions {
/** The OpenSearch supports only boolean flag for this operation */
refresh?: boolean;
}

/**
*
* @public
Expand Down Expand Up @@ -476,6 +481,18 @@ export class SavedObjectsClient {
return await this._repository.addToWorkspaces(objects, workspaces, options);
};

/**
* delete saved objects by workspace id
* @param workspace
* @param options
*/
deleteByWorkspace = async (
workspace: string,
options: SavedObjectsDeleteByWorkspaceOptions = {}
): Promise<SavedObjectsAddToWorkspacesResponse[]> => {
return await this._repository.deleteByWorkspace(workspace, options);
};

/**
* Bulk Updates multiple SavedObject at once
*
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/workspace/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ export class WorkspacePlugin implements Plugin<{}, {}, WorkspacePluginSetupDeps>
};
}

private async _changeSavedObjectCurrentWorkspace() {
private _changeSavedObjectCurrentWorkspace() {
if (this.coreStart) {
return this.coreStart.workspaces.currentWorkspaceId$.subscribe((currentWorkspaceId) => {
if (currentWorkspaceId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
WORKSPACE_TYPE,
ACL,
WorkspacePermissionMode,
SavedObjectsDeleteByWorkspaceOptions,
} from '../../../../core/server';

// Can't throw unauthorized for now, the page will be refreshed if unauthorized
Expand Down Expand Up @@ -298,6 +299,18 @@ export class WorkspaceSavedObjectsClientWrapper {
return await wrapperOptions.client.addToWorkspaces(objects, targetWorkspaces, options);
};

const deleteByWorkspaceWithPermissionControl = async (
workspace: string,
options: SavedObjectsDeleteByWorkspaceOptions = {}
) => {
await this.validateMultiWorkspacesPermissions([workspace], wrapperOptions.request, [
WorkspacePermissionMode.LibraryWrite,
WorkspacePermissionMode.Management,
]);

return await wrapperOptions.client.deleteByWorkspace(workspace, options);
};

return {
...wrapperOptions.client,
get: getWithWorkspacePermissionControl,
Expand All @@ -313,6 +326,7 @@ export class WorkspaceSavedObjectsClientWrapper {
update: wrapperOptions.client.update,
bulkUpdate: wrapperOptions.client.bulkUpdate,
addToWorkspaces: addToWorkspacesWithPermissionControl,
deleteByWorkspace: deleteByWorkspaceWithPermissionControl,
};
};

Expand Down
4 changes: 3 additions & 1 deletion src/plugins/workspace/server/workspace_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,9 @@ export class WorkspaceClientWithSavedObject implements IWorkspaceDBImpl {
}
public async delete(requestDetail: IRequestDetail, id: string): Promise<IResponse<boolean>> {
try {
await this.getSavedObjectClientsFromRequestDetail(requestDetail).delete(WORKSPACE_TYPE, id);
const savedObjectClient = this.getSavedObjectClientsFromRequestDetail(requestDetail);
await savedObjectClient.delete(WORKSPACE_TYPE, id);
await savedObjectClient.deleteByWorkspace(id);
return {
success: true,
result: true,
Expand Down

0 comments on commit 110ce63

Please sign in to comment.