From bb402b2d7149ba3e456b1a964a03d11fb6bb19eb Mon Sep 17 00:00:00 2001 From: Nikita Melkozerov Date: Fri, 29 Nov 2024 13:46:58 +0100 Subject: [PATCH] move account from deleted to configured when creating --- fixbackend/cloud_accounts/service.py | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/fixbackend/cloud_accounts/service.py b/fixbackend/cloud_accounts/service.py index 3983637b..b44789b8 100644 --- a/fixbackend/cloud_accounts/service.py +++ b/fixbackend/cloud_accounts/service.py @@ -903,8 +903,9 @@ async def create_azure_account( raise ResourceNotFound("Organization does not exist") if existing := await self.cloud_account_repository.get_by_account_id(workspace_id, account_id): - log.info("Azure account already exists") - return existing + if not isinstance(existing.state, CloudAccountStates.Deleted): + log.info("Azure account already exists") + return existing should_be_enabled = await self._should_be_enabled(workspace) @@ -935,8 +936,26 @@ async def create_azure_account( last_degraded_scan_started_at=None, ) - result = await self.cloud_account_repository.create(account) - log.info(f"Azure cloud Account {account_id} created") + if existing: + + def set_state(acc: CloudAccount) -> CloudAccount: + return evolve( + acc, + state=CloudAccountStates.Configured( + access=AzureCloudAccess(subscription_credentials_id), + enabled=should_be_enabled, + scan=should_be_enabled, + ), + 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"Azure cloud Account {account_id} updated from deleted to configured") + else: + result = await self.cloud_account_repository.create(account) + log.info(f"Azure cloud Account {account_id} created") await self.domain_events.publish( CloudAccountConfigured(