api/machines: better input/output validation
Also move contents tof 'config' to the top-level
This commit is contained in:
@@ -2,7 +2,7 @@ import logging
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
from pydantic import AnyUrl, BaseModel, validator
|
||||
from pydantic import AnyUrl, BaseModel, Extra, validator
|
||||
|
||||
from ..dirs import clan_data_dir, clan_flakes_dir
|
||||
from ..flakes.create import DEFAULT_URL
|
||||
@@ -29,3 +29,12 @@ class ClanFlakePath(BaseModel):
|
||||
|
||||
class FlakeCreateInput(ClanFlakePath):
|
||||
url: AnyUrl = DEFAULT_URL
|
||||
|
||||
|
||||
class MachineConfig(BaseModel):
|
||||
clanImports: list[str] = [] # noqa: N815
|
||||
clan: dict = {}
|
||||
|
||||
# allow extra fields to cover the full spectrum of a nixos config
|
||||
class Config:
|
||||
extra = Extra.allow
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
from enum import Enum
|
||||
from typing import Dict, List
|
||||
|
||||
from pydantic import BaseModel, Field
|
||||
from pydantic import BaseModel, Extra, Field
|
||||
|
||||
from ..async_cmd import CmdOut
|
||||
from ..task_manager import TaskStatus
|
||||
@@ -36,7 +36,12 @@ class MachineResponse(BaseModel):
|
||||
|
||||
|
||||
class ConfigResponse(BaseModel):
|
||||
config: dict
|
||||
clanImports: list[str] = [] # noqa: N815
|
||||
clan: dict = {}
|
||||
|
||||
# allow extra fields to cover the full spectrum of a nixos config
|
||||
class Config:
|
||||
extra = Extra.allow
|
||||
|
||||
|
||||
class SchemaResponse(BaseModel):
|
||||
|
||||
@@ -4,6 +4,8 @@ from typing import Annotated
|
||||
|
||||
from fastapi import APIRouter, Body
|
||||
|
||||
from clan_cli.webui.api_inputs import MachineConfig
|
||||
|
||||
from ...config.machine import (
|
||||
config_for_machine,
|
||||
schema_for_machine,
|
||||
@@ -55,15 +57,15 @@ async def get_machine(flake_name: FlakeName, name: str) -> MachineResponse:
|
||||
@router.get("/api/{flake_name}/machines/{name}/config", tags=[Tags.machine])
|
||||
async def get_machine_config(flake_name: FlakeName, name: str) -> ConfigResponse:
|
||||
config = config_for_machine(flake_name, name)
|
||||
return ConfigResponse(config=config)
|
||||
return ConfigResponse(**config)
|
||||
|
||||
|
||||
@router.put("/api/{flake_name}/machines/{name}/config", tags=[Tags.machine])
|
||||
async def set_machine_config(
|
||||
flake_name: FlakeName, name: str, config: Annotated[dict, Body()]
|
||||
) -> ConfigResponse:
|
||||
set_config_for_machine(flake_name, name, config)
|
||||
return ConfigResponse(config=config)
|
||||
flake_name: FlakeName, name: str, config: Annotated[MachineConfig, Body()]
|
||||
) -> None:
|
||||
conf = dict(config)
|
||||
set_config_for_machine(flake_name, name, conf)
|
||||
|
||||
|
||||
@router.get("/api/{flake_name}/machines/{name}/schema", tags=[Tags.machine])
|
||||
|
||||
Reference in New Issue
Block a user