persist/writeability: expose is writeable key helper

This commit is contained in:
Johannes Kirschbauer
2025-07-27 00:01:36 +02:00
parent c2e84f11af
commit 414e412e7e

View File

@@ -200,6 +200,28 @@ def parent_is_dict(key: str, data: dict[str, Any]) -> bool:
return False
def is_writeable_key(
key: str,
writeables: dict[str, set[str]],
) -> bool:
"""
Recursively check if a key is writeable.
key "machines.machine1.deploy.targetHost" is specified but writeability is only defined for "machines"
We pop the last key and check if the parent key is writeable/non-writeable.
"""
remaining = key.split(".")
while remaining:
if ".".join(remaining) in writeables["writeable"]:
return True
if ".".join(remaining) in writeables["non_writeable"]:
return False
remaining.pop()
msg = f"Cannot determine writeability for key '{key}'"
raise ClanError(msg, description="F001")
def calc_patches(
persisted: dict[str, Any],
update: dict[str, Any],
@@ -225,24 +247,6 @@ def calc_patches(
data_all_updated = flatten_data(update)
data_dyn = flatten_data(persisted)
def is_writeable_key(key: str) -> bool:
"""
Recursively check if a key is writeable.
key "machines.machine1.deploy.targetHost" is specified but writeability is only defined for "machines"
We pop the last key and check if the parent key is writeable/non-writeable.
"""
remaining = key.split(".")
while remaining:
if ".".join(remaining) in writeables["writeable"]:
return True
if ".".join(remaining) in writeables["non_writeable"]:
return False
remaining.pop()
msg = f"Cannot determine writeability for key '{key}'"
raise ClanError(msg, description="F001")
all_keys = set(data_all) | set(data_all_updated)
patchset = {}
@@ -256,7 +260,7 @@ def calc_patches(
# Some kind of change
if old != new:
# If there is a change, check if the key is writeable
if not is_writeable_key(key):
if not is_writeable_key(key, writeables):
msg = f"Key '{key}' is not writeable. It seems its value is statically defined in nix."
raise ClanError(msg)