Skip to content

Commit

Permalink
Work on netbox-community#2562, creating a hierarchy of prefixes
Browse files Browse the repository at this point in the history
  • Loading branch information
DanSheps committed Sep 17, 2019
1 parent 0601619 commit 96169d4
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion netbox/ipam/querysets.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,33 @@
from django.db.models import QuerySet

from netaddr import IPNetwork
from .constants import PREFIX_STATUS_CONTAINER

class PrefixQuerySet(QuerySet):

def build_hierachy(self, limit=None):
"""
Iterate through a Queryset of Prefixes and build the parent-child relationships.
"""
queryset = self
for p in queryset:
p.parent = None
p.depth = 0

for p in queryset:
p.children = []
for c in queryset:
if (p.prefix != c.prefix and p.family == c.family and ((p.vrf is None and
p.status == PREFIX_STATUS_CONTAINER) or p.vrf == c.vrf) and c.prefix.cidr in p.prefix.cidr):
parent = c.parent
if parent is None or parent.prefix in p.prefix:
c.parent = p
c.depth = p.depth + 1
p.children.append(c)

return list(filter(lambda p: p.parent == None, self))



def annotate_depth(self, limit=None):
"""
Iterate through a QuerySet of Prefixes and annotate the hierarchical level of each. While it would be preferable
Expand Down

0 comments on commit 96169d4

Please sign in to comment.