Merge pull request 'Chore(api): narrow down get_machine' (#3863) from api-narrowing into main
Reviewed-on: https://git.clan.lol/clan/clan-core/pulls/3863
This commit is contained in:
@@ -69,7 +69,7 @@ def query_machines_by_tags(flake: Flake, tags: list[str]) -> dict[str, Machine]:
|
||||
|
||||
filtered_machines = {}
|
||||
for machine in machines.values():
|
||||
inv_machine = get_machine(machine)
|
||||
inv_machine = get_machine(machine.flake, machine.name)
|
||||
machine_tags = inv_machine.get("tags", [])
|
||||
if all(tag in machine_tags for tag in tags):
|
||||
filtered_machines[machine.name] = machine
|
||||
@@ -97,7 +97,7 @@ def extract_header(c: str) -> str:
|
||||
|
||||
@API.register
|
||||
def get_machine_details(machine: Machine) -> MachineDetails:
|
||||
machine_inv = get_machine(machine)
|
||||
machine_inv = get_machine(machine.flake, machine.name)
|
||||
hw_config = HardwareConfig.detect_type(machine)
|
||||
|
||||
machine_dir = specific_machine_dir(machine)
|
||||
|
||||
@@ -4,17 +4,24 @@ import sys
|
||||
import urllib.parse
|
||||
from enum import Enum
|
||||
from pathlib import Path
|
||||
from typing import TYPE_CHECKING
|
||||
from typing import TYPE_CHECKING, Protocol
|
||||
|
||||
from clan_lib.errors import ClanError
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from clan_lib.flake import Flake
|
||||
from clan_lib.machines.machines import Machine
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class MachineSpecProtocol(Protocol):
|
||||
@property
|
||||
def flake(self) -> "Flake": ...
|
||||
|
||||
@property
|
||||
def name(self) -> str: ...
|
||||
|
||||
|
||||
def get_clan_flake_toplevel_or_env() -> Path | None:
|
||||
if clan_dir := os.environ.get("CLAN_DIR"):
|
||||
return Path(clan_dir)
|
||||
@@ -145,7 +152,7 @@ def machines_dir(flake: "Flake") -> Path:
|
||||
return Path(store_path) / "machines"
|
||||
|
||||
|
||||
def specific_machine_dir(machine: "Machine") -> Path:
|
||||
def specific_machine_dir(machine: "MachineSpecProtocol") -> Path:
|
||||
return machines_dir(machine.flake) / machine.name
|
||||
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
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,13 +10,13 @@ from clan_lib.persist.util import set_value_by_path
|
||||
|
||||
|
||||
@API.register
|
||||
def get_machine(machine: Machine) -> InventoryMachine:
|
||||
inventory_store = InventoryStore(flake=machine.flake)
|
||||
def get_machine(flake: Flake, name: str) -> InventoryMachine:
|
||||
inventory_store = InventoryStore(flake=flake)
|
||||
inventory = inventory_store.read()
|
||||
|
||||
machine_inv = inventory.get("machines", {}).get(machine.name)
|
||||
machine_inv = inventory.get("machines", {}).get(name)
|
||||
if machine_inv is None:
|
||||
msg = f"Machine {machine.name} not found in inventory"
|
||||
msg = f"Machine {name} not found in inventory"
|
||||
raise ClanError(msg)
|
||||
|
||||
return InventoryMachine(**machine_inv)
|
||||
|
||||
Reference in New Issue
Block a user