Merge pull request 'API: refactor into resource oriented names' (#4223) from api-cleanup into main

Reviewed-on: https://git.clan.lol/clan/clan-core/pulls/4223
This commit is contained in:
hsjobeki
2025-07-06 19:11:31 +00:00
20 changed files with 116 additions and 197 deletions

View File

@@ -10,7 +10,7 @@ from clan_lib.api.modules import Frontmatter, extract_frontmatter
from clan_lib.dirs import TemplateType, clan_templates
from clan_lib.errors import ClanError
from clan_lib.git import commit_file
from clan_lib.machines.hardware import HardwareConfig, show_machine_hardware_config
from clan_lib.machines.hardware import HardwareConfig, get_machine_hardware_config
from clan_lib.machines.machines import Machine
log = logging.getLogger(__name__)
@@ -137,7 +137,7 @@ def set_machine_disk_schema(
Set the disk placeholders of the template
"""
# Assert the hw-config must exist before setting the disk
hw_config = show_machine_hardware_config(machine)
hw_config = get_machine_hardware_config(machine)
hw_config_path = hw_config.config_path(machine)
if not hw_config_path.exists():

View File

@@ -10,14 +10,3 @@ Which is an abstraction over the inventory
Interacting with 'clan_lib.inventory' is NOT recommended and will be removed
"""
from clan_lib.api import API
from clan_lib.flake import Flake
from clan_lib.persist.inventory_store import InventorySnapshot, InventoryStore
@API.register
def get_inventory(flake: Flake) -> InventorySnapshot:
inventory_store = InventoryStore(flake)
inventory = inventory_store.read()
return inventory

View File

@@ -9,7 +9,7 @@ from clan_cli.secrets.secrets import (
list_secrets,
)
from clan_lib import inventory
from clan_lib.persist.inventory_store import InventoryStore
from clan_lib.api import API
from clan_lib.dirs import specific_machine_dir
from clan_lib.machines.machines import Machine
@@ -19,7 +19,7 @@ log = logging.getLogger(__name__)
@API.register
def delete_machine(machine: Machine) -> None:
inventory_store = inventory.InventoryStore(machine.flake)
inventory_store = InventoryStore(machine.flake)
try:
inventory_store.delete(
{f"machines.{machine.name}"},

View File

@@ -3,6 +3,7 @@ import logging
from dataclasses import dataclass
from enum import Enum
from pathlib import Path
from typing import TypedDict
from clan_lib.api import API
from clan_lib.cmd import RunOpts, run
@@ -40,19 +41,7 @@ class HardwareConfig(Enum):
return HardwareConfig.NONE
@API.register
def show_machine_hardware_config(machine: Machine) -> HardwareConfig:
"""
Show hardware information for a machine returns None if none exist.
"""
return HardwareConfig.detect_type(machine)
@API.register
def show_machine_hardware_platform(machine: Machine) -> str | None:
"""
Show hardware information for a machine returns None if none exist.
"""
def get_machine_target_platform(machine: Machine) -> str | None:
config = nix_config()
system = config["system"]
cmd = nix_eval(
@@ -132,7 +121,7 @@ def generate_machine_hardware_info(
f"machines/{opts.machine}/{hw_file.name}: update hardware configuration",
)
try:
show_machine_hardware_platform(opts.machine)
get_machine_target_platform(opts.machine)
if backup_file:
backup_file.unlink(missing_ok=True)
except ClanCmdError as e:
@@ -150,3 +139,29 @@ def generate_machine_hardware_info(
) from e
return opts.backend
def get_machine_hardware_config(machine: Machine) -> HardwareConfig:
"""
Detect and return the full hardware configuration for the given machine.
Returns:
HardwareConfig: Structured hardware information, or None if unavailable.
"""
return HardwareConfig.detect_type(machine)
class MachineHardwareBrief(TypedDict):
hardware_config: HardwareConfig
platform: str | None
@API.register
def describe_machine_hardware(machine: Machine) -> MachineHardwareBrief:
"""
Return a high-level summary of hardware config and platform type.
"""
return {
"hardware_config": get_machine_hardware_config(machine),
"platform": get_machine_target_platform(machine),
}

View File

@@ -129,7 +129,7 @@ class Machine:
return self.flake.path
def target_host(self) -> Remote:
remote = get_host(self.name, self.flake, field="targetHost")
remote = get_machine_host(self.name, self.flake, field="targetHost")
if remote is None:
msg = f"'targetHost' is not set for machine '{self.name}'"
raise ClanError(
@@ -144,7 +144,7 @@ class Machine:
The host where the machine is built and deployed from.
Can be the same as the target host.
"""
remote = get_host(self.name, self.flake, field="buildHost")
remote = get_machine_host(self.name, self.flake, field="buildHost")
if remote:
data = remote.data
@@ -176,7 +176,7 @@ class RemoteSource:
@API.register
def get_host(
def get_machine_host(
name: str, flake: Flake, field: Literal["targetHost", "buildHost"]
) -> RemoteSource | None:
"""

View File

@@ -14,7 +14,7 @@ from clan_cli.machines.create import create_machine
from clan_cli.secrets.key import generate_key
from clan_cli.secrets.sops import maybe_get_admin_public_keys
from clan_cli.secrets.users import add_user
from clan_cli.vars.generate import generate_vars_for_machine, get_generators_closure
from clan_cli.vars.generate import create_machine_vars, get_machine_generators
from clan_lib.api.disk import hw_main_disk_options, set_machine_disk_schema
from clan_lib.api.modules import list_modules
@@ -22,7 +22,7 @@ from clan_lib.cmd import RunOpts, run
from clan_lib.dirs import specific_machine_dir
from clan_lib.errors import ClanError
from clan_lib.flake import Flake
from clan_lib.inventory import InventoryStore
from clan_lib.persist.inventory_store import InventoryStore
from clan_lib.machines.machines import Machine
from clan_lib.nix import nix_command
from clan_lib.nix_models.clan import (
@@ -221,7 +221,7 @@ def test_clan_create_api(
# Invalidate cache because of new inventory
clan_dir_flake.invalidate_cache()
generators = get_generators_closure(machine.name, machine.flake.path)
generators = get_machine_generators(machine.name, machine.flake.path)
all_prompt_values = {}
for generator in generators:
prompt_values = {}
@@ -234,7 +234,7 @@ def test_clan_create_api(
raise ClanError(msg)
all_prompt_values[generator.name] = prompt_values
generate_vars_for_machine(
create_machine_vars(
machine_name=machine.name,
base_dir=machine.flake.path,
generators=[gen.name for gen in generators],