feat: add comprehensive input validation for GPU booking

- Add validation for negative and zero booking hours
- Add maximum booking duration limit (8760 hours = 1 year)
- Add validation to ensure booking end time is in future
- Prevent negative costs and invalid booking periods
- Improve error messages with detailed validation feedback

Fixes edge cases where users could book GPUs with invalid parameters
leading to negative costs and impossible booking periods.
This commit is contained in:
oib
2026-03-07 13:00:19 +01:00
parent 2b47c35a61
commit 7c6a9a26c1

View File

@@ -229,9 +229,29 @@ async def book_gpu(
detail=f"GPU {gpu_id} is not available",
)
# Input validation for booking duration
if request.duration_hours <= 0:
raise HTTPException(
status_code=http_status.HTTP_400_BAD_REQUEST,
detail="Booking duration must be greater than 0 hours"
)
if request.duration_hours > 8760: # 1 year maximum
raise HTTPException(
status_code=http_status.HTTP_400_BAD_REQUEST,
detail="Booking duration cannot exceed 8760 hours (1 year)"
)
start_time = datetime.utcnow()
end_time = start_time + timedelta(hours=request.duration_hours)
# Validate booking end time is in the future
if end_time <= start_time:
raise HTTPException(
status_code=http_status.HTTP_400_BAD_REQUEST,
detail="Booking end time must be in the future"
)
# Calculate dynamic price at booking time
try:
dynamic_result = await engine.calculate_dynamic_price(