@@ -903,8 +903,9 @@ async def create_azure_account(
903903 raise ResourceNotFound ("Organization does not exist" )
904904
905905 if existing := await self .cloud_account_repository .get_by_account_id (workspace_id , account_id ):
906- log .info ("Azure account already exists" )
907- return existing
906+ if not isinstance (existing .state , CloudAccountStates .Deleted ):
907+ log .info ("Azure account already exists" )
908+ return existing
908909
909910 should_be_enabled = await self ._should_be_enabled (workspace )
910911
@@ -935,8 +936,26 @@ async def create_azure_account(
935936 last_degraded_scan_started_at = None ,
936937 )
937938
938- result = await self .cloud_account_repository .create (account )
939- log .info (f"Azure cloud Account { account_id } created" )
939+ if existing :
940+
941+ def set_state (acc : CloudAccount ) -> CloudAccount :
942+ return evolve (
943+ acc ,
944+ state = CloudAccountStates .Configured (
945+ access = AzureCloudAccess (subscription_credentials_id ),
946+ enabled = should_be_enabled ,
947+ scan = should_be_enabled ,
948+ ),
949+ state_updated_at = utc (),
950+ created_at = created_at ,
951+ updated_at = created_at ,
952+ )
953+
954+ result = await self .cloud_account_repository .update (existing .id , set_state )
955+ log .info (f"Azure cloud Account { account_id } updated from deleted to configured" )
956+ else :
957+ result = await self .cloud_account_repository .create (account )
958+ log .info (f"Azure cloud Account { account_id } created" )
940959
941960 await self .domain_events .publish (
942961 CloudAccountConfigured (
0 commit comments