Fix importing IP ranges

IPRange.save() computes its size from start_address.ip, but the importer assigned the archived addresses as plain strings, so every archive containing IP ranges failed with "'str' object has no attribute 'ip'". Convert values of NetBox IP fields with the field's to_python(). This also affects NetBox 4.6.

IP ranges also had no natural key, so importing into an instance that already contained the range created a duplicate. Match them by (vrf, start_address, end_address).

Verified with a real export and repeated imports on NetBox 4.7.2; the version stays 0.3.14 because Netbox-Utilities only enables its integration for known versions.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
MrBlakeandClaude Opus 5.5 committed 2026-10-13 15:21:11 +02:00
1 parent 0fdde044e1
commit 44149acf16
2 files changed
+32

No files matched your search

+8
View File
@@ -54,6 +54,7 @@ EXPLICIT_IDENTITIES = {
"dcim.inventoryitem": ("device", "parent", "name"),
"ipam.prefix": ("vrf", "prefix"),
"ipam.ipaddress": ("vrf", "address"),
"ipam.iprange": ("vrf", "start_address", "end_address"),
"ipam.vlan": ("group", "vid"),
"circuits.circuit": ("provider", "cid"),
"virtualization.virtualmachine": ("cluster", "tenant", "name"),
@@ -169,9 +170,16 @@ def _coerce_model_field_value(field, value):
and hasattr(base_field, "base_field")
):
return [_coerce_range_value(base_field, item) for item in value]
if isinstance(value, str) and _is_netbox_ip_field(field):
# IPRange.save() computes its size from start_address.ip, so plain strings are not enough.
return field.to_python(value)
return value
def _is_netbox_ip_field(field):
return any(cls.__name__ == "BaseIPField" for cls in type(field).__mro__)
def _identity_candidates(model):
explicit = EXPLICIT_IDENTITIES.get(model._meta.label_lower)
if explicit:
+24
View File
@@ -54,3 +54,27 @@ def test_range_like_strings_remain_unchanged_for_normal_fields():
field = object()
assert _coerce_model_field_value(field, "[1, 56)") == "[1, 56)"
class BaseIPField:
@staticmethod
def to_python(value):
return ("parsed", value)
class IPAddressField(BaseIPField):
pass
class CharField:
@staticmethod
def to_python(value):
raise AssertionError("plain fields must not be converted")
def test_netbox_ip_field_values_are_converted_for_model_save():
assert _coerce_model_field_value(IPAddressField(), "192.0.2.1/24") == ("parsed", "192.0.2.1/24")
def test_other_string_values_are_left_unchanged():
assert _coerce_model_field_value(CharField(), "192.0.2.1/24") == "192.0.2.1/24"