@@ -838,8 +838,9 @@ async def create_gcp_account(
838838 raise ResourceNotFound ("Organization does not exist" )
839839
840840 if existing := await self .cloud_account_repository .get_by_account_id (workspace_id , account_id ):
841- log .info ("GCP account already exists" )
842- return existing
841+ if isinstance (existing .state , CloudAccountStates .Configured ):
842+ log .info ("GCP account already exists" )
843+ return existing
843844
844845 should_be_enabled = await self ._should_be_enabled (workspace )
845846
@@ -870,8 +871,25 @@ async def create_gcp_account(
870871 last_degraded_scan_started_at = None ,
871872 )
872873
873- result = await self .cloud_account_repository .create (account )
874- log .info (f"GCP cloud Account { account_id } created" )
874+ if existing :
875+
876+ def set_state (acc : CloudAccount ) -> CloudAccount :
877+ return evolve (
878+ acc ,
879+ state = CloudAccountStates .Configured (
880+ access = GcpCloudAccess (key_id ), enabled = should_be_enabled , scan = should_be_enabled
881+ ),
882+ account_name = account_name ,
883+ state_updated_at = utc (),
884+ created_at = created_at ,
885+ updated_at = created_at ,
886+ )
887+
888+ result = await self .cloud_account_repository .update (existing .id , set_state )
889+ log .info (f"GCP cloud Account { account_id } updated from deleted to configured" )
890+ else :
891+ result = await self .cloud_account_repository .create (account )
892+ log .info (f"GCP cloud Account { account_id } created" )
875893
876894 await self .domain_events .publish (
877895 CloudAccountConfigured (
0 commit comments