@@ -91,14 +91,18 @@ def blocking_call() -> List[Dict[str, Any]]:
9191 return await asyncio .to_thread (blocking_call )
9292
9393 async def update_cloud_accounts (
94- self , projects : List [Dict [str , Any ]], tenant_id : WorkspaceId , key_id : GcpServiceAccountKeyId
94+ self , projects : List [Dict [str , Any ]], tenant_id : WorkspaceId , key_id : GcpServiceAccountKeyId , only_new : bool
9595 ) -> None :
9696 for project in projects :
9797 await self .cloud_account_service .create_gcp_account (
98- workspace_id = tenant_id , account_id = project ["projectId" ], account_name = project .get ("name" ), key_id = key_id
98+ workspace_id = tenant_id ,
99+ account_id = project ["projectId" ],
100+ account_name = project .get ("name" ),
101+ key_id = key_id ,
102+ only_new_accounts = only_new ,
99103 )
100104
101- async def _import_projects_from_service_account (self , key : GcpServiceAccountKey ) -> None :
105+ async def _import_projects_from_service_account (self , key : GcpServiceAccountKey , only_new : bool = False ) -> None :
102106 try :
103107 projects = await self .list_projects (key .value )
104108 except MalformedError as e :
@@ -114,7 +118,7 @@ async def _import_projects_from_service_account(self, key: GcpServiceAccountKey)
114118 await self .service_account_key_repo .update_status (key .id , can_access_sa = False , error = str (e ))
115119 return None
116120 await self .service_account_key_repo .update_status (key .id , can_access_sa = True )
117- await self .update_cloud_accounts (projects , key .tenant_id , key .id )
121+ await self .update_cloud_accounts (projects , key .tenant_id , key .id , only_new = only_new )
118122
119123 async def _ping_new_service_account_keys (self ) -> None :
120124 created_less_than_30_minutes_ago = await self .service_account_key_repo .list_created_after (
@@ -126,10 +130,14 @@ async def _ping_new_service_account_keys(self) -> None:
126130 tg .create_task (self ._import_projects_from_service_account (key ))
127131
128132 async def _service_account_healthcheck (self ) -> None :
133+ """
134+ This will look for any new projects created by the users after we imported the SA keys
135+ and import them if we don't know about them yet.
136+ """
129137 older_than_1_hour = await self .service_account_key_repo .list_created_before (
130138 utc () - timedelta (hours = 1 ), only_valid_keys = True
131139 )
132140
133141 async with asyncio .TaskGroup () as tg :
134142 for key in older_than_1_hour :
135- tg .create_task (self ._import_projects_from_service_account (key ))
143+ tg .create_task (self ._import_projects_from_service_account (key , only_new = True ))
0 commit comments