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

Update workspace user_count when remove a user from a workspac_group #365

Merged
merged 1 commit into from
Sep 23, 2024
Merged
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
52 changes: 47 additions & 5 deletions src/spaceone/identity/service/workspace_group_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class WorkspaceGroupService(BaseService):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.workspace_group_mgr = WorkspaceGroupManager()
self.workspace_mgr = WorkspaceManager()
self.user_mgr = UserManager()
self.role_mgr = RoleManager()
self.rb_mgr = RoleBindingManager()
Expand Down Expand Up @@ -233,6 +234,7 @@ def remove_users(
Returns:
WorkspaceGroupResponse:
"""
workspace_id = params.workspace_id
workspace_group_id = params.workspace_group_id
users = params.users
domain_id = params.domain_id
Expand All @@ -253,7 +255,7 @@ def remove_users(
workspace_group_dict = workspace_group_vo.to_mongo().to_dict()
old_users = workspace_group_dict["users"]
updated_users = self.remove_users_from_workspace_group(
user_ids, old_users, workspace_group_id, domain_id
user_ids, old_users, workspace_group_id, domain_id, workspace_id
)
params.users = updated_users

Expand Down Expand Up @@ -453,10 +455,8 @@ def get_role_map(

return role_map

@staticmethod
def get_workspace_ids(workspace_group_id: str, domain_id: str) -> List[str]:
workspace_mgr = WorkspaceManager()
workspace_vos = workspace_mgr.filter_workspaces(
def get_workspace_ids(self, workspace_group_id: str, domain_id: str) -> List[str]:
workspace_vos = self.workspace_mgr.filter_workspaces(
workspace_group_id=workspace_group_id, domain_id=domain_id
)
workspace_ids = [workspace_vo.workspace_id for workspace_vo in workspace_vos]
Expand Down Expand Up @@ -578,6 +578,7 @@ def remove_users_from_workspace_group(
old_users: List[Dict[str, str]],
workspace_group_id: str,
domain_id: str,
workspace_id: str = None,
) -> List[Dict[str, str]]:
rb_vos = self.rb_mgr.filter_role_bindings(
user_id=user_ids,
Expand All @@ -591,6 +592,47 @@ def remove_users_from_workspace_group(

updated_users = [user for user in old_users if user["user_id"] not in user_ids]

if not workspace_id:
workspace_vos = self.workspace_mgr.filter_workspaces(
workspace_group_id=workspace_group_id, domain_id=domain_id
)
for workspace_vo in workspace_vos:
user_rb_ids = self.rb_mgr.stat_role_bindings(
query={
"distinct": "user_id",
"filter": [
{
"k": "workspace_id",
"v": workspace_vo.workspace_id,
"o": "eq",
},
{"k": "domain_id", "v": domain_id, "o": "eq"},
],
}
).get("results", [])
user_rb_total_count = len(user_rb_ids)

self.workspace_mgr.update_workspace_by_vo(
{"user_count": user_rb_total_count}, workspace_vo
)
else:
workspace_vo = self.workspace_mgr.get_workspace(workspace_id, domain_id)
if workspace_vo:
user_rb_ids = self.rb_mgr.stat_role_bindings(
query={
"distinct": "user_id",
"filter": [
{"k": "workspace_id", "v": workspace_id, "o": "eq"},
{"k": "domain_id", "v": domain_id, "o": "eq"},
],
}
).get("results", [])
user_rb_total_count = len(user_rb_ids)

self.workspace_mgr.update_workspace_by_vo(
{"user_count": user_rb_total_count}, workspace_vo
)

return updated_users

@staticmethod
Expand Down
Loading