mirror of
https://github.com/netbox-community/netbox.git
synced 2024-05-10 07:54:54 +00:00
Introduce rebuild_prefixes management command
This commit is contained in:
27
netbox/ipam/management/commands/rebuild_prefixes.py
Normal file
27
netbox/ipam/management/commands/rebuild_prefixes.py
Normal file
@@ -0,0 +1,27 @@
|
||||
from django.core.management.base import BaseCommand
|
||||
|
||||
from ipam.models import Prefix, VRF
|
||||
from ipam.utils import rebuild_prefixes
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = "Rebuild the prefix hierarchy (depth and children counts)"
|
||||
|
||||
def handle(self, *model_names, **options):
|
||||
self.stdout.write(f'Rebuilding {Prefix.objects.count()} prefixes...')
|
||||
|
||||
# Reset existing counts
|
||||
Prefix.objects.update(_depth=0, _children=0)
|
||||
|
||||
# Rebuild the global table
|
||||
global_count = Prefix.objects.filter(vrf__isnull=True).count()
|
||||
self.stdout.write(f'Global: {global_count} prefixes...')
|
||||
rebuild_prefixes(None)
|
||||
|
||||
# Rebuild each VRF
|
||||
for vrf in VRF.objects.all():
|
||||
vrf_count = Prefix.objects.filter(vrf=vrf).count()
|
||||
self.stdout.write(f'VRF {vrf}: {vrf_count} prefixes...')
|
||||
rebuild_prefixes(vrf)
|
||||
|
||||
self.stdout.write(self.style.SUCCESS('Finished.'))
|
Reference in New Issue
Block a user