Skip to content

Commit

Permalink
Merge pull request #275 from ImMin5/feature-service-accont-auto-sync
Browse files Browse the repository at this point in the history
Fix the error of not saving references
  • Loading branch information
ImMin5 authored Apr 24, 2024
2 parents 650783e + 1931c0a commit cb5194f
Showing 1 changed file with 50 additions and 42 deletions.
92 changes: 50 additions & 42 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: dict):
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.create_service_account_job(trusted_account_vo, {})
Expand Down Expand Up @@ -334,7 +334,7 @@ def sync_service_accounts(self, params: dict) -> None:
)

def create_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 @@ -437,10 +437,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 @@ -464,7 +464,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 @@ -474,10 +474,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 @@ -486,7 +486,7 @@ 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")
Expand All @@ -504,20 +504,23 @@ def _create_workspace(
workspace_vo = workspace_vos[0]

if workspace_vo.name != name:
params.update({"name": name})
params["name"] = name

if not workspace_vo.references:
params.update({"references": [reference_id]})
params["references"] = [reference_id]
elif reference_id not in workspace_vo.references:
params.update({"references": workspace_vo.references + [reference_id]})
params["references"] = workspace_vo.references + [reference_id]

params.update({"last_synced_at": datetime.utcnow()})
params["last_synced_at"] = datetime.utcnow()
_LOGGER.debug(f"[_create_workspace] update workspace: {params}")

workspace_vo = self.workspace_mgr.update_workspace_by_vo(
params, workspace_vo
)

self._remove_old_reference_id_from_workspace(domain_id, reference_id)
self._remove_old_reference_id_from_workspace(
domain_id, workspace_vo.workspace_id, reference_id
)
else:
params.update(
{
Expand All @@ -532,12 +535,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 @@ -587,14 +590,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 @@ -633,13 +636,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 @@ -725,11 +728,16 @@ def _create_service_account(
return service_account_vo

def _remove_old_reference_id_from_workspace(
self, domain_id: str, reference_id: str
self, domain_id: str, workspace_id: str, reference_id: str
) -> None:
workspace_vos = self.workspace_mgr.filter_workspaces(
domain_id=domain_id, references=[reference_id]
)
query = {
"filter": [
{"k": "domain_id", "v": domain_id, "o": "eq"},
{"k": "workspace_id", "v": workspace_id, "o": "not"},
{"k": "references", "v": [reference_id], "o": "in"},
],
}
workspace_vos, _ = self.workspace_mgr.list_workspaces(query)
for workspace_vo in workspace_vos:
references = workspace_vo.references
references.remove(reference_id)
Expand Down

0 comments on commit cb5194f

Please sign in to comment.