@@ -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 (
@@ -903,8 +921,9 @@ async def create_azure_account(
903921 raise ResourceNotFound ("Organization does not exist" )
904922
905923 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
924+ if isinstance (existing .state , CloudAccountStates .Configured ):
925+ log .info ("Azure account already exists" )
926+ return existing
908927
909928 should_be_enabled = await self ._should_be_enabled (workspace )
910929
@@ -935,8 +954,26 @@ async def create_azure_account(
935954 last_degraded_scan_started_at = None ,
936955 )
937956
938- result = await self .cloud_account_repository .create (account )
939- log .info (f"Azure cloud Account { account_id } created" )
957+ if existing :
958+
959+ def set_state (acc : CloudAccount ) -> CloudAccount :
960+ return evolve (
961+ acc ,
962+ state = CloudAccountStates .Configured (
963+ access = AzureCloudAccess (subscription_credentials_id ),
964+ enabled = should_be_enabled ,
965+ scan = should_be_enabled ,
966+ ),
967+ state_updated_at = utc (),
968+ created_at = created_at ,
969+ updated_at = created_at ,
970+ )
971+
972+ result = await self .cloud_account_repository .update (existing .id , set_state )
973+ log .info (f"Azure cloud Account { account_id } updated from deleted to configured" )
974+ else :
975+ result = await self .cloud_account_repository .create (account )
976+ log .info (f"Azure cloud Account { account_id } created" )
940977
941978 await self .domain_events .publish (
942979 CloudAccountConfigured (
0 commit comments