1
0
mirror of https://github.com/netbox-community/netbox.git synced 2024-05-10 07:54:54 +00:00

#6454 fix merge conflicts

This commit is contained in:
Arthur
2022-08-18 15:31:54 -07:00
458 changed files with 14069 additions and 6039 deletions

View File

@@ -2,6 +2,7 @@
from .fhrp import *
from .vrfs import *
from .ip import *
from .l2vpn import *
from .services import *
from .vlans import *
@@ -12,6 +13,8 @@ __all__ = (
'IPRange',
'FHRPGroup',
'FHRPGroupAssignment',
'L2VPN',
'L2VPNTermination',
'Prefix',
'RIR',
'Role',

View File

@@ -48,7 +48,7 @@ class FHRPGroup(NetBoxModel):
related_query_name='fhrpgroup'
)
clone_fields = ('protocol', 'auth_type', 'auth_key')
clone_fields = ('protocol', 'auth_type', 'auth_key', 'description')
class Meta:
ordering = ['protocol', 'group_id', 'pk']

View File

@@ -179,9 +179,9 @@ class Aggregate(GetAvailablePrefixesMixin, NetBoxModel):
blank=True
)
clone_fields = [
clone_fields = (
'rir', 'tenant', 'date_added', 'description',
]
)
class Meta:
ordering = ('prefix', 'pk') # prefix may be non-unique
@@ -368,9 +368,9 @@ class Prefix(GetAvailablePrefixesMixin, NetBoxModel):
objects = PrefixQuerySet.as_manager()
clone_fields = [
clone_fields = (
'site', 'vrf', 'tenant', 'vlan', 'status', 'role', 'is_pool', 'mark_utilized', 'description',
]
)
class Meta:
ordering = (F('vrf').asc(nulls_first=True), 'prefix', 'pk') # (vrf, prefix) may be non-unique
@@ -616,9 +616,9 @@ class IPRange(NetBoxModel):
blank=True
)
clone_fields = [
clone_fields = (
'vrf', 'tenant', 'status', 'role', 'description',
]
)
class Meta:
ordering = (F('vrf').asc(nulls_first=True), 'start_address', 'pk') # (vrf, start_address) may be non-unique
@@ -821,7 +821,7 @@ class IPAddress(NetBoxModel):
ct_field='assigned_object_type',
fk_field='assigned_object_id'
)
nat_inside = models.OneToOneField(
nat_inside = models.ForeignKey(
to='self',
on_delete=models.SET_NULL,
related_name='nat_outside',
@@ -844,9 +844,9 @@ class IPAddress(NetBoxModel):
objects = IPAddressManager()
clone_fields = [
'vrf', 'tenant', 'status', 'role', 'description',
]
clone_fields = (
'vrf', 'tenant', 'status', 'role', 'dns_name', 'description',
)
class Meta:
ordering = ('address', 'pk') # address may be non-unique
@@ -865,6 +865,25 @@ class IPAddress(NetBoxModel):
address__net_host=str(self.address.ip)
).exclude(pk=self.pk)
def get_next_available_ip(self):
"""
Return the next available IP address within this IP's network (if any)
"""
if self.address and self.address.broadcast:
start_ip = self.address.ip + 1
end_ip = self.address.broadcast - 1
if start_ip <= end_ip:
available_ips = netaddr.IPSet(netaddr.IPRange(start_ip, end_ip))
available_ips -= netaddr.IPSet([
address.ip for address in IPAddress.objects.filter(
vrf=self.vrf,
address__gt=self.address,
address__net_contained_or_equal=self.address.cidr
).values_list('address', flat=True)
])
if available_ips:
return next(iter(available_ips))
def clean(self):
super().clean()
@@ -915,6 +934,15 @@ class IPAddress(NetBoxModel):
super().save(*args, **kwargs)
def clone(self):
attrs = super().clone()
# Populate the address field with the next available IP (if any)
if next_available_ip := self.get_next_available_ip():
attrs['address'] = f'{next_available_ip}/{self.address.prefixlen}'
return attrs
def to_objectchange(self, action):
objectchange = super().to_objectchange(action)
objectchange.related_object = self.assigned_object

141
netbox/ipam/models/l2vpn.py Normal file
View File

@@ -0,0 +1,141 @@
from django.contrib.contenttypes.fields import GenericRelation, GenericForeignKey
from django.contrib.contenttypes.models import ContentType
from django.core.exceptions import ValidationError
from django.db import models
from django.urls import reverse
from ipam.choices import L2VPNTypeChoices
from ipam.constants import L2VPN_ASSIGNMENT_MODELS
from netbox.models import NetBoxModel
__all__ = (
'L2VPN',
'L2VPNTermination',
)
class L2VPN(NetBoxModel):
name = models.CharField(
max_length=100,
unique=True
)
slug = models.SlugField()
type = models.CharField(
max_length=50,
choices=L2VPNTypeChoices
)
identifier = models.BigIntegerField(
null=True,
blank=True
)
import_targets = models.ManyToManyField(
to='ipam.RouteTarget',
related_name='importing_l2vpns',
blank=True,
)
export_targets = models.ManyToManyField(
to='ipam.RouteTarget',
related_name='exporting_l2vpns',
blank=True
)
description = models.CharField(
max_length=200,
blank=True
)
tenant = models.ForeignKey(
to='tenancy.Tenant',
on_delete=models.PROTECT,
related_name='l2vpns',
blank=True,
null=True
)
contacts = GenericRelation(
to='tenancy.ContactAssignment'
)
clone_fields = ('type',)
class Meta:
ordering = ('name', 'identifier')
verbose_name = 'L2VPN'
def __str__(self):
if self.identifier:
return f'{self.name} ({self.identifier})'
return f'{self.name}'
def get_absolute_url(self):
return reverse('ipam:l2vpn', args=[self.pk])
class L2VPNTermination(NetBoxModel):
l2vpn = models.ForeignKey(
to='ipam.L2VPN',
on_delete=models.CASCADE,
related_name='terminations'
)
assigned_object_type = models.ForeignKey(
to=ContentType,
limit_choices_to=L2VPN_ASSIGNMENT_MODELS,
on_delete=models.PROTECT,
related_name='+'
)
assigned_object_id = models.PositiveBigIntegerField()
assigned_object = GenericForeignKey(
ct_field='assigned_object_type',
fk_field='assigned_object_id'
)
clone_fields = ('l2vpn',)
class Meta:
ordering = ('l2vpn',)
verbose_name = 'L2VPN termination'
constraints = (
models.UniqueConstraint(
fields=('assigned_object_type', 'assigned_object_id'),
name='ipam_l2vpntermination_assigned_object'
),
)
def __str__(self):
if self.pk is not None:
return f'{self.assigned_object} <> {self.l2vpn}'
return super().__str__()
def get_absolute_url(self):
return reverse('ipam:l2vpntermination', args=[self.pk])
def clean(self):
# Only check is assigned_object is set. Required otherwise we have an Integrity Error thrown.
if self.assigned_object:
obj_id = self.assigned_object.pk
obj_type = ContentType.objects.get_for_model(self.assigned_object)
if L2VPNTermination.objects.filter(assigned_object_id=obj_id, assigned_object_type=obj_type).\
exclude(pk=self.pk).count() > 0:
raise ValidationError(f'L2VPN Termination already assigned ({self.assigned_object})')
# Only check if L2VPN is set and is of type P2P
if hasattr(self, 'l2vpn') and self.l2vpn.type in L2VPNTypeChoices.P2P:
terminations_count = L2VPNTermination.objects.filter(l2vpn=self.l2vpn).exclude(pk=self.pk).count()
if terminations_count >= 2:
l2vpn_type = self.l2vpn.get_type_display()
raise ValidationError(
f'{l2vpn_type} L2VPNs cannot have more than two terminations; found {terminations_count} already '
f'defined.'
)
@property
def assigned_object_parent(self):
obj_type = ContentType.objects.get_for_model(self.assigned_object)
if obj_type.model == 'vminterface':
return self.assigned_object.virtual_machine
elif obj_type.model == 'interface':
return self.assigned_object.device
elif obj_type.model == 'vminterface':
return self.assigned_object.virtual_machine
return None
@property
def assigned_object_site(self):
return self.assigned_object_parent.site

View File

@@ -1,4 +1,4 @@
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation
from django.contrib.contenttypes.models import ContentType
from django.core.exceptions import ValidationError
from django.core.validators import MaxValueValidator, MinValueValidator
@@ -8,6 +8,7 @@ from django.urls import reverse
from dcim.models import Interface
from ipam.choices import *
from ipam.constants import *
from ipam.models import L2VPNTermination
from ipam.querysets import VLANQuerySet
from netbox.models import OrganizationalModel, NetBoxModel
from virtualization.models import VMInterface
@@ -173,6 +174,13 @@ class VLAN(NetBoxModel):
blank=True
)
l2vpn_terminations = GenericRelation(
to='ipam.L2VPNTermination',
content_type_field='assigned_object_type',
object_id_field='assigned_object_id',
related_query_name='vlan'
)
objects = VLANQuerySet.as_manager()
clone_fields = [
@@ -227,3 +235,7 @@ class VLAN(NetBoxModel):
Q(untagged_vlan_id=self.pk) |
Q(tagged_vlans=self.pk)
).distinct()
@property
def l2vpn_termination(self):
return self.l2vpn_terminations.first()

View File

@@ -55,9 +55,9 @@ class VRF(NetBoxModel):
blank=True
)
clone_fields = [
clone_fields = (
'tenant', 'enforce_unique', 'description',
]
)
class Meta:
ordering = ('name', 'rd', 'pk') # (name, rd) may be non-unique