Coverage for ipam/models/vrfs.py: 91%

31 statements  

« prev     ^ index     » next       coverage.py v7.15.2, created at 2026-10-10 18:35 +0000

1from django.db import models 

2from django.utils.translation import gettext_lazy as _ 

3 

4from ipam.constants import * 

5from netbox.models import PrimaryModel 

6 

7__all__ = ( 

8 'VRF', 

9 'RouteTarget', 

10) 

11 

12 

13class VRF(PrimaryModel): 

14 """ 

15 A virtual routing and forwarding (VRF) table represents a discrete layer three forwarding domain (e.g. a routing 

16 table). Prefixes and IPAddresses can optionally be assigned to VRFs. (Prefixes and IPAddresses not assigned to a VRF 

17 are said to exist in the "global" table.) 

18 """ 

19 name = models.CharField( 

20 verbose_name=_('name'), 

21 max_length=100, 

22 db_collation="natural_sort" 

23 ) 

24 rd = models.CharField( 

25 max_length=VRF_RD_MAX_LENGTH, 

26 unique=True, 

27 blank=True, 

28 null=True, 

29 verbose_name=_('route distinguisher'), 

30 help_text=_('Unique route distinguisher (as defined in RFC 4364)') 

31 ) 

32 tenant = models.ForeignKey( 

33 to='tenancy.Tenant', 

34 on_delete=models.PROTECT, 

35 related_name='vrfs', 

36 blank=True, 

37 null=True 

38 ) 

39 enforce_unique = models.BooleanField( 

40 default=True, 

41 verbose_name=_('enforce unique space'), 

42 help_text=_('Prevent duplicate prefixes/IP addresses within this VRF') 

43 ) 

44 import_targets = models.ManyToManyField( 

45 to='ipam.RouteTarget', 

46 related_name='importing_vrfs', 

47 blank=True 

48 ) 

49 export_targets = models.ManyToManyField( 

50 to='ipam.RouteTarget', 

51 related_name='exporting_vrfs', 

52 blank=True 

53 ) 

54 

55 clone_fields = ( 

56 'tenant', 'enforce_unique', 'description', 

57 ) 

58 

59 class Meta: 

60 ordering = ('name', 'rd', 'pk') # (name, rd) may be non-unique 

61 indexes = ( 

62 models.Index(fields=('name', 'rd', 'id')), # Default ordering 

63 ) 

64 verbose_name = _('VRF') 

65 verbose_name_plural = _('VRFs') 

66 

67 def __str__(self): 

68 if self.rd: 68 ↛ 69line 68 didn't jump to line 69 because the condition on line 68 was never true

69 return f'{self.name} ({self.rd})' 

70 return self.name 

71 

72 

73class RouteTarget(PrimaryModel): 

74 """ 

75 A BGP extended community used to control the redistribution of routes among VRFs, as defined in RFC 4364. 

76 """ 

77 name = models.CharField( 

78 verbose_name=_('name'), 

79 max_length=VRF_RD_MAX_LENGTH, # Same format options as VRF RD (RFC 4360 section 4) 

80 unique=True, 

81 help_text=_('Route target value (formatted in accordance with RFC 4360)'), 

82 db_collation="natural_sort" 

83 ) 

84 tenant = models.ForeignKey( 

85 to='tenancy.Tenant', 

86 on_delete=models.PROTECT, 

87 related_name='route_targets', 

88 blank=True, 

89 null=True 

90 ) 

91 

92 class Meta: 

93 ordering = ['name'] 

94 verbose_name = _('route target') 

95 verbose_name_plural = _('route targets') 

96 

97 def __str__(self): 

98 return self.name