From 1aa698004a159c9fe6d82b64d6d2cf53f3ad00ab Mon Sep 17 00:00:00 2001 From: ImMin5 Date: Fri, 12 Apr 2024 15:03:44 +0900 Subject: [PATCH 1/2] feat: remove duplicated workspace create logic (#199) Signed-off-by: ImMin5 --- src/spaceone/identity/service/job_service.py | 89 ++++++++------------ 1 file changed, 36 insertions(+), 53 deletions(-) diff --git a/src/spaceone/identity/service/job_service.py b/src/spaceone/identity/service/job_service.py index 8c4a31c4..3f07e87b 100644 --- a/src/spaceone/identity/service/job_service.py +++ b/src/spaceone/identity/service/job_service.py @@ -65,7 +65,7 @@ def create_jobs_by_trusted_account(self, params): current_hour = params.get("current_hour", datetime.utcnow().hour) for trusted_account_vo in self._get_all_schedule_enabled_trusted_accounts( - current_hour + current_hour ): try: self.created_service_account_job(trusted_account_vo, {}) @@ -327,7 +327,7 @@ def sync_service_accounts(self, params: dict) -> None: ) def created_service_account_job( - self, trusted_account_vo: TrustedAccount, job_options: dict + self, trusted_account_vo: TrustedAccount, job_options: dict ) -> Union[Job, dict]: resource_group = trusted_account_vo.resource_group provider = trusted_account_vo.provider @@ -431,10 +431,10 @@ def _get_trusted_secret_data(self, trusted_secret_id: str, domain_id: str) -> di return secret_data def _check_duplicate_job( - self, - domain_id: str, - trusted_account_id: str, - this_job_vo: Job, + self, + domain_id: str, + trusted_account_id: str, + this_job_vo: Job, ) -> bool: query = { "filter": [ @@ -458,7 +458,7 @@ def _check_duplicate_job( return False def _is_job_failed( - self, job_id: str, domain_id: str, workspace_id: str = None + self, job_id: str, domain_id: str, workspace_id: str = None ) -> bool: job_vo: Job = self.job_mgr.get_job(domain_id, job_id, workspace_id) @@ -468,10 +468,10 @@ def _is_job_failed( return False def _close_job( - self, - job_id: str, - domain_id: str, - workspace_id: str = None, + self, + job_id: str, + domain_id: str, + workspace_id: str = None, ): job_vo: Job = self.job_mgr.get_job(domain_id, job_id, workspace_id) if job_vo.status == "IN_PROGRESS": @@ -480,21 +480,20 @@ def _close_job( self.job_mgr.update_job_by_vo({"finished_at": datetime.utcnow()}, job_vo) def _create_workspace( - self, domain_id: str, trusted_account_id: str, location_info: dict + self, domain_id: str, trusted_account_id: str, location_info: dict ) -> Workspace: name = location_info.get("name") reference_id = location_info.get("resource_id") - name, tags = self._check_duplicated_workspace_name(name, domain_id) workspace_vos = self.workspace_mgr.filter_workspaces( - domain_id=domain_id, reference_id=reference_id, is_managed=True + domain_id=domain_id, name=name ) _LOGGER.debug( f"[_create_workspace] {name} 'domain_id': {domain_id}, 'reference_id': {reference_id} 'tags': {tags}, 'is_managed' :True count: {len(workspace_vos)}" ) - params = {"trusted_account_id": trusted_account_id} + params = {"trusted_account_id": trusted_account_id, "is_managed": True} if workspace_vos: workspace_vo = workspace_vos[0] if workspace_vo.name != name: @@ -508,7 +507,7 @@ def _create_workspace( { "name": name, "is_managed": True, - "tags": self._set_workspace_theme(tags), + "tags": self._set_workspace_theme(), "reference_id": reference_id, "domain_id": domain_id, "last_synced_at": datetime.utcnow(), @@ -518,12 +517,12 @@ def _create_workspace( return workspace_vo def _create_project_group( - self, - domain_id: str, - workspace_id: str, - trusted_account_id: str, - location_info: dict, - parent_group_id: str = None, + self, + domain_id: str, + workspace_id: str, + trusted_account_id: str, + location_info: dict, + parent_group_id: str = None, ) -> ProjectGroup: name = location_info["name"] reference_id = location_info["resource_id"] @@ -571,14 +570,14 @@ def _create_project_group( return project_group_vo def _create_project( - self, - result: dict, - domain_id: str, - workspace_id: str, - trusted_account_id: str, - project_group_id: str = None, - sync_options: dict = None, - project_type: str = "PRIVATE", + self, + result: dict, + domain_id: str, + workspace_id: str, + trusted_account_id: str, + project_group_id: str = None, + sync_options: dict = None, + project_type: str = "PRIVATE", ) -> Project: name = result["name"] reference_id = result["resource_id"] @@ -614,13 +613,13 @@ def _create_project( return project_vo def _create_service_account( - self, - result: dict, - project_vo: Project, - trusted_account_id: str, - trusted_secret_id: str, - provider: str, - sync_options: dict = None, + self, + result: dict, + project_vo: Project, + trusted_account_id: str, + trusted_secret_id: str, + provider: str, + sync_options: dict = None, ) -> Union[ServiceAccount, None]: domain_id = project_vo.domain_id workspace_id = project_vo.workspace_id @@ -705,22 +704,6 @@ def _create_service_account( ) return service_account_vo - # todo : temporary function need policy about duplicated workspace name - def _check_duplicated_workspace_name( - self, name: str, domain_id: str, tags: dict = None - ) -> Tuple[str, dict]: - workspace_vos = self.workspace_mgr.filter_workspaces( - domain_id=domain_id, name=name - ) - - if tags is None: - tags = {} - - if workspace_vos: - tags.update({"origin_name": name}) - name = f"{name} ({len(workspace_vos) + 1})" - return name, tags - @staticmethod def _get_location(result: dict, resource_group: str, sync_options: dict) -> list: location = [] From 4c596e2be001d3de8a0cbaf085743b06c2784ee7 Mon Sep 17 00:00:00 2001 From: Youngjin Jo Date: Fri, 12 Apr 2024 14:07:59 +0900 Subject: [PATCH 2/2] feat: merge agent_info with app_info Signed-off-by: Youngjin Jo --- src/spaceone/identity/model/agent/response.py | 1 - .../identity/service/agent_service.py | 56 +++++++++++++++---- 2 files changed, 44 insertions(+), 13 deletions(-) diff --git a/src/spaceone/identity/model/agent/response.py b/src/spaceone/identity/model/agent/response.py index 27d527af..08c2e5fe 100644 --- a/src/spaceone/identity/model/agent/response.py +++ b/src/spaceone/identity/model/agent/response.py @@ -13,7 +13,6 @@ class AgentResponse(BaseModel): agent_id: Union[str, None] = None options: Union[dict, None] = None client_secret: Union[str, None] = None - name: Union[str, None] = None state: Union[State, None] = None is_managed: Union[bool, None] = None role_type: Union[RoleType, None] = None diff --git a/src/spaceone/identity/service/agent_service.py b/src/spaceone/identity/service/agent_service.py index 2db27823..17598b37 100644 --- a/src/spaceone/identity/service/agent_service.py +++ b/src/spaceone/identity/service/agent_service.py @@ -123,7 +123,9 @@ def create(self, params: AgentCreateRequest) -> Union[AgentResponse, dict]: agent_vo = self.agent_mgr.create_agent(create_agent_params) - return AgentResponse(**agent_vo.to_dict(), client_secret=client_secret) + agent_info = self._update_agent_info_with_app_vo(agent_vo, app_vo) + + return AgentResponse(**agent_info, client_secret=client_secret) @transaction( permission="identity:Agent.write", @@ -162,7 +164,9 @@ def enable(self, params: AgentEnableRequest) -> Union[AgentResponse, dict]: app_vo = self.app_mgr.get_app(agent_vo.app_id, domain_id, workspace_id) app_vo = self.app_mgr.enable_app(app_vo) - return AgentResponse(**app_vo.to_dict()) + agent_info = self._update_agent_info_with_app_vo(agent_vo, app_vo) + + return AgentResponse(**agent_info) @transaction( permission="identity:Agent.write", @@ -201,7 +205,9 @@ def disable(self, params: AgentDisableRequest) -> Union[AgentResponse, dict]: app_vo = self.app_mgr.get_app(agent_vo.app_id, domain_id, workspace_id) app_vo = self.app_mgr.disable_app(app_vo) - return AgentResponse(**app_vo.to_dict()) + agent_info = self._update_agent_info_with_app_vo(agent_vo, app_vo) + + return AgentResponse(**agent_info) @transaction( permission="identity:Agent.write", @@ -246,7 +252,9 @@ def regenerate(self, params: AgentRegenerateRequest) -> Union[AgentResponse, dic app_vo = self.app_mgr.update_app_by_vo({"client_id": client_id}, app_vo) - return AgentResponse(**app_vo.to_dict(), client_secret=client_secret) + agent_info = self._update_agent_info_with_app_vo(agent_vo, app_vo) + + return AgentResponse(**agent_info, client_secret=client_secret) @transaction( permission="identity:Agent.write", @@ -307,14 +315,23 @@ def get(self, params: AgentGetRequest) -> Union[AgentResponse, dict]: Returns: AgentResponse: """ + service_account_id = params.service_account_id + domain_id = params.domain_id + workspace_id = params.workspace_id + user_projects = params.user_projects + agent_vo = self.agent_mgr.get_agent( - params.service_account_id, - params.domain_id, - params.workspace_id, - params.user_projects, + service_account_id, + domain_id, + workspace_id, + user_projects, ) - return AgentResponse(**agent_vo.to_dict()) + app_vo = self.app_mgr.get_app(agent_vo.app_id, domain_id, workspace_id) + + agent_info = self._update_agent_info_with_app_vo(agent_vo, app_vo) + + return AgentResponse(**agent_info) @transaction( permission="identity:Agent.read", @@ -348,11 +365,13 @@ def list(self, params: AgentSearchQueryRequest) -> Union[AgentsResponse, dict]: AgentsResponse: """ query = params.query or {} - print(query) - agent_vos, total_count = self.agent_mgr.list_agents(query) + agent_vos, agent_total_count = self.agent_mgr.list_agents(query) agents_info = [agent_vo.to_dict() for agent_vo in agent_vos] - return AgentsResponse(results=agents_info, total_count=total_count) + # app_ids = [agent_vo.app_id for agent_vo in agent_vos] + # app_vos, app_total_count = self.app_mgr.list_apps({"app_id": {"$in": app_ids}}) + + return AgentsResponse(results=agents_info, total_count=agent_total_count) def _create_agent_client_secret( self, app_vo: App, service_account_id: str @@ -383,6 +402,19 @@ def _create_agent_client_secret( return client_id, client_secret + @staticmethod + def _update_agent_info_with_app_vo(agent_vo, app_vo): + agent_info = agent_vo.to_dict() + agent_info["state"] = app_vo.state + agent_info["is_managed"] = app_vo.is_managed + agent_info["role_type"] = app_vo.role_type + agent_info["role_id"] = app_vo.role_id + agent_info["app_id"] = app_vo.app_id + agent_info["client_id"] = app_vo.client_id + agent_info["expired_at"] = app_vo.expired_at + agent_info["last_accessed_at"] = app_vo.last_accessed_at + return agent_info + @staticmethod def _get_expired_at() -> str: return (datetime.utcnow() + timedelta(days=365)).strftime("%Y-%m-%d %H:%M:%S")