From 7727ec91f4f04b5225c8831b4459eb263a90df70 Mon Sep 17 00:00:00 2001 From: jeremystretch Date: Fri, 13 Aug 2021 10:43:25 -0400 Subject: [PATCH] #6934: Correct prefix utilization and available IP reporting to account for child IP ranges --- docs/release-notes/version-3.0.md | 1 + netbox/ipam/models/ip.py | 5 ++++- netbox/ipam/tests/test_models.py | 10 +++++----- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/docs/release-notes/version-3.0.md b/docs/release-notes/version-3.0.md index 7261ed997..61966031b 100644 --- a/docs/release-notes/version-3.0.md +++ b/docs/release-notes/version-3.0.md @@ -15,6 +15,7 @@ * [#6846](https://github.com/netbox-community/netbox/issues/6846) - Form-driven REST API calls should use brief mode * [#6871](https://github.com/netbox-community/netbox/issues/6871) - Support dynamic tag types in GraphQL API * [#6894](https://github.com/netbox-community/netbox/issues/6894) - Fix available IP generation for prefix assigned to a VRF +* [#6934](https://github.com/netbox-community/netbox/issues/6934) - Correct prefix utilization and available IP reporting to account for child IP ranges * [#6953](https://github.com/netbox-community/netbox/issues/6953) - Remove change log tab from non-applicable object views --- diff --git a/netbox/ipam/models/ip.py b/netbox/ipam/models/ip.py index 162573633..6c1b2d439 100644 --- a/netbox/ipam/models/ip.py +++ b/netbox/ipam/models/ip.py @@ -435,7 +435,10 @@ class Prefix(PrimaryModel): prefix = netaddr.IPSet(self.prefix) child_ips = netaddr.IPSet([ip.address.ip for ip in self.get_child_ips()]) - available_ips = prefix - child_ips + child_ranges = netaddr.IPSet() + for iprange in self.get_child_ranges(): + child_ranges.add(iprange.range) + available_ips = prefix - child_ips - child_ranges # IPv6, pool, or IPv4 /31-/32 sets are fully usable if self.family == 6 or self.is_pool or (self.family == 4 and self.prefix.prefixlen >= 31): diff --git a/netbox/ipam/tests/test_models.py b/netbox/ipam/tests/test_models.py index e60ee76b7..f6130f1c1 100644 --- a/netbox/ipam/tests/test_models.py +++ b/netbox/ipam/tests/test_models.py @@ -142,17 +142,17 @@ class TestPrefix(TestCase): IPAddress(address=IPNetwork('10.0.0.3/26')), IPAddress(address=IPNetwork('10.0.0.5/26')), IPAddress(address=IPNetwork('10.0.0.7/26')), - IPAddress(address=IPNetwork('10.0.0.9/26')), - IPAddress(address=IPNetwork('10.0.0.11/26')), - IPAddress(address=IPNetwork('10.0.0.13/26')), )) + IPRange.objects.create( + start_address=IPNetwork('10.0.0.9/26'), + end_address=IPNetwork('10.0.0.12/26') + ) missing_ips = IPSet([ '10.0.0.2/32', '10.0.0.4/32', '10.0.0.6/32', '10.0.0.8/32', - '10.0.0.10/32', - '10.0.0.12/32', + '10.0.0.13/32', '10.0.0.14/32', ]) available_ips = parent_prefix.get_available_ips()