Skip to content

Commit

Permalink
Fixes netbox-community#13440 by enriching data before deserialization…
Browse files Browse the repository at this point in the history
… for available-vlans API
  • Loading branch information
einar.ameen committed Aug 10, 2023
1 parent 72e1e8f commit 2ec416c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
11 changes: 5 additions & 6 deletions netbox/ipam/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -475,10 +475,11 @@ def post(self, request, pk):
vlangroup = get_object_or_404(VLANGroup.objects.restrict(request.user), pk=pk)
available_vlans = vlangroup.get_available_vids()
many = isinstance(request.data, list)
requested_vlans_data = request.data if many else [request.data]

# Validate requested VLANs
serializer = serializers.CreateAvailableVLANSerializer(
data=request.data if many else [request.data],
data=requested_vlans_data,
many=True,
context={
'request': request,
Expand All @@ -491,9 +492,7 @@ def post(self, request, pk):
status=status.HTTP_400_BAD_REQUEST
)

requested_vlans = serializer.validated_data

for i, requested_vlan in enumerate(requested_vlans):
for i, requested_vlan in enumerate(requested_vlans_data):
try:
requested_vlan['vid'] = available_vlans.pop(0)
requested_vlan['group'] = vlangroup.pk
Expand All @@ -505,9 +504,9 @@ def post(self, request, pk):
# Initialize the serializer with a list or a single object depending on what was requested
context = {'request': request}
if many:
serializer = serializers.VLANSerializer(data=requested_vlans, many=True, context=context)
serializer = serializers.VLANSerializer(data=requested_vlans_data, many=True, context=context)
else:
serializer = serializers.VLANSerializer(data=requested_vlans[0], context=context)
serializer = serializers.VLANSerializer(data=requested_vlans_data[0], context=context)

# Create the new VLAN(s)
if serializer.is_valid():
Expand Down
16 changes: 13 additions & 3 deletions netbox/ipam/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -812,6 +812,12 @@ def setUpTestData(cls):
)
VLANGroup.objects.bulk_create(vlan_groups)

tenants = (
Tenant(name='Tenant 1', slug='tenant-1'),
Tenant(name='Tenant 2', slug='tenant-2'),
)
Tenant.objects.bulk_create(tenants)

def test_list_available_vlans(self):
"""
Test retrieval of all available VLANs within a group.
Expand Down Expand Up @@ -858,6 +864,7 @@ def test_create_single_available_vlan(self):

data = {
"name": "First VLAN",
"tenant": Tenant.objects.first().pk,
}
url = reverse('ipam-api:vlangroup-available-vlans', kwargs={'pk': vlangroup.pk})
response = self.client.post(url, data, format='json', **self.header)
Expand All @@ -882,9 +889,12 @@ def test_create_multiple_available_vlans(self):
VLAN.objects.bulk_create(vlans)

data = (
{"name": "First VLAN"},
{"name": "Second VLAN"},
{"name": "Third VLAN"},
{"name": "First VLAN",
"tenant": Tenant.objects.first().pk},
{"name": "Second VLAN",
"tenant": Tenant.objects.first().pk},
{"name": "Third VLAN",
"tenant": Tenant.objects.last().pk},
)
url = reverse('ipam-api:vlangroup-available-vlans', kwargs={'pk': vlangroup.pk})
response = self.client.post(url, data, format='json', **self.header)
Expand Down

0 comments on commit 2ec416c

Please sign in to comment.