cli/vms: serialize uuid already in the api

This commit is contained in:
Jörg Thalheim
2023-09-27 11:11:30 +02:00
committed by Mic92
parent 08eab785c6
commit dea49073cb
2 changed files with 6 additions and 16 deletions

View File

@@ -5,14 +5,7 @@ import shlex
from typing import Annotated from typing import Annotated
from uuid import UUID from uuid import UUID
from fastapi import ( from fastapi import APIRouter, BackgroundTasks, Body, HTTPException, Request, status
APIRouter,
BackgroundTasks,
Body,
HTTPException,
Request,
status,
)
from fastapi.encoders import jsonable_encoder from fastapi.encoders import jsonable_encoder
from fastapi.responses import JSONResponse, StreamingResponse from fastapi.responses import JSONResponse, StreamingResponse
@@ -126,13 +119,13 @@ command output:
@router.get("/api/vms/{uuid}/status") @router.get("/api/vms/{uuid}/status")
async def get_status(uuid: str) -> VmStatusResponse: async def get_status(uuid: UUID) -> VmStatusResponse:
task = get_task(uuid) task = get_task(uuid)
return VmStatusResponse(running=not task.finished, status=0) return VmStatusResponse(running=not task.finished, status=0)
@router.get("/api/vms/{uuid}/logs") @router.get("/api/vms/{uuid}/logs")
async def get_logs(uuid: str) -> StreamingResponse: async def get_logs(uuid: UUID) -> StreamingResponse:
# Generator function that yields log lines as they are available # Generator function that yields log lines as they are available
def stream_logs(): def stream_logs():
task = get_task(uuid) task = get_task(uuid)
@@ -158,6 +151,7 @@ async def get_logs(uuid: str) -> StreamingResponse:
media_type="text/plain", media_type="text/plain",
) )
@router.post("/api/vms/create") @router.post("/api/vms/create")
async def create_vm( async def create_vm(
vm: Annotated[VmConfig, Body()], background_tasks: BackgroundTasks vm: Annotated[VmConfig, Body()], background_tasks: BackgroundTasks

View File

@@ -82,12 +82,8 @@ class TaskPool:
self.lock: threading.RLock = threading.RLock() self.lock: threading.RLock = threading.RLock()
self.pool: dict[UUID, BaseTask] = {} self.pool: dict[UUID, BaseTask] = {}
def __getitem__(self, uuid: str | UUID) -> BaseTask: def __getitem__(self, uuid: UUID) -> BaseTask:
with self.lock: with self.lock:
if type(uuid) is UUID:
return self.pool[uuid]
else:
uuid = UUID(uuid)
return self.pool[uuid] return self.pool[uuid]
def __setitem__(self, uuid: UUID, task: BaseTask) -> None: def __setitem__(self, uuid: UUID, task: BaseTask) -> None: