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

Merge agent_info with app_info #256

Merged
merged 1 commit into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
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
1 change: 0 additions & 1 deletion src/spaceone/identity/model/agent/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
56 changes: 44 additions & 12 deletions src/spaceone/identity/service/agent_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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")
Loading