Merge pull request 'add error handling to endpoint' (#386) from feat/flake-attr into main
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
import json
|
import json
|
||||||
|
from json.decoder import JSONDecodeError
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
from fastapi import APIRouter, HTTPException
|
from fastapi import APIRouter, HTTPException
|
||||||
@@ -11,14 +12,29 @@ from .utils import run_cmd
|
|||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
|
|
||||||
|
|
||||||
@router.get("/api/flake/attrs")
|
async def get_attrs(url: str) -> list[str]:
|
||||||
async def inspect_flake_attrs(url: str) -> FlakeAttrResponse:
|
|
||||||
cmd = nix_flake_show(url)
|
cmd = nix_flake_show(url)
|
||||||
stdout = await run_cmd(cmd)
|
stdout = await run_cmd(cmd)
|
||||||
data = json.loads(stdout)
|
|
||||||
nixos_configs = data["nixosConfigurations"]
|
data: dict[str, dict] = {}
|
||||||
|
try:
|
||||||
|
data = json.loads(stdout)
|
||||||
|
except JSONDecodeError:
|
||||||
|
raise HTTPException(status_code=422, detail="Could not load flake.")
|
||||||
|
|
||||||
|
nixos_configs = data.get("nixosConfigurations", {})
|
||||||
flake_attrs = list(nixos_configs.keys())
|
flake_attrs = list(nixos_configs.keys())
|
||||||
return FlakeAttrResponse(flake_attrs=flake_attrs)
|
|
||||||
|
if not flake_attrs:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=422, detail="No entry or no attribute: nixosConfigurations"
|
||||||
|
)
|
||||||
|
return flake_attrs
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/api/flake/attrs")
|
||||||
|
async def inspect_flake_attrs(url: str) -> FlakeAttrResponse:
|
||||||
|
return FlakeAttrResponse(flake_attrs=await get_attrs(url))
|
||||||
|
|
||||||
|
|
||||||
@router.get("/api/flake")
|
@router.get("/api/flake")
|
||||||
|
|||||||
@@ -3,9 +3,12 @@ import logging
|
|||||||
from typing import Annotated, Iterator
|
from typing import Annotated, Iterator
|
||||||
from uuid import UUID
|
from uuid import UUID
|
||||||
|
|
||||||
from fastapi import APIRouter, BackgroundTasks, Body
|
from fastapi import APIRouter, BackgroundTasks, Body, status
|
||||||
|
from fastapi.exceptions import HTTPException
|
||||||
from fastapi.responses import StreamingResponse
|
from fastapi.responses import StreamingResponse
|
||||||
|
|
||||||
|
from clan_cli.webui.routers.flake import get_attrs
|
||||||
|
|
||||||
from ...nix import nix_build, nix_eval
|
from ...nix import nix_build, nix_eval
|
||||||
from ..schemas import VmConfig, VmCreateResponse, VmInspectResponse, VmStatusResponse
|
from ..schemas import VmConfig, VmCreateResponse, VmInspectResponse, VmStatusResponse
|
||||||
from ..task_manager import BaseTask, get_task, register_task
|
from ..task_manager import BaseTask, get_task, register_task
|
||||||
@@ -107,5 +110,11 @@ async def get_vm_logs(uuid: UUID) -> StreamingResponse:
|
|||||||
async def create_vm(
|
async def create_vm(
|
||||||
vm: Annotated[VmConfig, Body()], background_tasks: BackgroundTasks
|
vm: Annotated[VmConfig, Body()], background_tasks: BackgroundTasks
|
||||||
) -> VmCreateResponse:
|
) -> VmCreateResponse:
|
||||||
|
flake_attrs = await get_attrs(vm.flake_url)
|
||||||
|
if vm.flake_attr not in flake_attrs:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=status.HTTP_400_BAD_REQUEST,
|
||||||
|
detail=f"Provided attribute '{vm.flake_attr}' does not exist.",
|
||||||
|
)
|
||||||
uuid = register_task(BuildVmTask, vm)
|
uuid = register_task(BuildVmTask, vm)
|
||||||
return VmCreateResponse(uuid=str(uuid))
|
return VmCreateResponse(uuid=str(uuid))
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ from api import TestClient
|
|||||||
|
|
||||||
|
|
||||||
@pytest.mark.impure
|
@pytest.mark.impure
|
||||||
def test_inspect(api: TestClient, test_flake_with_core: Path) -> None:
|
def test_inspect_ok(api: TestClient, test_flake_with_core: Path) -> None:
|
||||||
params = {"url": str(test_flake_with_core)}
|
params = {"url": str(test_flake_with_core)}
|
||||||
response = api.get(
|
response = api.get(
|
||||||
"/api/flake/attrs",
|
"/api/flake/attrs",
|
||||||
@@ -15,3 +15,16 @@ def test_inspect(api: TestClient, test_flake_with_core: Path) -> None:
|
|||||||
data = response.json()
|
data = response.json()
|
||||||
print("Data: ", data)
|
print("Data: ", data)
|
||||||
assert data.get("flake_attrs") == ["vm1"]
|
assert data.get("flake_attrs") == ["vm1"]
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.impure
|
||||||
|
def test_inspect_err(api: TestClient) -> None:
|
||||||
|
params = {"url": "flake-parts"}
|
||||||
|
response = api.get(
|
||||||
|
"/api/flake/attrs",
|
||||||
|
params=params,
|
||||||
|
)
|
||||||
|
assert response.status_code != 200, "Succeed to inspect vm but expected to fail"
|
||||||
|
data = response.json()
|
||||||
|
print("Data: ", data)
|
||||||
|
assert data.get("detail")
|
||||||
|
|||||||
Reference in New Issue
Block a user