Skip to content

Commit 4009683

Browse files
refactor: Refactored delete pilot functions
1 parent 76381b2 commit 4009683

File tree

1 file changed

+36
-38
lines changed

1 file changed

+36
-38
lines changed

diracx-routers/src/diracx/routers/pilots/management.py

Lines changed: 36 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -95,68 +95,66 @@ async def add_pilot_stamps(
9595

9696
@router.delete("/", status_code=HTTPStatus.NO_CONTENT)
9797
async def delete_pilots(
98-
pilot_stamps: Annotated[
99-
list[str], Query(description="Stamps of the pilots we want to delete.")
100-
],
10198
pilot_db: PilotAgentsDB,
10299
check_permissions: CheckPilotManagementPolicyCallable,
103-
):
104-
"""Endpoint to delete a pilot.
105-
106-
If at least one pilot is not found, it WILL rollback.
107-
"""
108-
await check_permissions(
109-
action=ActionType.MANAGE_PILOTS,
110-
)
111-
112-
try:
113-
await delete_pilots_by_stamps(pilot_db=pilot_db, pilot_stamps=pilot_stamps)
114-
except PilotNotFoundError as e:
115-
raise HTTPException(
116-
status_code=status.HTTP_400_BAD_REQUEST,
117-
detail="At least one pilot has not been found.",
118-
) from e
119-
120-
121-
@router.delete("/management/pilot/interval", status_code=HTTPStatus.NO_CONTENT)
122-
async def clear_pilots(
123-
pilot_db: PilotAgentsDB,
100+
pilot_stamps: Annotated[
101+
list[str] | None, Query(description="Stamps of the pilots we want to delete.")
102+
] = None,
124103
age_in_days: Annotated[
125-
int,
104+
int | None,
126105
Query(
127106
description=(
128107
"The number of days that define the maximum age of pilots to be deleted."
129108
"Pilots older than this age will be considered for deletion."
130109
)
131110
),
132-
],
133-
check_permissions: CheckPilotManagementPolicyCallable,
111+
] = None,
134112
delete_only_aborted: Annotated[
135113
bool,
136114
Query(
137115
description=(
138116
"Flag indicating whether to only delete pilots whose status is 'Aborted'."
139117
"If set to True, only pilots with the 'Aborted' status will be deleted."
140-
"It is set by default as True to avoid any mistake."
118+
"It is set by default as True to avoid any mistake.",
119+
"This flag is only used for deletion by time.",
141120
)
142121
),
143122
] = False,
144123
):
145-
"""Endpoint for DIRAC to delete all pilots that lived more than age_in_days."""
146-
await check_permissions(ActionType.MANAGE_PILOTS)
124+
"""Endpoint to delete a pilot.
125+
126+
Two features:
127+
128+
1. Or you provide pilot_stamps, so you can delete pilots by their stamp
129+
2. Or you provide age_in_days, so you can delete pilots that lived more than age_in_days days.
147130
148-
if age_in_days < 0:
131+
If deleting by stamps, if at least one pilot is not found, it WILL rollback.
132+
"""
133+
await check_permissions(
134+
action=ActionType.MANAGE_PILOTS,
135+
)
136+
137+
if pilot_stamps:
138+
try:
139+
await delete_pilots_by_stamps(pilot_db=pilot_db, pilot_stamps=pilot_stamps)
140+
except PilotNotFoundError as e:
141+
raise HTTPException(
142+
status_code=status.HTTP_400_BAD_REQUEST,
143+
detail="At least one pilot has not been found.",
144+
) from e
145+
146+
elif age_in_days:
147+
await clear_pilots_bl(
148+
pilot_db=pilot_db,
149+
age_in_days=age_in_days,
150+
delete_only_aborted=delete_only_aborted,
151+
)
152+
else:
149153
raise HTTPException(
150154
status_code=status.HTTP_400_BAD_REQUEST,
151-
detail="age_in_days must be positive.",
155+
detail="You must provide either age_in_days or pilot_stamps.",
152156
)
153157

154-
await clear_pilots_bl(
155-
pilot_db=pilot_db,
156-
age_in_days=age_in_days,
157-
delete_only_aborted=delete_only_aborted,
158-
)
159-
160158

161159
EXAMPLE_UPDATE_FIELDS = {
162160
"Update the BenchMark field": {

0 commit comments

Comments
 (0)