33from fastapi import APIRouter , Depends , Response , status
44from pydantic import BaseModel , model_validator
55
6+ from foxops .database .repositories .group .errors import GroupNotFoundError
67from foxops .database .repositories .incarnation .errors import IncarnationNotFoundError
8+ from foxops .database .repositories .user .errors import UserNotFoundError
79from foxops .dependencies import (
810 authorization ,
911 get_change_service ,
@@ -230,9 +232,9 @@ async def reset_incarnation(
230232 message = f"could not initialize the incarnation as the provided template data "
231233 f"is invalid: { '; ' .join (error_messages )} "
232234 )
233- except IncarnationNotFoundError :
235+ except IncarnationNotFoundError as exc :
234236 response .status_code = status .HTTP_404_NOT_FOUND
235- return ApiError (message = "The incarnation was not found in the inventory" )
237+ return ApiError (message = str ( exc ) )
236238 except ChangeRejectedDueToNoChanges :
237239 response .status_code = status .HTTP_422_UNPROCESSABLE_ENTITY
238240 return ApiError (message = "The incarnation does not have any customizations. Nothing to reset." )
@@ -295,8 +297,8 @@ class UpdateIncarnationRequest(BaseModel):
295297 template_data : TemplateData
296298 owner_id : int
297299
298- user_permission : list [UnresolvedUserPermissions ]
299- group_permission : list [UnresolvedGroupPermissions ]
300+ user_permissions : list [UnresolvedUserPermissions ]
301+ group_permissions : list [UnresolvedGroupPermissions ]
300302
301303 automerge : bool
302304
@@ -341,10 +343,32 @@ async def update_incarnation(
341343 """
342344
343345 await incarnation_service .remove_all_permissions (incarnation_id )
344- await incarnation_service .set_user_permissions (incarnation_id , request .user_permission )
345- await incarnation_service .set_group_permissions (incarnation_id , request .group_permission )
346+ try :
347+ await incarnation_service .set_user_permissions (incarnation_id , request .user_permissions )
348+ except UserNotFoundError as exc :
349+ response .status_code = status .HTTP_404_NOT_FOUND
350+ return ApiError (message = str (exc ))
351+ except IncarnationNotFoundError as exc :
352+ response .status_code = status .HTTP_404_NOT_FOUND
353+ return ApiError (message = str (exc ))
346354
347- await incarnation_service .set_owner (incarnation_id , request .owner_id )
355+ try :
356+ await incarnation_service .set_group_permissions (incarnation_id , request .group_permissions )
357+ except GroupNotFoundError as exc :
358+ response .status_code = status .HTTP_404_NOT_FOUND
359+ return ApiError (message = str (exc ))
360+ except IncarnationNotFoundError as exc :
361+ response .status_code = status .HTTP_404_NOT_FOUND
362+ return ApiError (message = str (exc ))
363+
364+ try :
365+ await incarnation_service .set_owner (incarnation_id , request .owner_id )
366+ except UserNotFoundError as exc :
367+ response .status_code = status .HTTP_404_NOT_FOUND
368+ return ApiError (message = str (exc ))
369+ except IncarnationNotFoundError as exc :
370+ response .status_code = status .HTTP_404_NOT_FOUND
371+ return ApiError (message = str (exc ))
348372
349373 return await _create_change (
350374 incarnation_id = incarnation_id ,
@@ -362,8 +386,8 @@ class PatchIncarnationRequest(BaseModel):
362386
363387 requested_version : str | None = None
364388 requested_data : TemplateData | None = None
365- user_permission : list [UnresolvedUserPermissions ] | None = None
366- group_permission : list [UnresolvedGroupPermissions ] | None = None
389+ user_permissions : list [UnresolvedUserPermissions ] | None = None
390+ group_permissions : list [UnresolvedGroupPermissions ] | None = None
367391 owner_id : int | None = None
368392
369393 automerge : bool | None = None
@@ -373,8 +397,8 @@ def check_either_version_or_data_change_requested(self) -> Self:
373397 if (
374398 self .requested_version is None
375399 and self .requested_data is None
376- and self .user_permission is None
377- and self .group_permission is None
400+ and self .user_permissions is None
401+ and self .group_permissions is None
378402 and self .owner_id is None
379403 ):
380404 raise ValueError (
@@ -427,16 +451,37 @@ async def patch_incarnation(
427451
428452 requested_data = request .requested_data or {}
429453
430- if request .group_permission is not None :
431- await incarnation_service .remove_all_group_permissions (incarnation_id )
432- await incarnation_service .set_group_permissions (incarnation_id , request .group_permission )
433-
434- if request .user_permission is not None :
435- await incarnation_service .remove_all_user_permissions (incarnation_id )
436- await incarnation_service .set_user_permissions (incarnation_id , request .user_permission )
454+ if request .group_permissions is not None :
455+ try :
456+ await incarnation_service .remove_all_group_permissions (incarnation_id )
457+ await incarnation_service .set_group_permissions (incarnation_id , request .group_permissions )
458+ except GroupNotFoundError as exc :
459+ response .status_code = status .HTTP_404_NOT_FOUND
460+ return ApiError (message = str (exc ))
461+ except IncarnationNotFoundError as exc :
462+ response .status_code = status .HTTP_404_NOT_FOUND
463+ return ApiError (message = str (exc ))
464+
465+ if request .user_permissions is not None :
466+ try :
467+ await incarnation_service .remove_all_user_permissions (incarnation_id )
468+ await incarnation_service .set_user_permissions (incarnation_id , request .user_permissions )
469+ except UserNotFoundError as exc :
470+ response .status_code = status .HTTP_404_NOT_FOUND
471+ return ApiError (message = str (exc ))
472+ except IncarnationNotFoundError as exc :
473+ response .status_code = status .HTTP_404_NOT_FOUND
474+ return ApiError (message = str (exc ))
437475
438476 if request .owner_id is not None :
439- await incarnation_service .set_owner (incarnation_id , request .owner_id )
477+ try :
478+ await incarnation_service .set_owner (incarnation_id , request .owner_id )
479+ except UserNotFoundError as exc :
480+ response .status_code = status .HTTP_404_NOT_FOUND
481+ return ApiError (message = str (exc ))
482+ except IncarnationNotFoundError as exc :
483+ response .status_code = status .HTTP_404_NOT_FOUND
484+ return ApiError (message = str (exc ))
440485
441486 if request .requested_version is not None or request .requested_data is not None :
442487 return await _create_change (
@@ -449,7 +494,11 @@ async def patch_incarnation(
449494 change_service = change_service ,
450495 )
451496 else :
452- return await change_service .get_incarnation_with_details (incarnation_id )
497+ try :
498+ return await change_service .get_incarnation_with_details (incarnation_id )
499+ except IncarnationNotFoundError as exc :
500+ response .status_code = status .HTTP_404_NOT_FOUND
501+ return ApiError (message = str (exc ))
453502
454503
455504@router .delete (
0 commit comments