Container Prefixes do not report available prefixes correctly #9081

Closed
opened 2025-12-29 20:45:09 +01:00 by adam · 3 comments
Owner

Originally created by @diabolic53 on GitHub (Jan 11, 2024).

Deployment Type

Self-hosted

NetBox Version

3.3.7

Python Version

3.8

Steps to Reproduce

Create Container prefix with Global VRF to be used as a shared pool for multiple tenants
add child prefix and assign a VRF by using the api to get the first available prefix in the container
/api/ipam/prefixes/<>/available-prefixes/
image

add a second prefix and assign a second VRF by using the api to get the first available prefix in the container
/api/ipam/prefixes/<>/available-prefixes/
image

Expected Behavior

get the next available prefix from the container
image

Observed Behavior

get the same prefix as the first prefix assigned due to VRF not being accounted for when calculating prefixes

Originally created by @diabolic53 on GitHub (Jan 11, 2024). ### Deployment Type Self-hosted ### NetBox Version 3.3.7 ### Python Version 3.8 ### Steps to Reproduce Create Container prefix with Global VRF to be used as a shared pool for multiple tenants add child prefix and assign a VRF by using the api to get the first available prefix in the container /api/ipam/prefixes/<<ID>>/available-prefixes/ ![image](https://github.com/netbox-community/netbox/assets/48087756/a6b5cf8c-5815-46b2-86c9-33423ae47367) add a second prefix and assign a second VRF by using the api to get the first available prefix in the container /api/ipam/prefixes/<<ID>>/available-prefixes/ ![image](https://github.com/netbox-community/netbox/assets/48087756/ddd53787-677b-418a-bf0f-dc671ba25ff1) ### Expected Behavior get the next available prefix from the container ![image](https://github.com/netbox-community/netbox/assets/48087756/bb7ba2c6-f226-4288-80e1-698d38dfaa0e) ### Observed Behavior get the same prefix as the first prefix assigned due to VRF not being accounted for when calculating prefixes
adam added the type: bug label 2025-12-29 20:45:09 +01:00
adam closed this issue 2025-12-29 20:45:09 +01:00
Author
Owner

@diabolic53 commented on GitHub (Jan 11, 2024):

i've noticed this was introduced in this fix
7ba0b420f1 (diff-b12db49b7b31202cc34428c1dbdf443ae9abb72547c0445e10497cee67698888)

i would propose a different tweak as container prefixes should not be treated the same as Active prefixes.

def get_available_prefixes(self):
        """
        Return all available prefixes within this Aggregate or Prefix as an IPSet.
        """
        params = {
            'prefix__net_contained': str(self.prefix)
        }
        if hasattr(self, 'status') and self.status.value != "container":
            if hasattr(self, 'vrf'):
                 params['vrf'] = self.vrf

            child_prefixes = Prefix.objects.filter(**params).values_list('prefix', flat=True)
            return netaddr.IPSet(self.prefix) - netaddr.IPSet(child_prefixes)
        else:
            prefix = netaddr.IPSet(self.prefix)
            child_prefixes = netaddr.IPSet([child.prefix for child in self.get_child_prefixes()])
            available_prefixes = prefix - child_prefixes

            return available_prefixes
@diabolic53 commented on GitHub (Jan 11, 2024): i've noticed this was introduced in this fix https://github.com/netbox-community/netbox/pull/10114/commits/7ba0b420f181cffac86a9de384a1ec9d8a9a07dd#diff-b12db49b7b31202cc34428c1dbdf443ae9abb72547c0445e10497cee67698888 i would propose a different tweak as container prefixes should not be treated the same as Active prefixes. ``` def get_available_prefixes(self): """ Return all available prefixes within this Aggregate or Prefix as an IPSet. """ params = { 'prefix__net_contained': str(self.prefix) } if hasattr(self, 'status') and self.status.value != "container": if hasattr(self, 'vrf'): params['vrf'] = self.vrf child_prefixes = Prefix.objects.filter(**params).values_list('prefix', flat=True) return netaddr.IPSet(self.prefix) - netaddr.IPSet(child_prefixes) else: prefix = netaddr.IPSet(self.prefix) child_prefixes = netaddr.IPSet([child.prefix for child in self.get_child_prefixes()]) available_prefixes = prefix - child_prefixes return available_prefixes ```
Author
Owner

@jeffgdotorg commented on GitHub (Jan 11, 2024):

Thank you for reporting a problem in NetBox. Thank you also for using IPv6 in your network 😀

Because you've opened against a very old version, and we've fixed many bugs since then, I'm closing this issue, but we encourage you to re-report it if you're still able to reproduce the problem in a recent (3.6.x or 3.7.x) version. If the problem persists in current releases, we are interested in working through it with you in a new issue.

When you create the new issue, please provide concrete values rather than <> placeholders in the textual part of the steps to reproduce. Screenshots are nice, but text is searchable.

@jeffgdotorg commented on GitHub (Jan 11, 2024): Thank you for reporting a problem in NetBox. Thank you also for using IPv6 in your network 😀 Because you've opened against a very old version, and we've fixed many bugs since then, I'm closing this issue, **but we encourage you to re-report it** if you're still able to reproduce the problem in a recent (3.6.x or 3.7.x) version. If the problem persists in current releases, we are interested in working through it with you in a new issue. When you create the new issue, please provide _concrete_ values rather than `<>` placeholders in the textual part of the *steps to reproduce*. Screenshots are nice, but text is searchable.
Author
Owner

@diabolic53 commented on GitHub (Jan 11, 2024):

Can i re-open it and change it to 3.7 ? the same issue is present in the latest version too ...

@diabolic53 commented on GitHub (Jan 11, 2024): Can i re-open it and change it to 3.7 ? the same issue is present in the latest version too ...
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#9081