diff --git a/netbox/ipam/models/__init__.py b/netbox/ipam/models/__init__.py index d13ee9076..1d890dee4 100644 --- a/netbox/ipam/models/__init__.py +++ b/netbox/ipam/models/__init__.py @@ -1,4 +1,5 @@ # Ensure that VRFs are imported before IPs/prefixes so dumpdata & loaddata work correctly +from .asns import * from .fhrp import * from .vrfs import * from .ip import * diff --git a/netbox/ipam/models/asns.py b/netbox/ipam/models/asns.py new file mode 100644 index 000000000..3d8a8e3f7 --- /dev/null +++ b/netbox/ipam/models/asns.py @@ -0,0 +1,69 @@ +from django.db import models +from django.urls import reverse +from django.utils.translation import gettext as _ + +from dcim.fields import ASNField +from netbox.models import PrimaryModel + +__all__ = ( + 'ASN', +) + + +class ASN(PrimaryModel): + """ + An autonomous system (AS) number is typically used to represent an independent routing domain. A site can have + one or more ASNs assigned to it. + """ + asn = ASNField( + unique=True, + verbose_name='ASN', + help_text=_('32-bit autonomous system number') + ) + rir = models.ForeignKey( + to='ipam.RIR', + on_delete=models.PROTECT, + related_name='asns', + verbose_name='RIR' + ) + tenant = models.ForeignKey( + to='tenancy.Tenant', + on_delete=models.PROTECT, + related_name='asns', + blank=True, + null=True + ) + + prerequisite_models = ( + 'ipam.RIR', + ) + + class Meta: + ordering = ['asn'] + verbose_name = 'ASN' + verbose_name_plural = 'ASNs' + + def __str__(self): + return f'AS{self.asn_with_asdot}' + + def get_absolute_url(self): + return reverse('ipam:asn', args=[self.pk]) + + @property + def asn_asdot(self): + """ + Return ASDOT notation for AS numbers greater than 16 bits. + """ + if self.asn > 65535: + return f'{self.asn // 65536}.{self.asn % 65536}' + return self.asn + + @property + def asn_with_asdot(self): + """ + Return both plain and ASDOT notation, where applicable. + """ + if self.asn > 65535: + return f'{self.asn} ({self.asn // 65536}.{self.asn % 65536})' + else: + return self.asn diff --git a/netbox/ipam/models/ip.py b/netbox/ipam/models/ip.py index e8bf13375..3463b9486 100644 --- a/netbox/ipam/models/ip.py +++ b/netbox/ipam/models/ip.py @@ -8,7 +8,6 @@ from django.urls import reverse from django.utils.functional import cached_property from django.utils.translation import gettext as _ -from dcim.fields import ASNField from ipam.choices import * from ipam.constants import * from ipam.fields import IPNetworkField, IPAddressField @@ -20,7 +19,6 @@ from netbox.models import OrganizationalModel, PrimaryModel __all__ = ( 'Aggregate', - 'ASN', 'IPAddress', 'IPRange', 'Prefix', @@ -74,65 +72,6 @@ class RIR(OrganizationalModel): return reverse('ipam:rir', args=[self.pk]) -class ASN(PrimaryModel): - """ - An autonomous system (AS) number is typically used to represent an independent routing domain. A site can have - one or more ASNs assigned to it. - """ - asn = ASNField( - unique=True, - verbose_name='ASN', - help_text=_('32-bit autonomous system number') - ) - rir = models.ForeignKey( - to='ipam.RIR', - on_delete=models.PROTECT, - related_name='asns', - verbose_name='RIR' - ) - tenant = models.ForeignKey( - to='tenancy.Tenant', - on_delete=models.PROTECT, - related_name='asns', - blank=True, - null=True - ) - - prerequisite_models = ( - 'ipam.RIR', - ) - - class Meta: - ordering = ['asn'] - verbose_name = 'ASN' - verbose_name_plural = 'ASNs' - - def __str__(self): - return f'AS{self.asn_with_asdot}' - - def get_absolute_url(self): - return reverse('ipam:asn', args=[self.pk]) - - @property - def asn_asdot(self): - """ - Return ASDOT notation for AS numbers greater than 16 bits. - """ - if self.asn > 65535: - return f'{self.asn // 65536}.{self.asn % 65536}' - return self.asn - - @property - def asn_with_asdot(self): - """ - Return both plain and ASDOT notation, where applicable. - """ - if self.asn > 65535: - return f'{self.asn} ({self.asn // 65536}.{self.asn % 65536})' - else: - return self.asn - - class Aggregate(GetAvailablePrefixesMixin, PrimaryModel): """ An aggregate exists at the root level of the IP address space hierarchy in NetBox. Aggregates are used to organize