Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes: #15924 - Prevent API payload from allowing tagged_vlans while interface mode is set to tagged-all #17211

Draft
wants to merge 14 commits into
base: feature
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion netbox/dcim/api/serializers_/device_components.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.utils.translation import gettext as _
from django.contrib.contenttypes.models import ContentType
from drf_spectacular.utils import extend_schema_field
from rest_framework import serializers
Expand Down Expand Up @@ -236,8 +237,8 @@ class Meta:

def validate(self, data):

# Validate many-to-many VLAN assignments
if not self.nested:
# Validate many-to-many VLAN assignments
device = self.instance.device if self.instance else data.get('device')
for vlan in data.get('tagged_vlans', []):
if vlan.site not in [device.site, None]:
Expand All @@ -246,6 +247,13 @@ def validate(self, data):
f"or it must be global."
})

# Validate that tagged-all payload does not include tagged_vlans
mode = data.get('mode') or getattr(self.instance, 'mode', None)
if mode == InterfaceModeChoices.MODE_TAGGED_ALL and data.get('tagged_vlans'):
DanSheps marked this conversation as resolved.
Show resolved Hide resolved
raise serializers.ValidationError({
'tagged_vlans': "Tagged-All interface mode must not include any tagged vlans"
})

return super().validate(data)


Expand Down
4 changes: 4 additions & 0 deletions netbox/dcim/models/device_components.py
Original file line number Diff line number Diff line change
Expand Up @@ -914,6 +914,10 @@ def save(self, *args, **kwargs):

super().save(*args, **kwargs)

# Clear any tagged vlans set when mode is tagged-all
if self.mode == InterfaceModeChoices.MODE_TAGGED_ALL and self.tagged_vlans.count():
self.tagged_vlans.set([])
DanSheps marked this conversation as resolved.
Show resolved Hide resolved

@property
def _occupied(self):
return super()._occupied or bool(self.wireless_link_id)
Expand Down