fix(dhcp): a refused subnet no longer stops the rest of the set
driver / standard (pull_request) Successful in 16s
driver / lint (pull_request) Successful in 16s
driver / types (pull_request) Successful in 1m19s
driver / test (3.12) (pull_request) Successful in 1m32s
driver / test (3.13) (pull_request) Successful in 1m16s
driver / lowest (pull_request) Successful in 41s
driver / build (pull_request) Successful in 35s
driver / security (pull_request) Successful in 1m13s
CI / driver (pull_request) Successful in 3m15s
driver / publish (pull_request) Skipped

apply_dhcp_subnetset called apply_dhcp_subnet for each add and update
and committed only at the end, so one refused write raised before the
commit: the subnets written ahead of it stayed saved but inactive, and
as the device reports them as configured, no later diff committed them.

It now behaves like apply_dhcp_reservationset since 4.35.1: a refused
subnet gets a [failed] line, the others are written, any non-empty diff
is committed, then DhcpApplyError is raised. DhcpApplyError takes an
optional kind ("reservation" by default) for its message.

Closes #113
This commit is contained in:
christianmanivong committed 2026-11-12 19:19:03 +01:00
1 parent 9dfb5596bc
commit 65991d0930
3 files changed
+145 -17

No files matched your search

+48 -17
View File
@@ -66,16 +66,18 @@ _SWAP_NOTE = (
class DhcpApplyError(RuntimeError):
"""Some reservations of an applied set were refused by the device.
"""Some changes of an applied reservation or subnet set were refused by the device.
Raised after the rest were written and committed: ``failed`` of ``attempted``
changes did not go through, and each was named in a ``[failed]`` progress line.
``kind`` is what was applied: ``"reservation"`` or ``"subnet"``.
"""
def __init__(self, failed: int, attempted: int) -> None:
def __init__(self, failed: int, attempted: int, kind: str = "reservation") -> None:
self.failed = failed
self.attempted = attempted
super().__init__(f"{failed} of {attempted} DHCP reservation change(s) were refused")
self.kind = kind
super().__init__(f"{failed} of {attempted} DHCP {kind} change(s) were refused")
def normalize_mac(mac: str | None) -> str:
@@ -455,24 +457,53 @@ class DhcpServerMixin:
As with ``apply_dhcp_reservationset``, an empty diff does **not**
commit: reconfiguring the DHCP daemon is too expensive for a no-op.
A subnet the device refuses (say, one that overlaps a subnet it serves)
does not stop the others: it gets a ``[failed]`` line, the rest are
written and committed, and ``DhcpApplyError`` is raised at the end. Any
non-empty diff commits, even one whose every write was refused, so that
writes an earlier, interrupted run saved take effect (#113).
:param desired: The complete desired subnet set.
:yields: Progress lines, one per applied add/update, plus a final
commit line.
:yields: Progress lines, one per applied, updated or refused change,
plus a final commit line.
:raises DhcpApplyError: after the commit, when a change was refused.
"""
diff = self.diff_dhcp_subnets(desired)
for subnet in diff["add"]:
self.apply_dhcp_subnet(subnet)
yield f"[add] {subnet['subnet']}"
for entry in diff["update"]:
self.apply_dhcp_subnet(entry["subnet"], uuid=entry["uuid"])
fields = ", ".join(entry["changed_fields"])
yield f"[update] {entry['subnet']['subnet']} ({fields})"
if not diff["add"] and not diff["update"]:
attempted = len(diff["add"]) + len(diff["update"])
if not attempted:
yield "[commit] no changes"
return
changes: list[tuple[DhcpSubnetDict, str | None, str]] = [
(subnet, None, "") for subnet in diff["add"]
]
changes += [
(e["subnet"], e["uuid"], ", ".join(e["changed_fields"])) for e in diff["update"]
]
added = updated = refused = 0
for subnet, uuid, fields in changes:
line, ok = self._write_subnet(subnet, uuid, fields)
refused += not ok
added += ok and uuid is None
updated += ok and uuid is not None
yield line
# Committed even when every write was refused; see apply_dhcp_reservationset.
self.commit_dhcp_subnets()
yield (f"[commit] applied {len(diff['add'])} add(s), {len(diff['update'])} update(s)")
yield f"[commit] applied {added} add(s), {updated} update(s)"
if refused:
raise DhcpApplyError(refused, attempted, "subnet")
def _write_subnet(
self, subnet: DhcpSubnetDict, uuid: str | None, fields: str
) -> tuple[str, bool]:
"""Write one subnet; its progress line, and whether the device took it."""
try:
self.apply_dhcp_subnet(subnet, uuid=uuid)
except Exception as exc:
return f"[failed] {subnet['subnet']}: {exc}", False
if uuid is None:
return f"[add] {subnet['subnet']}", True
return f"[update] {subnet['subnet']} ({fields})", True