refactor(dhcp): one reservation's write in a helper of its own
driver / standard (pull_request) Successful in 17s
driver / lint (pull_request) Successful in 15s
driver / types (pull_request) Successful in 1m6s
driver / test (3.12) (pull_request) Successful in 1m19s
driver / test (3.13) (pull_request) Successful in 1m10s
driver / lowest (pull_request) Successful in 42s
driver / build (pull_request) Successful in 37s
driver / security (pull_request) Successful in 1m9s
CI / driver (pull_request) Successful in 3m0s
driver / publish (pull_request) Skipped

apply_dhcp_reservationset grew to 59 lines with the per-write handling;
the quality ratchet allows no new function that long.

Refs NetOrk/netork#2381
This commit is contained in:
christianmanivong committed 2026-11-11 22:08:50 +01:00
1 parent c74173523a
commit 8516f2a1ee
1 file changed
+20 -21
+20 -21
View File
@@ -328,30 +328,18 @@ class DhcpServerMixin:
yield "[commit] no changes"
return
added = updated = 0
failed: list[str] = []
added = updated = refused = 0
for reservation in diff["add"]:
try:
self.apply_dhcp_reservation(reservation)
except Exception as exc:
failed.append(f"[failed] {reservation['mac']} -> {reservation['ip']}: {exc}")
yield failed[-1]
continue
added += 1
yield f"[add] {reservation['mac']} -> {reservation['ip']}"
line, ok = self._write_reservation(reservation, None, "[add]")
added, refused = added + ok, refused + (not ok)
yield line
for entry in diff["update"]:
target = entry["reservation"]
try:
self.apply_dhcp_reservation(target, uuid=entry["uuid"])
except Exception as exc:
failed.append(f"[failed] {target['mac']} -> {target['ip']}: {exc}")
yield failed[-1]
continue
updated += 1
fields = ", ".join(entry["changed_fields"])
yield f"[update] {target['mac']} -> {target['ip']} ({fields})"
line, ok = self._write_reservation(entry["reservation"], entry["uuid"], "[update]")
updated, refused = updated + ok, refused + (not ok)
yield f"{line} ({fields})" if ok else line
# Committed even when every write was refused: a run that stopped before its
# commit left saved writes behind, which the device reports as configured, so
@@ -359,8 +347,19 @@ class DhcpServerMixin:
self.commit_dhcp_reservations()
yield f"[commit] applied {added} add(s), {updated} update(s)"
if failed:
raise DhcpApplyError(len(failed), attempted)
if refused:
raise DhcpApplyError(refused, attempted)
def _write_reservation(
self, reservation: DhcpReservationDict, uuid: str | None, tag: str
) -> tuple[str, bool]:
"""Write one reservation; its progress line, and whether the device took it."""
target = f"{reservation['mac']} -> {reservation['ip']}"
try:
self.apply_dhcp_reservation(reservation, uuid=uuid)
except Exception as exc:
return f"[failed] {target}: {exc}", False
return f"{tag} {target}", True
def diff_dhcp_subnets(self, desired: list[DhcpSubnetDict]) -> DhcpSubnetDiffDict:
"""