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:
@@ -4,31 +4,36 @@ import sys
|
|||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
|
||||||
|
from fastapi import HTTPException
|
||||||
|
|
||||||
from clan_cli.dirs import get_clan_flake_toplevel
|
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:
|
def config_for_machine(machine_name: str) -> dict:
|
||||||
# find the flake root
|
|
||||||
if flake is None:
|
|
||||||
flake = get_clan_flake_toplevel()
|
|
||||||
# read the config from a json file located at {flake}/machines/{machine_name}.json
|
# read the config from a json file located at {flake}/machines/{machine_name}.json
|
||||||
config_path = flake / "machines" / f"{machine_name}.json"
|
if not machine_folder(machine_name).exists():
|
||||||
if not config_path.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 {}
|
return {}
|
||||||
with open(config_path) as f:
|
with open(settings_path) as f:
|
||||||
return json.load(f)
|
return json.load(f)
|
||||||
|
|
||||||
|
|
||||||
def set_config_for_machine(
|
def set_config_for_machine(machine_name: str, config: dict) -> None:
|
||||||
machine_name: str, config: dict, flake: Optional[Path] = None
|
|
||||||
) -> None:
|
|
||||||
# find the flake root
|
|
||||||
if flake is None:
|
|
||||||
flake = get_clan_flake_toplevel()
|
|
||||||
# write the config to a json file located at {flake}/machines/{machine_name}.json
|
# write the config to a json file located at {flake}/machines/{machine_name}.json
|
||||||
config_path = flake / "machines" / f"{machine_name}.json"
|
if not machine_folder(machine_name).exists():
|
||||||
config_path.parent.mkdir(parents=True, exist_ok=True)
|
raise HTTPException(
|
||||||
with open(config_path, "w") as f:
|
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)
|
json.dump(config, f)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -9,3 +9,7 @@ def machines_folder() -> Path:
|
|||||||
|
|
||||||
def machine_folder(machine: str) -> Path:
|
def machine_folder(machine: str) -> Path:
|
||||||
return machines_folder() / machine
|
return machines_folder() / machine
|
||||||
|
|
||||||
|
|
||||||
|
def machine_settings_file(machine: str) -> Path:
|
||||||
|
return machine_folder(machine) / "settings.json"
|
||||||
|
|||||||
@@ -22,9 +22,28 @@ def test_machines(api: TestClient, clan_flake: Path) -> None:
|
|||||||
|
|
||||||
|
|
||||||
def test_configure_machine(api: TestClient, machine_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")
|
response = api.get("/api/machines/machine1/config")
|
||||||
assert response.status_code == 200
|
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
|
# set some config
|
||||||
response = api.put(
|
response = api.put(
|
||||||
|
|||||||
Reference in New Issue
Block a user