Просмотр исходного кода

Commit fixes Jeremy suggested

Co-authored-by: Jeremy Stretch <jstretch@ns1.com>
Daniel Sheppard 3 лет назад
Родитель
Сommit
0004b834fb

+ 0 - 1
netbox/ipam/api/serializers.py

@@ -483,7 +483,6 @@ class L2VPNTerminationSerializer(NetBoxModelSerializer):
         fields = [
             'id', 'url', 'display', 'l2vpn', 'assigned_object_type', 'assigned_object_id',
             'assigned_object',
-            # Extra Fields
             'tags', 'custom_fields', 'created', 'last_updated'
         ]
 

+ 2 - 2
netbox/ipam/api/views.py

@@ -18,7 +18,7 @@ from netbox.config import get_config
 from utilities.constants import ADVISORY_LOCK_KEYS
 from utilities.utils import count_related
 from . import serializers
-from ..models.l2vpn import L2VPN, L2VPNTermination
+from ipam.models import L2VPN, L2VPNTermination
 
 
 class IPAMRootView(APIRootView):
@@ -159,7 +159,7 @@ class ServiceViewSet(NetBoxModelViewSet):
 
 
 class L2VPNViewSet(NetBoxModelViewSet):
-    queryset = L2VPN.objects
+    queryset = L2VPN.objects.prefetch_related('import_targets', 'export_targets', 'tenant', 'tags')
     serializer_class = serializers.L2VPNSerializer
     filterset_class = filtersets.L2VPNFilterSet
 

+ 3 - 0
netbox/ipam/forms/models.py

@@ -893,6 +893,9 @@ class L2VPNForm(TenancyForm, NetBoxModelForm):
         fields = (
             'name', 'slug', 'type', 'identifier', 'description', 'import_targets', 'export_targets', 'tenant', 'tags'
         )
+        widgets = {
+            'type': StaticSelect(),
+        }
 
 
 class L2VPNTerminationForm(NetBoxModelForm):

+ 8 - 4
netbox/ipam/models/l2vpn.py

@@ -34,7 +34,7 @@ class L2VPN(NetBoxModel):
     description = models.TextField(null=True, blank=True)
     tenant = models.ForeignKey(
         to='tenancy.Tenant',
-        on_delete=models.SET_NULL,
+        on_delete=models.PROTECT,
         related_name='l2vpns',
         blank=True,
         null=True
@@ -85,7 +85,6 @@ class L2VPNTermination(NetBoxModel):
     class Meta:
         ordering = ('l2vpn',)
         verbose_name = 'L2VPN Termination'
-
         constraints = (
             models.UniqueConstraint(
                 fields=('assigned_object_type', 'assigned_object_id'),
@@ -112,5 +111,10 @@ class L2VPNTermination(NetBoxModel):
 
         # Only check if L2VPN is set and is of type P2P
         if self.l2vpn and self.l2vpn.type in L2VPNTypeChoices.P2P:
-            if L2VPNTermination.objects.filter(l2vpn=self.l2vpn).exclude(pk=self.pk).count() >= 2:
-                raise ValidationError(f'P2P Type L2VPNs can only have 2 terminations; first delete a termination')
+            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.'
+                )

+ 1 - 1
netbox/netbox/navigation_menu.py

@@ -263,7 +263,7 @@ IPAM_MENU = Menu(
         MenuGroup(
             label='L2VPNs',
             items=(
-                get_model_item('ipam', 'l2vpn', 'L2VPN'),
+                get_model_item('ipam', 'l2vpn', 'L2VPNs'),
                 get_model_item('ipam', 'l2vpntermination', 'Terminations'),
             ),
         ),