diff --git a/napalm_device_types/dhcp.py b/napalm_device_types/dhcp.py index efd1d4b..12c194c 100644 --- a/napalm_device_types/dhcp.py +++ b/napalm_device_types/dhcp.py @@ -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: """