diff --git a/netbox/ipam/api/views.py b/netbox/ipam/api/views.py index 99b4c023d95..78040fa40fe 100644 --- a/netbox/ipam/api/views.py +++ b/netbox/ipam/api/views.py @@ -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, @@ -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 @@ -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(): diff --git a/netbox/ipam/tests/test_api.py b/netbox/ipam/tests/test_api.py index b6aeccc1a73..f7dfcacfd20 100644 --- a/netbox/ipam/tests/test_api.py +++ b/netbox/ipam/tests/test_api.py @@ -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. @@ -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) @@ -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)