Skip to content

Commit

Permalink
fix the gcp idempotency logic
Browse files Browse the repository at this point in the history
  • Loading branch information
meln1k committed Nov 29, 2024
1 parent dbf8f02 commit 3c7deff
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions fixbackend/cloud_accounts/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -838,8 +838,9 @@ async def create_gcp_account(
raise ResourceNotFound("Organization does not exist")

if existing := await self.cloud_account_repository.get_by_account_id(workspace_id, account_id):
log.info("GCP account already exists")
return existing
if isinstance(existing.state, CloudAccountStates.Configured):
log.info("GCP account already exists")
return existing

should_be_enabled = await self._should_be_enabled(workspace)

Expand Down Expand Up @@ -870,8 +871,25 @@ async def create_gcp_account(
last_degraded_scan_started_at=None,
)

result = await self.cloud_account_repository.create(account)
log.info(f"GCP cloud Account {account_id} created")
if existing:

def set_state(acc: CloudAccount) -> CloudAccount:
return evolve(
acc,
state=CloudAccountStates.Configured(
access=GcpCloudAccess(key_id), enabled=should_be_enabled, scan=should_be_enabled
),
account_name=account_name,
state_updated_at=utc(),
created_at=created_at,
updated_at=created_at,
)

result = await self.cloud_account_repository.update(existing.id, set_state)
log.info(f"GCP cloud Account {account_id} updated from deleted to configured")
else:
result = await self.cloud_account_repository.create(account)
log.info(f"GCP cloud Account {account_id} created")

await self.domain_events.publish(
CloudAccountConfigured(
Expand Down

0 comments on commit 3c7deff

Please sign in to comment.