From 84017776ecdc9f9f69b36fbb7947da36a93f9799 Mon Sep 17 00:00:00 2001 From: jeremystretch Date: Thu, 27 May 2021 10:03:00 -0400 Subject: [PATCH] Fix handling of duplicate prefixes --- netbox/ipam/utils.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/netbox/ipam/utils.py b/netbox/ipam/utils.py index 953a4a468..18a470253 100644 --- a/netbox/ipam/utils.py +++ b/netbox/ipam/utils.py @@ -105,7 +105,7 @@ def rebuild_prefixes(vrf): for n in stack: n['children'] += 1 stack.append({ - 'pk': prefix['pk'], + 'pk': [prefix['pk']], 'prefix': prefix['prefix'], 'children': 0, }) @@ -123,18 +123,17 @@ def rebuild_prefixes(vrf): # Handle duplicate prefixes elif stack[-1]['prefix'] == p['prefix']: - update_queue.append( - Prefix(pk=p['pk'], _depth=len(stack) - 1, _children=stack[-1]['children']) - ) + stack[-1]['pk'].append(p['pk']) # If this is a sibling or parent of the most recent prefix, pop nodes from the # stack until we reach a parent prefix (or the root) else: while stack and not contains(stack[-1]['prefix'], p['prefix']): node = stack.pop() - update_queue.append( - Prefix(pk=node['pk'], _depth=len(stack), _children=node['children']) - ) + for pk in node['pk']: + update_queue.append( + Prefix(pk=pk, _depth=len(stack), _children=node['children']) + ) push_to_stack(p) # Flush the update queue once it reaches 100 Prefixes @@ -145,9 +144,10 @@ def rebuild_prefixes(vrf): # Clear out any prefixes remaining in the stack while stack: node = stack.pop() - update_queue.append( - Prefix(pk=node['pk'], _depth=len(stack), _children=node['children']) - ) + for pk in node['pk']: + update_queue.append( + Prefix(pk=pk, _depth=len(stack), _children=node['children']) + ) # Final flush of any remaining Prefixes Prefix.objects.bulk_update(update_queue, ['_depth', '_children'])