From 70ba12dcb955a5570ce1e542ab19ed2fb5dc3918 Mon Sep 17 00:00:00 2001 From: ImMin5 Date: Mon, 22 Apr 2024 21:24:29 +0900 Subject: [PATCH] feat: modify field 'reference_id' -> 'references' at WorkspaceInfo Signed-off-by: ImMin5 --- .../identity/model/workspace/database.py | 6 +++++- .../identity/model/workspace/response.py | 2 +- src/spaceone/identity/service/job_service.py | 20 +++++++++++++++++++ 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/spaceone/identity/model/workspace/database.py b/src/spaceone/identity/model/workspace/database.py index 73c13cbe..47cbe2be 100644 --- a/src/spaceone/identity/model/workspace/database.py +++ b/src/spaceone/identity/model/workspace/database.py @@ -13,7 +13,7 @@ class Workspace(MongoModel): ) tags = DictField(default=None) created_by = StringField(max_length=255) - reference_id = StringField(max_length=255, default=None, null=True) + references = ListField(StringField(max_length=255), default=None, null=True) is_managed = BooleanField(default=False) trusted_account_id = StringField(max_length=40, default=None, null=True) domain_id = StringField(max_length=40) @@ -28,6 +28,7 @@ class Workspace(MongoModel): "tags", "is_managed", "trusted_account_id", + "references", "deleted_at", "last_synced_at", ], @@ -37,6 +38,9 @@ class Workspace(MongoModel): "state", "is_managed", ], + "change_query_keys": { + "reference_id": "references", + }, "ordering": ["name"], "indexes": ["name", "domain_id"], } diff --git a/src/spaceone/identity/model/workspace/response.py b/src/spaceone/identity/model/workspace/response.py index 9a308fde..2245403d 100644 --- a/src/spaceone/identity/model/workspace/response.py +++ b/src/spaceone/identity/model/workspace/response.py @@ -15,7 +15,7 @@ class WorkspaceResponse(BaseModel): state: Union[State, None] = None tags: Union[dict, None] = None created_by: Union[str, None] = None - reference_id: Union[str, None] = None + references: Union[list, None] = None is_managed: Union[bool, None] = None trusted_account_id: Union[str, None] = None domain_id: Union[str, None] = None diff --git a/src/spaceone/identity/service/job_service.py b/src/spaceone/identity/service/job_service.py index cbcbb596..7fafe27a 100644 --- a/src/spaceone/identity/service/job_service.py +++ b/src/spaceone/identity/service/job_service.py @@ -502,12 +502,18 @@ def _create_workspace( params = {"trusted_account_id": trusted_account_id, "is_managed": True} if workspace_vos: workspace_vo = workspace_vos[0] + if workspace_vo.name != name: params.update({"name": name}) + if workspace_vo.references not in reference_id: + params.update({"references": workspace_vo.references + [reference_id]}) + params.update({"last_synced_at": datetime.utcnow()}) workspace_vo = self.workspace_mgr.update_workspace_by_vo( params, workspace_vo ) + + self._remove_old_reference_id_from_workspace(domain_id, reference_id) else: params.update( { @@ -515,6 +521,7 @@ def _create_workspace( "tags": self._set_workspace_theme(), "domain_id": domain_id, "last_synced_at": datetime.utcnow(), + "references": [reference_id], } ) workspace_vo = self.workspace_mgr.create_workspace(params) @@ -713,6 +720,19 @@ def _create_service_account( ) return service_account_vo + def _remove_old_reference_id_from_workspace( + self, domain_id: str, reference_id: str + ) -> None: + workspace_vos = self.workspace_mgr.filter_workspaces( + domain_id=domain_id, references=[reference_id] + ) + for workspace_vo in workspace_vos: + references = workspace_vo.references + references.remove(reference_id) + self.workspace_mgr.update_workspace_by_vo( + {"references": references}, workspace_vo + ) + @staticmethod def _get_location(result: dict, resource_group: str, sync_options: dict) -> list: location = result.get("location", [])