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

Remove duplicated workspace create logic #257

Merged
merged 2 commits 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")
89 changes: 36 additions & 53 deletions src/spaceone/identity/service/job_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, {})
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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": [
Expand All @@ -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)

Expand All @@ -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":
Expand All @@ -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:
Expand All @@ -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(),
Expand All @@ -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"]
Expand Down Expand Up @@ -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"]
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 = []
Expand Down
Loading