diff --git a/CHANGELOG.md b/CHANGELOG.md index 21c21a1..6d667e6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,17 @@ recorded here; their history is the git log. ## [Unreleased] +## [4.35.1] - 2026-11-11 + +### Fixed +- `apply_dhcp_reservationset()` no longer stops at the first reservation the device refuses. + One refused write (on OPNsense, an address in a subnet Kea does not manage) ended the run + before the commit: every later reservation went unwritten, and the earlier ones stayed + saved but inactive, where the next diff found them configured and never committed them. A + refused change now gets a `[failed] -> : ` line, the others are written, + any non-empty diff commits, and `DhcpApplyError` (a `RuntimeError`, exported) is raised + after the commit with how many of how many were refused (NetOrk/netork#2381). + ## [4.35.0] - 2026-11-11 ### Added diff --git a/napalm_device_types/__init__.py b/napalm_device_types/__init__.py index cce669d..3bd5a45 100644 --- a/napalm_device_types/__init__.py +++ b/napalm_device_types/__init__.py @@ -127,7 +127,7 @@ from napalm_device_types.container_engine import ( ContainerEngineMixin, ContainerEngineUnavailable, ) -from napalm_device_types.dhcp import DhcpServerMixin, normalize_cidr, normalize_mac +from napalm_device_types.dhcp import DhcpApplyError, DhcpServerMixin, normalize_cidr, normalize_mac from napalm_device_types.dns_forward import DnsForwardMixin from napalm_device_types.firewall import FirewallDriver from napalm_device_types.firewall_rules import FirewallRuleMixin @@ -404,6 +404,7 @@ __all__ = [ "DhcpHaDict", "DhcpHaPeerDict", "DeviceTypeDriver", + "DhcpApplyError", "DhcpServerMixin", "FingerprintRule", "rules_max_weight", diff --git a/napalm_device_types/dhcp.py b/napalm_device_types/dhcp.py index f1f72ef..12c194c 100644 --- a/napalm_device_types/dhcp.py +++ b/napalm_device_types/dhcp.py @@ -58,6 +58,19 @@ _DHCP_SUBNET_COMPARE_FIELDS = ( _HEX_ONLY = re.compile(r"[^0-9a-f]") +class DhcpApplyError(RuntimeError): + """Some reservations of an applied 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. + """ + + def __init__(self, failed: int, attempted: int) -> None: + self.failed = failed + self.attempted = attempted + super().__init__(f"{failed} of {attempted} DHCP reservation change(s) were refused") + + def normalize_mac(mac: str | None) -> str: """Reduces a MAC address to lowercase colon-separated form. @@ -298,29 +311,55 @@ class DhcpServerMixin: committing reloads the DHCP daemon and drops in-flight requests, which is too high a price for a no-op run. + A change the device refuses (say, an address in a subnet its DHCP server + does not manage) 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. + :param desired: The complete desired reservation 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_reservations(desired) - - for reservation in diff["add"]: - self.apply_dhcp_reservation(reservation) - yield f"[add] {reservation['mac']} -> {reservation['ip']}" - - for entry in diff["update"]: - self.apply_dhcp_reservation(entry["reservation"], uuid=entry["uuid"]) - fields = ", ".join(entry["changed_fields"]) - yield ( - f"[update] {entry['reservation']['mac']} -> {entry['reservation']['ip']} ({fields})" - ) - - if not diff["add"] and not diff["update"]: + attempted = len(diff["add"]) + len(diff["update"]) + if not attempted: yield "[commit] no changes" return + added = updated = refused = 0 + + for reservation in diff["add"]: + line, ok = self._write_reservation(reservation, None, "[add]") + added, refused = added + ok, refused + (not ok) + yield line + + for entry in diff["update"]: + fields = ", ".join(entry["changed_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 + # no later diff would ever commit them. self.commit_dhcp_reservations() - 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) + + 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: """ diff --git a/pyproject.toml b/pyproject.toml index 0f7fa07..eb7fa23 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "napalm-device-types" -version = "4.35.0" +version = "4.35.1" description = "Abstract device-type base classes for NAPALM drivers" readme = "README.md" requires-python = ">=3.12" diff --git a/tests/test_dhcp_diff_apply.py b/tests/test_dhcp_diff_apply.py index 4aaef44..3440862 100644 --- a/tests/test_dhcp_diff_apply.py +++ b/tests/test_dhcp_diff_apply.py @@ -14,7 +14,7 @@ from typing import Any import pytest from napalm_device_types import DhcpServerMixin, FirewallDriver, ResidentialGatewayDriver -from napalm_device_types.dhcp import normalize_mac +from napalm_device_types.dhcp import DhcpApplyError, normalize_mac from napalm_device_types.models import DhcpReservationDict @@ -209,6 +209,95 @@ class TestApplyReservationSet: assert lines == ["[commit] no changes"] +class _RefusingDhcpServer(_FakeDhcpServer): + """A server that refuses the reservations for the IPs in *refused*, as Kea does + for one whose subnet it does not manage.""" + + def __init__(self, live: list[DhcpReservationDict] | None = None, *, refused: set[str]) -> None: + super().__init__(live) + self.refused = refused + + def apply_dhcp_reservation( + self, reservation: DhcpReservationDict, *, uuid: str | None = None + ) -> dict[str, Any]: + if reservation["ip"] in self.refused: + raise ValueError(f"No Kea-managed subnet contains {reservation['ip']}") + return super().apply_dhcp_reservation(reservation, uuid=uuid) + + +def _three() -> list[DhcpReservationDict]: + return [ + _reservation(mac="aa:bb:cc:dd:ee:01", ip="10.10.20.50"), + _reservation(mac="aa:bb:cc:dd:ee:02", ip="10.10.99.5", subnet=""), + _reservation(mac="aa:bb:cc:dd:ee:03", ip="10.10.20.52"), + ] + + +class TestApplyWithARefusedReservation: + """One reservation the server refuses must not hold back the others, and what was + written must take effect: a server that saved a write and was never committed shows + it as configured, so the next apply finds nothing to do and never commits it.""" + + def test_the_others_are_still_written(self): + driver = _RefusingDhcpServer(refused={"10.10.99.5"}) + + with pytest.raises(DhcpApplyError): + list(driver.apply_dhcp_reservationset(_three())) + + assert [entry["reservation"]["ip"] for entry in driver.applied] == [ + "10.10.20.50", + "10.10.20.52", + ] + + def test_what_was_written_is_committed(self): + driver = _RefusingDhcpServer(refused={"10.10.99.5"}) + + with pytest.raises(DhcpApplyError): + list(driver.apply_dhcp_reservationset(_three())) + + assert driver.commits == 1 + + def test_a_refused_set_still_commits(self): + # An earlier apply that stopped before its commit left saved writes behind; a + # run whose every write is refused commits too, so those take effect. + driver = _RefusingDhcpServer(refused={"10.10.99.5"}) + + with pytest.raises(DhcpApplyError): + list(driver.apply_dhcp_reservationset([_three()[1]])) + + assert driver.commits == 1 + + def test_progress_names_the_refused_one_and_why(self): + driver = _RefusingDhcpServer(refused={"10.10.99.5"}) + lines: list[str] = [] + + with pytest.raises(DhcpApplyError) as raised: + for line in driver.apply_dhcp_reservationset(_three()): + lines.append(line) + + failed = [line for line in lines if line.startswith("[failed]")] + assert failed == [ + "[failed] aa:bb:cc:dd:ee:02 -> 10.10.99.5: No Kea-managed subnet contains 10.10.99.5" + ] + assert lines[-1] == "[commit] applied 2 add(s), 0 update(s)" + assert "1 of 3" in str(raised.value) + + def test_a_refused_update_is_reported_like_an_add(self): + driver = _RefusingDhcpServer( + [_reservation(uuid="u1", ip="10.10.20.9")], refused={"10.10.20.50"} + ) + + with pytest.raises(DhcpApplyError, match="1 of 1"): + list(driver.apply_dhcp_reservationset([_reservation()])) + + assert driver.applied == [] + assert driver.commits == 1 + + def test_the_error_is_a_runtime_error(self): + # Callers that catch RuntimeError for a failed apply keep working. + assert issubclass(DhcpApplyError, RuntimeError) + + class TestKeysThatCannotNameOneReservation: def test_a_live_reservation_without_mac_is_never_matched(self): driver = _FakeDhcpServer([_reservation(uuid="b", mac="")])