Skip to content

Commit

Permalink
Merge pull request #359 from yjinjo/master
Browse files Browse the repository at this point in the history
Add changed_at to Workspace and modify the list of Workspace
  • Loading branch information
yjinjo authored Sep 11, 2024
2 parents 9e8b1e8 + f9ab372 commit 30120c6
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 14 deletions.
17 changes: 16 additions & 1 deletion src/spaceone/identity/manager/workspace_manager.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import logging
from typing import Tuple
from typing import Dict, List, Tuple

from mongoengine import QuerySet
from spaceone.core import cache
Expand Down Expand Up @@ -92,5 +92,20 @@ def filter_workspaces(self, **conditions) -> QuerySet:
def list_workspaces(self, query: dict) -> Tuple[QuerySet, int]:
return self.workspace_model.query(**query)

def list_workspace_group_workspaces(
self, workspace_group_id: str, domain_id: str
) -> Tuple[List[Dict[str, str]], int]:
workspace_vos = self.filter_workspaces(
workspace_group_id=workspace_group_id,
domain_id=domain_id,
)

workspace_group_workspaces = [
workspace_vo.to_dict() for workspace_vo in workspace_vos
]
total_count = len(workspace_group_workspaces)

return workspace_group_workspaces, total_count

def stat_workspaces(self, query: dict) -> dict:
return self.workspace_model.stat(**query)
2 changes: 2 additions & 0 deletions src/spaceone/identity/model/workspace/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class Workspace(MongoModel):
deleted_at = DateTimeField(default=None, null=True)
last_synced_at = DateTimeField(default=None, null=True)
dormant_updated_at = DateTimeField(default=None, null=True)
changed_at = DateTimeField(default=None, null=True)

meta = {
"updatable_fields": [
Expand All @@ -47,6 +48,7 @@ class Workspace(MongoModel):
"deleted_at",
"last_synced_at",
"dormant_updated_at",
"changed_at",
],
"minimal_fields": [
"workspace_id",
Expand Down
2 changes: 2 additions & 0 deletions src/spaceone/identity/model/workspace/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class WorkspaceResponse(BaseModel):
created_at: Union[datetime, None] = None
last_synced_at: Union[datetime, None] = None
dormant_updated_at: Union[datetime, None] = None
changed_at: Union[datetime, None] = None

def dict(self, *args, **kwargs):
data = super().dict(*args, **kwargs)
Expand All @@ -36,6 +37,7 @@ def dict(self, *args, **kwargs):
data["dormant_updated_at"] = utils.datetime_to_iso8601(
data.get("dormant_updated_at")
)
data["changed_at"] = utils.datetime_to_iso8601(data.get("changed_at"))
return data


Expand Down
54 changes: 41 additions & 13 deletions src/spaceone/identity/service/workspace_service.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
from datetime import datetime
from typing import Dict, List, Union

from spaceone.core.error import *
Expand All @@ -11,12 +12,9 @@
from spaceone.identity.manager.project_manager import ProjectManager
from spaceone.identity.manager.resource_manager import ResourceManager
from spaceone.identity.manager.role_binding_manager import RoleBindingManager
from spaceone.identity.manager.service_account_manager import \
ServiceAccountManager
from spaceone.identity.manager.trusted_account_manager import \
TrustedAccountManager
from spaceone.identity.manager.workspace_group_manager import \
WorkspaceGroupManager
from spaceone.identity.manager.service_account_manager import ServiceAccountManager
from spaceone.identity.manager.trusted_account_manager import TrustedAccountManager
from spaceone.identity.manager.workspace_group_manager import WorkspaceGroupManager
from spaceone.identity.manager.workspace_manager import WorkspaceManager
from spaceone.identity.model import Workspace
from spaceone.identity.model.workspace.request import *
Expand Down Expand Up @@ -125,7 +123,9 @@ def change_workspace_group(
workspace_group_vo = self.workspace_group_mgr.get_workspace_group(
old_workspace_group_id, domain_id
)
self._remove_workspace_from_group(old_workspace_group_id, domain_id)
self._remove_workspace_from_group(
workspace_id, old_workspace_group_id, domain_id
)

if is_updatable:
workspace_vo = self.workspace_mgr.update_workspace_by_vo(
Expand All @@ -141,9 +141,11 @@ def change_workspace_group(
workspace_vos = self.workspace_mgr.filter_workspaces(
workspace_group_id=old_workspace_group_id, domain_id=domain_id
)
self.workspace_group_mgr.update_workspace_group_by_vo(
{"workspace_count": len(workspace_vos)}, workspace_group_vo
)

if workspace_vos:
self.workspace_group_mgr.update_workspace_group_by_vo(
{"workspace_count": len(workspace_vos)}, workspace_group_vo
)

return WorkspaceResponse(**workspace_vo.to_dict())

Expand Down Expand Up @@ -299,9 +301,20 @@ def list(
"""

query = params.query or {}
workspace_vos, total_count = self.workspace_mgr.list_workspaces(query)
workspace_group_id = params.workspace_group_id

if not workspace_group_id:
workspace_vos, total_count = self.workspace_mgr.list_workspaces(query)

workspaces_info = [workspace_vo.to_dict() for workspace_vo in workspace_vos]
else:
workspaces_info, total_count = (
self.workspace_mgr.list_workspace_group_workspaces(
params.workspace_group_id,
params.domain_id,
)
)

workspaces_info = [workspace_vo.to_dict() for workspace_vo in workspace_vos]
return WorkspacesResponse(results=workspaces_info, total_count=total_count)

@transaction(permission="identity:Workspace.read", role_types=["DOMAIN_ADMIN"])
Expand Down Expand Up @@ -431,13 +444,28 @@ def _add_workspace_to_group(
domain_id,
)

if is_updatable:
workspace_vo.changed_at = datetime.utcnow()
self.workspace_mgr.update_workspace_by_vo(
{"changed_at": workspace_vo.changed_at}, workspace_vo
)

return is_updatable

def _remove_workspace_from_group(
self, old_workspace_group_id: str, domain_id: str
self, workspace_id: str, old_workspace_group_id: str, domain_id: str
) -> None:
self._delete_role_bindings(old_workspace_group_id, domain_id)

if old_workspace_group_id:
workspace_vo = self.workspace_mgr.get_workspace(
workspace_id=workspace_id, domain_id=domain_id
)
workspace_vo.changed_at = datetime.utcnow()
self.workspace_mgr.update_workspace_by_vo(
{"changed_at": workspace_vo.changed_at}, workspace_vo
)

@staticmethod
def _delete_role_bindings(existing_workspace_group_id: str, domain_id: str):
rb_mgr = RoleBindingManager()
Expand Down

0 comments on commit 30120c6

Please sign in to comment.