From 24b3674983acd4ae19d6933c057eef37d2dd2809 Mon Sep 17 00:00:00 2001 From: Johannes Kirschbauer Date: Wed, 17 Jul 2024 11:32:36 +0200 Subject: [PATCH] Inventory: fix dataclasses discrepancy once more --- inventory.json | 44 ++++++++++---------- pkgs/clan-cli/clan_cli/inventory/__init__.py | 32 +++++++------- 2 files changed, 38 insertions(+), 38 deletions(-) diff --git a/inventory.json b/inventory.json index 1ce2a79bf..f984b63a9 100644 --- a/inventory.json +++ b/inventory.json @@ -7,13 +7,13 @@ "machines": { "test-inventory-machine": { "name": "foo", + "deploy": { + "targetHost": null + }, "description": "A nice thing", "icon": "./path/to/icon.png", "tags": ["1", "2", "3"], - "system": "x86_64-linux", - "deploy": { - "targetHost": "root@remote.com" - } + "system": "x86_64-linux" } }, "services": { @@ -26,14 +26,16 @@ }, "roles": { "default": { - "machines": ["test-inventory-machine"], - "tags": [], "config": { "packages": ["vim"] }, - "imports": [] + "imports": [], + "machines": ["test-inventory-machine"], + "tags": [] } }, + "config": {}, + "imports": [], "machines": { "test-inventory-machine": { "config": { @@ -41,9 +43,7 @@ }, "imports": [] } - }, - "config": null, - "imports": [] + } }, "browsing": { "meta": { @@ -53,12 +53,14 @@ }, "roles": { "default": { + "config": {}, + "imports": [], "machines": ["test-inventory-machine"], - "tags": [], - "config": null, - "imports": [] + "tags": [] } }, + "config": {}, + "imports": [], "machines": { "test-inventory-machine": { "config": { @@ -66,9 +68,7 @@ }, "imports": [] } - }, - "config": null, - "imports": [] + } } }, "single-disk": { @@ -80,12 +80,14 @@ }, "roles": { "default": { + "config": {}, + "imports": [], "machines": ["test-inventory-machine"], - "tags": [], - "config": null, - "imports": [] + "tags": [] } }, + "config": {}, + "imports": [], "machines": { "test-inventory-machine": { "config": { @@ -93,9 +95,7 @@ }, "imports": [] } - }, - "config": null, - "imports": [] + } } } } diff --git a/pkgs/clan-cli/clan_cli/inventory/__init__.py b/pkgs/clan-cli/clan_cli/inventory/__init__.py index e1247034d..95778b7e8 100644 --- a/pkgs/clan-cli/clan_cli/inventory/__init__.py +++ b/pkgs/clan-cli/clan_cli/inventory/__init__.py @@ -1,3 +1,5 @@ +# ruff: noqa: N815 +# ruff: noqa: N806 import json from dataclasses import asdict, dataclass, field, is_dataclass from pathlib import Path @@ -41,7 +43,7 @@ class DeploymentInfo: Deployment information for a machine. """ - target_host: str | None = None + targetHost: str | None = None @dataclass @@ -58,30 +60,28 @@ class Machine: """ name: str + deploy: DeploymentInfo = field(default_factory=DeploymentInfo) description: str | None = None icon: str | None = None tags: list[str] = field(default_factory=list) system: Literal["x86_64-linux"] | str | None = None - deploy: DeploymentInfo | None = None - @staticmethod - def from_dict(d: dict[str, Any]) -> "Machine": + def from_dict(data: dict[str, Any]) -> "Machine": + targetHost = data.get("deploy", {}).get("targetHost", None) return Machine( - name=d["name"], - description=d.get("description", None), - icon=d.get("icon", None), - tags=d.get("tags", []), - system=d.get("system", None), - deploy=DeploymentInfo( - target_host=d.get("deploy", {}).get("targetHost", None) - ), + name=data["name"], + description=data.get("description", None), + icon=data.get("icon", None), + tags=data.get("tags", []), + system=data.get("system", None), + deploy=DeploymentInfo(targetHost), ) @dataclass class MachineServiceConfig: - config: dict[str, Any] | None = None + config: dict[str, Any] = field(default_factory=dict) imports: list[str] = field(default_factory=list) @@ -94,7 +94,7 @@ class ServiceMeta: @dataclass class Role: - config: dict[str, Any] | None = None + config: dict[str, Any] = field(default_factory=dict) imports: list[str] = field(default_factory=list) machines: list[str] = field(default_factory=list) tags: list[str] = field(default_factory=list) @@ -104,7 +104,7 @@ class Role: class Service: meta: ServiceMeta roles: dict[str, Role] - config: dict[str, Any] | None = None + config: dict[str, Any] = field(default_factory=dict) imports: list[str] = field(default_factory=list) machines: dict[str, MachineServiceConfig] = field(default_factory=dict) @@ -121,7 +121,7 @@ class Service: if d.get("machines") else {} ), - config=d.get("config", None), + config=d.get("config", {}), imports=d.get("imports", []), )