webui: fixes for machines endpoint

- fix machines settings file location
- raise 404 if machine doesn't exist
- improve machine api test
This commit is contained in:
DavHau
2023-08-26 14:17:25 +02:00
parent 2b2584a0ba
commit 8c7ee25dc9
4 changed files with 45 additions and 17 deletions

View File

@@ -4,31 +4,36 @@ import sys
from pathlib import Path
from typing import Optional
from fastapi import HTTPException
from clan_cli.dirs import get_clan_flake_toplevel
from clan_cli.machines.folders import machine_folder, machine_settings_file
def config_for_machine(machine_name: str, flake: Optional[Path] = None) -> dict:
# find the flake root
if flake is None:
flake = get_clan_flake_toplevel()
def config_for_machine(machine_name: str) -> dict:
# read the config from a json file located at {flake}/machines/{machine_name}.json
config_path = flake / "machines" / f"{machine_name}.json"
if not config_path.exists():
if not machine_folder(machine_name).exists():
raise HTTPException(
status_code=404,
detail=f"Machine {machine_name} not found. Create the machine first`",
)
settings_path = machine_settings_file(machine_name)
if not settings_path.exists():
return {}
with open(config_path) as f:
with open(settings_path) as f:
return json.load(f)
def set_config_for_machine(
machine_name: str, config: dict, flake: Optional[Path] = None
) -> None:
# find the flake root
if flake is None:
flake = get_clan_flake_toplevel()
def set_config_for_machine(machine_name: str, config: dict) -> None:
# write the config to a json file located at {flake}/machines/{machine_name}.json
config_path = flake / "machines" / f"{machine_name}.json"
config_path.parent.mkdir(parents=True, exist_ok=True)
with open(config_path, "w") as f:
if not machine_folder(machine_name).exists():
raise HTTPException(
status_code=404,
detail=f"Machine {machine_name} not found. Create the machine first`",
)
settings_path = machine_settings_file(machine_name)
settings_path.parent.mkdir(parents=True, exist_ok=True)
with open(settings_path, "w") as f:
json.dump(config, f)

View File

@@ -9,3 +9,7 @@ def machines_folder() -> Path:
def machine_folder(machine: str) -> Path:
return machines_folder() / machine
def machine_settings_file(machine: str) -> Path:
return machine_folder(machine) / "settings.json"

View File

@@ -22,9 +22,28 @@ def test_machines(api: TestClient, clan_flake: Path) -> None:
def test_configure_machine(api: TestClient, machine_flake: Path) -> None:
# ensure error 404 if machine does not exist when accessing the config
response = api.get("/api/machines/machine1/config")
assert response.status_code == 404
# ensure error 404 if machine does not exist when writing to the config
response = api.put("/api/machines/machine1/config", json={})
assert response.status_code == 404
# create the machine
response = api.post("/api/machines", json={"name": "machine1"})
assert response.status_code == 201
# ensure an empty config is returned by default for a new machine
response = api.get("/api/machines/machine1/config")
assert response.status_code == 200
assert response.json() == {"config": dict()}
assert response.json() == {"config": {}}
# get jsonschema for machine
response = api.get("/api/machines/machine1/schema")
assert response.status_code == 200
json_response = response.json()
assert "schema" in json_response and "properties" in json_response["schema"]
# set some config
response = api.put(