pierrejeambrun commented on code in PR #44121:
URL: https://github.com/apache/airflow/pull/44121#discussion_r1850345992
##########
airflow/api_fastapi/core_api/routes/public/pools.py:
##########
@@ -165,15 +167,51 @@ def patch_pool(
@pools_router.post(
"/",
status_code=status.HTTP_201_CREATED,
- responses=create_openapi_http_exception_doc([status.HTTP_401_UNAUTHORIZED,
status.HTTP_403_FORBIDDEN]),
+ responses=create_openapi_http_exception_doc([status.HTTP_409_CONFLICT]),
)
def post_pool(
- post_body: PoolPostBody,
+ body: PoolPostBody,
session: Annotated[Session, Depends(get_session)],
) -> PoolResponse:
"""Create a Pool."""
- pool = Pool(**post_body.model_dump())
+ pool = Pool(**body.model_dump())
session.add(pool)
+ try:
+ session.commit()
+ except IntegrityError:
+ session.rollback()
+ raise HTTPException(status.HTTP_409_CONFLICT, f"Pool with name:
`{body.pool}` already exists")
return PoolResponse.model_validate(pool, from_attributes=True)
+
+
+@pools_router.post(
+ "/bulk",
+ status_code=status.HTTP_201_CREATED,
+ responses=create_openapi_http_exception_doc(
+ [
+ status.HTTP_409_CONFLICT,
+ ]
+ ),
+)
+def post_pools(
+ body: PoolPostBulkBody,
+ session: Annotated[Session, Depends(get_session)],
+) -> PoolCollectionResponse:
+ """Create multiple pools."""
+ pools = [Pool(**body.model_dump()) for body in body.pools]
+ session.add_all(pools)
+ try:
+ session.commit()
+ except IntegrityError as e:
+ session.rollback()
+ raise HTTPException(
+ status.HTTP_409_CONFLICT,
+ detail=f"One or more pools already exists. Error: {e}",
+ )
Review Comment:
Actually I am second guessing this.
The session is already commited on request exit.
Maybe what we need is simply an additional exception handler for
`IngegrityError`, global.
Because here the context manager will try to commit again an empty session
on exiting the endpoint. Which is a little bit weird and unecessary. Also this
would remove the need to `try / commit/rollback` manually.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]