Merge pull request 'Refactor(api/update_machine): rename to set_machine; use name, flake' (#3899) from api-narrowing into main

Reviewed-on: https://git.clan.lol/clan/clan-core/pulls/3899
This commit is contained in:
hsjobeki
2025-06-09 11:55:28 +00:00
15 changed files with 129 additions and 119 deletions

View File

@@ -0,0 +1,71 @@
import json
from pathlib import Path
from urllib.parse import urlparse
from clan_lib.api import API
from clan_lib.cmd import run
from clan_lib.errors import ClanCmdError, ClanError
from clan_lib.flake import Flake
from clan_lib.nix import nix_eval
from clan_lib.nix_models.clan import InventoryMeta as Meta
@API.register
def show_clan_meta(flake: Flake) -> Meta:
if flake.is_local and not flake.path.exists():
msg = f"Path {flake} does not exist"
raise ClanError(msg, description="clan directory does not exist")
cmd = nix_eval(
[
f"{flake}#clanInternals.inventory.meta",
"--json",
]
)
res = "{}"
try:
proc = run(cmd)
res = proc.stdout.strip()
except ClanCmdError as e:
msg = "Evaluation failed on meta attribute"
raise ClanError(
msg,
location=f"show_clan {flake}",
description=str(e.cmd),
) from e
clan_meta = json.loads(res)
# Check if icon is a URL such as http:// or https://
# Check if icon is an relative path
# All other schemas such as file://, ftp:// are not yet supported.
icon_path: str | None = None
if meta_icon := clan_meta.get("icon"):
scheme = urlparse(meta_icon).scheme
if scheme in ["http", "https"]:
icon_path = meta_icon
elif scheme in [""]:
if Path(meta_icon).is_absolute():
msg = "Invalid absolute path"
raise ClanError(
msg,
location=f"show_clan {flake}",
description="Icon path must be a URL or a relative path",
)
icon_path = str((flake.path / meta_icon).resolve())
else:
msg = "Invalid schema"
raise ClanError(
msg,
location=f"show_clan {flake}",
description="Icon path must be a URL or a relative path",
)
return Meta(
{
"name": clan_meta.get("name"),
"description": clan_meta.get("description"),
"icon": icon_path if icon_path else "",
}
)

View File

@@ -0,0 +1,23 @@
from dataclasses import dataclass
from clan_lib.api import API
from clan_lib.flake import Flake
from clan_lib.nix_models.clan import InventoryMeta as Meta
from clan_lib.persist.inventory_store import InventorySnapshot, InventoryStore
from clan_lib.persist.util import set_value_by_path
@dataclass
class UpdateOptions:
flake: Flake
meta: Meta
@API.register
def update_clan_meta(options: UpdateOptions) -> InventorySnapshot:
inventory_store = InventoryStore(options.flake)
inventory = inventory_store.read()
set_value_by_path(inventory, "meta", options.meta)
inventory_store.write(inventory, message="Update clan metadata")
return inventory

View File

@@ -1,7 +1,8 @@
from dataclasses import dataclass
from clan_lib.api import API
from clan_lib.errors import ClanError
from clan_lib.flake.flake import Flake
from clan_lib.machines.machines import Machine
from clan_lib.nix_models.clan import (
InventoryMachine,
)
@@ -9,6 +10,18 @@ from clan_lib.persist.inventory_store import InventoryStore
from clan_lib.persist.util import set_value_by_path
@API.register
def list_machines(flake: Flake) -> dict[str, InventoryMachine]:
"""
List machines in the inventory for the UI.
"""
inventory_store = InventoryStore(flake=flake)
inventory = inventory_store.read()
machines = inventory.get("machines", {})
return machines
@API.register
def get_machine(flake: Flake, name: str) -> InventoryMachine:
inventory_store = InventoryStore(flake=flake)
@@ -22,9 +35,21 @@ def get_machine(flake: Flake, name: str) -> InventoryMachine:
return InventoryMachine(**machine_inv)
# TODO: remove this machine, once the Machine class is refactored
# We added this now, to allow for dispatching actions. To require only 'name' and 'flake' of a machine.
@dataclass(frozen=True)
class MachineID:
name: str
flake: Flake
@API.register
def update_machine(machine: Machine, update: InventoryMachine) -> None:
def set_machine(machine: MachineID, update: InventoryMachine) -> None:
"""
Update the machine information in the inventory.
"""
assert machine.name == update.get("name", machine.name), "Machine name mismatch"
inventory_store = InventoryStore(flake=machine.flake)
inventory = inventory_store.read()