@@ -280,13 +280,14 @@ async def list_gateways(self, db: Session, include_inactive: bool = False) -> Li
280
280
gateways = db .execute (query ).scalars ().all ()
281
281
return [GatewayRead .model_validate (g ) for g in gateways ]
282
282
283
- async def update_gateway (self , db : Session , gateway_id : str , gateway_update : GatewayUpdate ) -> GatewayRead :
283
+ async def update_gateway (self , db : Session , gateway_id : str , gateway_update : GatewayUpdate , include_inactive : bool = True ) -> GatewayRead :
284
284
"""Update a gateway.
285
285
286
286
Args:
287
287
db: Database session
288
288
gateway_id: Gateway ID to update
289
289
gateway_update: Updated gateway data
290
+ include_inactive: Whether to include inactive gateways
290
291
291
292
Returns:
292
293
Updated gateway information
@@ -302,88 +303,86 @@ async def update_gateway(self, db: Session, gateway_id: str, gateway_update: Gat
302
303
if not gateway :
303
304
raise GatewayNotFoundError (f"Gateway not found: { gateway_id } " )
304
305
305
- if not gateway .enabled :
306
- raise GatewayNotFoundError (f"Gateway '{ gateway .name } ' exists but is inactive" )
307
-
308
- # Check for name conflicts if name is being changed
309
- if gateway_update .name is not None and gateway_update .name != gateway .name :
310
- existing_gateway = db .execute (select (DbGateway ).where (DbGateway .name == gateway_update .name ).where (DbGateway .id != gateway_id )).scalar_one_or_none ()
311
-
312
- if existing_gateway :
313
- raise GatewayNameConflictError (
314
- gateway_update .name ,
315
- enabled = existing_gateway .enabled ,
316
- gateway_id = existing_gateway .id ,
317
- )
318
-
319
- # Update fields if provided
320
- if gateway_update .name is not None :
321
- gateway .name = gateway_update .name
322
- gateway .slug = slugify (gateway_update .name )
323
- if gateway_update .url is not None :
324
- gateway .url = gateway_update .url
325
- if gateway_update .description is not None :
326
- gateway .description = gateway_update .description
327
- if gateway_update .transport is not None :
328
- gateway .transport = gateway_update .transport
329
-
330
- if getattr (gateway , "auth_type" , None ) is not None :
331
- gateway .auth_type = gateway_update .auth_type
332
-
333
- # if auth_type is not None and only then check auth_value
334
- if getattr (gateway , "auth_value" , {}) != {}:
335
- gateway .auth_value = gateway_update .auth_value
336
-
337
- # Try to reinitialize connection if URL changed
338
- if gateway_update .url is not None :
339
- try :
340
- capabilities , tools = await self ._initialize_gateway (gateway .url , gateway .auth_value , gateway .transport )
341
- new_tool_names = [tool .name for tool in tools ]
306
+ if gateway .enabled or include_inactive :
307
+ # Check for name conflicts if name is being changed
308
+ if gateway_update .name is not None and gateway_update .name != gateway .name :
309
+ existing_gateway = db .execute (select (DbGateway ).where (DbGateway .name == gateway_update .name ).where (DbGateway .id != gateway_id )).scalar_one_or_none ()
310
+
311
+ if existing_gateway :
312
+ raise GatewayNameConflictError (
313
+ gateway_update .name ,
314
+ enabled = existing_gateway .enabled ,
315
+ gateway_id = existing_gateway .id ,
316
+ )
317
+
318
+ # Update fields if provided
319
+ if gateway_update .name is not None :
320
+ gateway .name = gateway_update .name
321
+ gateway .slug = slugify (gateway_update .name )
322
+ if gateway_update .url is not None :
323
+ gateway .url = gateway_update .url
324
+ if gateway_update .description is not None :
325
+ gateway .description = gateway_update .description
326
+ if gateway_update .transport is not None :
327
+ gateway .transport = gateway_update .transport
328
+
329
+ if getattr (gateway , "auth_type" , None ) is not None :
330
+ gateway .auth_type = gateway_update .auth_type
331
+
332
+ # if auth_type is not None and only then check auth_value
333
+ if getattr (gateway , "auth_value" , {}) != {}:
334
+ gateway .auth_value = gateway_update .auth_value
335
+
336
+ # Try to reinitialize connection if URL changed
337
+ if gateway_update .url is not None :
338
+ try :
339
+ capabilities , tools = await self ._initialize_gateway (gateway .url , gateway .auth_value , gateway .transport )
340
+ new_tool_names = [tool .name for tool in tools ]
342
341
343
- for tool in tools :
344
- existing_tool = db .execute (select (DbTool ).where (DbTool .original_name == tool .name ).where (DbTool .gateway_id == gateway_id )).scalar_one_or_none ()
345
- if not existing_tool :
346
- gateway .tools .append (
347
- DbTool (
348
- original_name = tool .name ,
349
- original_name_slug = slugify (tool .name ),
350
- url = gateway .url ,
351
- description = tool .description ,
352
- integration_type = tool .integration_type ,
353
- request_type = tool .request_type ,
354
- headers = tool .headers ,
355
- input_schema = tool .input_schema ,
356
- jsonpath_filter = tool .jsonpath_filter ,
357
- auth_type = gateway .auth_type ,
358
- auth_value = gateway .auth_value ,
342
+ for tool in tools :
343
+ existing_tool = db .execute (select (DbTool ).where (DbTool .original_name == tool .name ).where (DbTool .gateway_id == gateway_id )).scalar_one_or_none ()
344
+ if not existing_tool :
345
+ gateway .tools .append (
346
+ DbTool (
347
+ original_name = tool .name ,
348
+ original_name_slug = slugify (tool .name ),
349
+ url = gateway .url ,
350
+ description = tool .description ,
351
+ integration_type = tool .integration_type ,
352
+ request_type = tool .request_type ,
353
+ headers = tool .headers ,
354
+ input_schema = tool .input_schema ,
355
+ jsonpath_filter = tool .jsonpath_filter ,
356
+ auth_type = gateway .auth_type ,
357
+ auth_value = gateway .auth_value ,
358
+ )
359
359
)
360
- )
361
360
362
- gateway .capabilities = capabilities
363
- gateway .tools = [tool for tool in gateway .tools if tool .original_name in new_tool_names ] # keep only still-valid rows
364
- gateway .last_seen = datetime .now (timezone .utc )
361
+ gateway .capabilities = capabilities
362
+ gateway .tools = [tool for tool in gateway .tools if tool .original_name in new_tool_names ] # keep only still-valid rows
363
+ gateway .last_seen = datetime .now (timezone .utc )
365
364
366
- # Update tracking with new URL
367
- self ._active_gateways .discard (gateway .url )
368
- self ._active_gateways .add (gateway .url )
369
- except Exception as e :
370
- logger .warning (f"Failed to initialize updated gateway: { e } " )
365
+ # Update tracking with new URL
366
+ self ._active_gateways .discard (gateway .url )
367
+ self ._active_gateways .add (gateway .url )
368
+ except Exception as e :
369
+ logger .warning (f"Failed to initialize updated gateway: { e } " )
371
370
372
- gateway .updated_at = datetime .now (timezone .utc )
373
- db .commit ()
374
- db .refresh (gateway )
371
+ gateway .updated_at = datetime .now (timezone .utc )
372
+ db .commit ()
373
+ db .refresh (gateway )
375
374
376
- # Notify subscribers
377
- await self ._notify_gateway_updated (gateway )
375
+ # Notify subscribers
376
+ await self ._notify_gateway_updated (gateway )
378
377
379
- logger .info (f"Updated gateway: { gateway .name } " )
380
- return GatewayRead .model_validate (gateway )
378
+ logger .info (f"Updated gateway: { gateway .name } " )
379
+ return GatewayRead .model_validate (gateway )
381
380
382
381
except Exception as e :
383
382
db .rollback ()
384
383
raise GatewayError (f"Failed to update gateway: { str (e )} " )
385
384
386
- async def get_gateway (self , db : Session , gateway_id : str , include_inactive : bool = False ) -> GatewayRead :
385
+ async def get_gateway (self , db : Session , gateway_id : str , include_inactive : bool = True ) -> GatewayRead :
387
386
"""Get a specific gateway by ID.
388
387
389
388
Args:
@@ -401,10 +400,10 @@ async def get_gateway(self, db: Session, gateway_id: str, include_inactive: bool
401
400
if not gateway :
402
401
raise GatewayNotFoundError (f"Gateway not found: { gateway_id } " )
403
402
404
- if not gateway .enabled and not include_inactive :
405
- raise GatewayNotFoundError ( f"Gateway ' { gateway . name } ' exists but is inactive" )
403
+ if gateway .enabled or include_inactive :
404
+ return GatewayRead . model_validate ( gateway )
406
405
407
- return GatewayRead . model_validate ( gateway )
406
+ raise GatewayNotFoundError ( f"Gateway not found: { gateway_id } " )
408
407
409
408
async def toggle_gateway_status (self , db : Session , gateway_id : str , activate : bool , reachable : bool = True , only_update_reachable : bool = False ) -> GatewayRead :
410
409
"""Toggle gateway active status.
0 commit comments