From 8c7ee25dc965c54275058e315a23f1e4bc98b73d Mon Sep 17 00:00:00 2001 From: DavHau Date: Sat, 26 Aug 2023 14:17:25 +0200 Subject: [PATCH] webui: fixes for machines endpoint - fix machines settings file location - raise 404 if machine doesn't exist - improve machine api test --- pkgs/clan-cli/clan_cli/config/machine.py | 37 +++++++++++-------- pkgs/clan-cli/clan_cli/machines/folders.py | 4 ++ pkgs/clan-cli/tests/test_api_machines.py | 21 ++++++++++- ...fig_machine.py => test_machines_config.py} | 0 4 files changed, 45 insertions(+), 17 deletions(-) rename pkgs/clan-cli/tests/{test_config_machine.py => test_machines_config.py} (100%) diff --git a/pkgs/clan-cli/clan_cli/config/machine.py b/pkgs/clan-cli/clan_cli/config/machine.py index 37187f1aa..068606324 100644 --- a/pkgs/clan-cli/clan_cli/config/machine.py +++ b/pkgs/clan-cli/clan_cli/config/machine.py @@ -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) diff --git a/pkgs/clan-cli/clan_cli/machines/folders.py b/pkgs/clan-cli/clan_cli/machines/folders.py index b7a5af2a3..a7e010ec2 100644 --- a/pkgs/clan-cli/clan_cli/machines/folders.py +++ b/pkgs/clan-cli/clan_cli/machines/folders.py @@ -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" diff --git a/pkgs/clan-cli/tests/test_api_machines.py b/pkgs/clan-cli/tests/test_api_machines.py index 30edb0993..2551fbfe2 100644 --- a/pkgs/clan-cli/tests/test_api_machines.py +++ b/pkgs/clan-cli/tests/test_api_machines.py @@ -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( diff --git a/pkgs/clan-cli/tests/test_config_machine.py b/pkgs/clan-cli/tests/test_machines_config.py similarity index 100% rename from pkgs/clan-cli/tests/test_config_machine.py rename to pkgs/clan-cli/tests/test_machines_config.py