Merge remote-tracking branch 'origin/main' into feat/time-sync-dhcp-ntp-2380
driver / lint (pull_request) Successful in 15s
driver / standard (pull_request) Successful in 19s
driver / types (pull_request) Successful in 1m34s
driver / test (3.13) (pull_request) Successful in 1m32s
driver / test (3.12) (pull_request) Successful in 1m52s
driver / lowest (pull_request) Successful in 1m3s
driver / build (pull_request) Successful in 48s
driver / security (pull_request) Successful in 1m26s
CI / driver (pull_request) Successful in 3m17s
driver / publish (pull_request) Skipped

# Conflicts:
#	CHANGELOG.md
#	pyproject.toml
This commit is contained in:
christianmanivong committed 2026-11-11 22:21:34 +01:00
commit bc47220318
4 files changed
+158 -18

No files matched your search

+2 -1
View File
@@ -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",
+55 -16
View File
@@ -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:
"""