api/machines: move configuration data into subattribute

This helps to make room for 'instance_refs'
And potentially other metadata that we want to compute and expose
This commit is contained in:
Johannes Kirschbauer
2025-09-01 14:42:12 +02:00
parent a06940e981
commit 727d4e70ae
7 changed files with 54 additions and 30 deletions

View File

@@ -3,7 +3,7 @@ import { A } from "@solidjs/router";
import { Accordion } from "@kobalte/core/accordion";
import Icon from "../Icon/Icon";
import { Typography } from "@/src/components/Typography/Typography";
import { For, Show, useContext } from "solid-js";
import { For, Show } from "solid-js";
import { MachineStatus } from "@/src/components/MachineStatus/MachineStatus";
import { buildMachinePath, useClanURI } from "@/src/hooks/clan";
import { useMachineStateQuery } from "@/src/hooks/queries";
@@ -136,7 +136,7 @@ export const SidebarBody = (props: SidebarProps) => {
<MachineRoute
clanURI={clanURI}
machineID={id}
name={machine.name || id}
name={machine.data.name || id}
serviceCount={0}
/>
)}

View File

@@ -26,13 +26,19 @@ const mockFetcher: Fetcher = <K extends OperationNames>(
const resultData: Partial<ResultDataMap> = {
list_machines: {
pandora: {
name: "pandora",
data: {
name: "pandora",
},
},
enceladus: {
name: "enceladus",
data: {
name: "enceladus",
},
},
dione: {
name: "dione",
data: {
name: "dione",
},
},
},
};

View File

@@ -62,20 +62,28 @@ const mockFetcher: Fetcher = <K extends OperationNames>(
},
list_machines: {
jon: {
name: "jon",
tags: ["all", "nixos", "tag1"],
data: {
name: "jon",
tags: ["all", "nixos", "tag1"],
},
},
sara: {
name: "sara",
tags: ["all", "darwin", "tag2"],
data: {
name: "sara",
tags: ["all", "darwin", "tag2"],
},
},
kyra: {
name: "kyra",
tags: ["all", "darwin", "tag2"],
data: {
name: "kyra",
tags: ["all", "darwin", "tag2"],
},
},
leila: {
name: "leila",
tags: ["all", "darwin", "tag2"],
data: {
name: "leila",
tags: ["all", "darwin", "tag2"],
},
},
},
list_tags: {

View File

@@ -120,7 +120,7 @@ const SelectService = () => {
label: t,
type: "tag" as const,
members: Object.entries(machinesQuery.data || {})
.filter(([_, m]) => m.tags?.includes(t))
.filter(([_, m]) => m.data.tags?.includes(t))
.map(([k]) => k),
};
});
@@ -206,7 +206,7 @@ const useOptions = (tagsQuery: TagsQuery, machinesQuery: MachinesQuery) =>
label: tag,
value: "t_" + tag,
members: Object.entries(machines)
.filter(([_, v]) => v.tags?.includes(tag))
.filter(([_, v]) => v.data.tags?.includes(tag))
.map(([k]) => k),
}));

View File

@@ -103,7 +103,9 @@ def get_machines_for_update(
machines_to_update = list(
filter(
requires_explicit_update,
instantiate_inventory_to_machines(flake, machines_with_tags).values(),
instantiate_inventory_to_machines(
flake, {name: m.data for name, m in machines_with_tags.items()}
).values(),
),
)
# all machines that are in the clan but not included in the update list
@@ -128,13 +130,13 @@ def get_machines_for_update(
machines_to_update = []
valid_names = validate_machine_names(explicit_names, flake)
for name in valid_names:
inventory_machine = machines_with_tags.get(name)
if not inventory_machine:
machine = machines_with_tags.get(name)
if not machine:
msg = "This is an internal bug"
raise ClanError(msg)
machines_to_update.append(
Machine.from_inventory(name, flake, inventory_machine),
Machine.from_inventory(name, flake, machine.data),
)
return machines_to_update

View File

@@ -41,28 +41,34 @@ class MachineState(TypedDict):
# add more info later when retrieving remote state
@dataclass
class MachineResponse:
data: InventoryMachine
# Reference the installed service instances
instance_refs: list[str] = field(default_factory=list)
@API.register
def list_machines(
flake: Flake,
opts: ListOptions | None = None,
) -> dict[str, InventoryMachine]:
) -> dict[str, MachineResponse]:
"""List machines of a clan"""
inventory_store = InventoryStore(flake=flake)
inventory = inventory_store.read()
machines = inventory.get("machines", {})
raw_machines = inventory.get("machines", {})
if opts and opts.filter.tags is not None:
filtered_machines = {}
for machine_name, machine in machines.items():
res: dict[str, MachineResponse] = {}
for machine_name, machine in raw_machines.items():
if opts and opts.filter.tags is not None:
machine_tags = machine.get("tags", [])
if all(ft in machine_tags for ft in opts.filter.tags):
filtered_machines[machine_name] = machine
res[machine_name] = MachineResponse(data=InventoryMachine(**machine))
else:
res[machine_name] = MachineResponse(data=InventoryMachine(**machine))
return filtered_machines
return machines
return res
@API.register

View File

@@ -30,7 +30,9 @@ def list_full_machines(flake: Flake) -> dict[str, Machine]:
"""Like `list_machines`, but returns a full 'machine' instance for each machine."""
machines = list_machines(flake)
return instantiate_inventory_to_machines(flake, machines)
return instantiate_inventory_to_machines(
flake, {name: m.data for name, m in machines.items()}
)
@dataclass