Coverage for ipam/management/commands/rebuild_prefixes.py: 0%
17 statements
« prev ^ index » next coverage.py v7.15.2, created at 2026-10-10 18:35 +0000
« prev ^ index » next coverage.py v7.15.2, created at 2026-10-10 18:35 +0000
1from django.core.management.base import BaseCommand
3from ipam.models import VRF, Prefix
4from ipam.utils import rebuild_prefixes
5from utilities.querysets import chunked_update
8class Command(BaseCommand):
9 help = "Rebuild the prefix hierarchy (depth and children counts)"
11 def handle(self, *model_names, **options):
12 self.stdout.write(f'Rebuilding {Prefix.objects.count()} prefixes...')
14 # Reset existing counts
15 chunked_update(Prefix.objects.all(), _depth=0, _children=0)
17 # Rebuild the global table
18 global_count = Prefix.objects.filter(vrf__isnull=True).count()
19 self.stdout.write(f'Global: {global_count} prefixes...')
20 rebuild_prefixes(None)
22 # Rebuild each VRF
23 for vrf in VRF.objects.all():
24 vrf_count = Prefix.objects.filter(vrf=vrf).count()
25 self.stdout.write(f'VRF {vrf}: {vrf_count} prefixes...')
26 rebuild_prefixes(vrf.pk)
28 self.stdout.write(self.style.SUCCESS('Finished.'))