From 70840830c745c3ac8234e2460528eb9ff33882c6 Mon Sep 17 00:00:00 2001 From: Youngjin Jo Date: Fri, 6 Sep 2024 21:08:46 +0900 Subject: [PATCH] feat: add edge case when workspace group deleted Signed-off-by: Youngjin Jo --- src/spaceone/identity/error/custom.py | 5 +++++ .../identity/manager/workspace_group_manager.py | 17 +++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/src/spaceone/identity/error/custom.py b/src/spaceone/identity/error/custom.py index fa37fadc..e7198b8f 100644 --- a/src/spaceone/identity/error/custom.py +++ b/src/spaceone/identity/error/custom.py @@ -23,3 +23,8 @@ class ERROR_NOT_ALLOWED_TO_DELETE_ROLE_BINDING(ERROR_INVALID_ARGUMENT): class ERROR_ROLE_IN_USED_AT_ROLE_BINDING(ERROR_INVALID_ARGUMENT): _message = "Role is in used at RoleBinding. (role_id = {role_id})" + + +class ERROR_WORKSPACE_EXIST_IN_WORKSPACE_GROUP(ERROR_INVALID_ARGUMENT): + _message = """Workspace exists in WorkspaceGroup. (workspace_id = {workspace_id}, workspace_group_id = {workspace_group_id}) + Remove the workspace from the workspace group before deleting the workspace group.""" diff --git a/src/spaceone/identity/manager/workspace_group_manager.py b/src/spaceone/identity/manager/workspace_group_manager.py index f93af6c0..c7fe783b 100644 --- a/src/spaceone/identity/manager/workspace_group_manager.py +++ b/src/spaceone/identity/manager/workspace_group_manager.py @@ -5,6 +5,7 @@ from spaceone.core.error import ERROR_INVALID_PARAMETER, ERROR_NOT_FOUND from spaceone.core.manager import BaseManager +from spaceone.identity.error import ERROR_WORKSPACE_EXIST_IN_WORKSPACE_GROUP from spaceone.identity.manager.role_binding_manager import RoleBindingManager from spaceone.identity.manager.workspace_manager import WorkspaceManager from spaceone.identity.model.workspace_group.database import WorkspaceGroup @@ -15,6 +16,7 @@ class WorkspaceGroupManager(BaseManager): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) + self.workspace_manager = WorkspaceManager() self.workspace_group_model = WorkspaceGroup self.rb_mgr = RoleBindingManager() @@ -44,6 +46,21 @@ def _rollback(old_data): return workspace_group_vo.update(params) def delete_workspace_group_by_vo(self, workspace_group_vo: WorkspaceGroup) -> None: + workspace_vos = self.workspace_manager.filter_workspaces( + workspace_group_id=workspace_group_vo.workspace_group_id, + domain_id=workspace_group_vo.domain_id, + ) + for workspace_vo in workspace_vos: + if workspace_vo: + _LOGGER.error( + f"Workspace exists in WorkspaceGroup. ({workspace_vo.workspace_id}, {workspace_group_vo.workspace_group_id})" + "Remove the workspace from the workspace group before deleting the workspace group." + ) + raise ERROR_WORKSPACE_EXIST_IN_WORKSPACE_GROUP( + workspace_id=workspace_vo.workspace_id, + workspace_group_id=workspace_group_vo.workspace_group_id, + ) + if workspace_group_vo.users: user_ids = [user["user_id"] for user in workspace_group_vo.users] rb_vos = self.rb_mgr.filter_role_bindings(