mirror of
https://github.com/netbox-community/netbox.git
synced 2024-05-10 07:54:54 +00:00
Closes #10043: Add support for 'limit' query parameter to available VLANs API endpoint
This commit is contained in:
@ -4,6 +4,7 @@
|
|||||||
|
|
||||||
### Enhancements
|
### Enhancements
|
||||||
|
|
||||||
|
* [#10043](https://github.com/netbox-community/netbox/issues/10043) - Add support for `limit` query parameter to available VLANs API endpoint
|
||||||
* [#10060](https://github.com/netbox-community/netbox/issues/10060) - Add journal entries to global search
|
* [#10060](https://github.com/netbox-community/netbox/issues/10060) - Add journal entries to global search
|
||||||
|
|
||||||
### Bug Fixes
|
### Bug Fixes
|
||||||
|
@ -174,6 +174,21 @@ class L2VPNTerminationViewSet(NetBoxModelViewSet):
|
|||||||
# Views
|
# Views
|
||||||
#
|
#
|
||||||
|
|
||||||
|
def get_results_limit(request):
|
||||||
|
"""
|
||||||
|
Return the lesser of the specified limit (if any) and the configured MAX_PAGE_SIZE.
|
||||||
|
"""
|
||||||
|
config = get_config()
|
||||||
|
try:
|
||||||
|
limit = int(request.query_params.get('limit', config.PAGINATE_COUNT)) or config.MAX_PAGE_SIZE
|
||||||
|
except ValueError:
|
||||||
|
limit = config.PAGINATE_COUNT
|
||||||
|
if config.MAX_PAGE_SIZE:
|
||||||
|
limit = min(limit, config.MAX_PAGE_SIZE)
|
||||||
|
|
||||||
|
return limit
|
||||||
|
|
||||||
|
|
||||||
class AvailablePrefixesView(ObjectValidationMixin, APIView):
|
class AvailablePrefixesView(ObjectValidationMixin, APIView):
|
||||||
queryset = Prefix.objects.all()
|
queryset = Prefix.objects.all()
|
||||||
|
|
||||||
@ -265,16 +280,7 @@ class AvailableIPAddressesView(ObjectValidationMixin, APIView):
|
|||||||
@swagger_auto_schema(responses={200: serializers.AvailableIPSerializer(many=True)})
|
@swagger_auto_schema(responses={200: serializers.AvailableIPSerializer(many=True)})
|
||||||
def get(self, request, pk):
|
def get(self, request, pk):
|
||||||
parent = self.get_parent(request, pk)
|
parent = self.get_parent(request, pk)
|
||||||
config = get_config()
|
limit = get_results_limit(request)
|
||||||
PAGINATE_COUNT = config.PAGINATE_COUNT
|
|
||||||
MAX_PAGE_SIZE = config.MAX_PAGE_SIZE
|
|
||||||
|
|
||||||
try:
|
|
||||||
limit = int(request.query_params.get('limit', PAGINATE_COUNT))
|
|
||||||
except ValueError:
|
|
||||||
limit = PAGINATE_COUNT
|
|
||||||
if MAX_PAGE_SIZE:
|
|
||||||
limit = min(limit, MAX_PAGE_SIZE)
|
|
||||||
|
|
||||||
# Calculate available IPs within the parent
|
# Calculate available IPs within the parent
|
||||||
ip_list = []
|
ip_list = []
|
||||||
@ -357,8 +363,9 @@ class AvailableVLANsView(ObjectValidationMixin, APIView):
|
|||||||
@swagger_auto_schema(responses={200: serializers.AvailableVLANSerializer(many=True)})
|
@swagger_auto_schema(responses={200: serializers.AvailableVLANSerializer(many=True)})
|
||||||
def get(self, request, pk):
|
def get(self, request, pk):
|
||||||
vlangroup = get_object_or_404(VLANGroup.objects.restrict(request.user), pk=pk)
|
vlangroup = get_object_or_404(VLANGroup.objects.restrict(request.user), pk=pk)
|
||||||
available_vlans = vlangroup.get_available_vids()
|
limit = get_results_limit(request)
|
||||||
|
|
||||||
|
available_vlans = vlangroup.get_available_vids()[:limit]
|
||||||
serializer = serializers.AvailableVLANSerializer(available_vlans, many=True, context={
|
serializer = serializers.AvailableVLANSerializer(available_vlans, many=True, context={
|
||||||
'request': request,
|
'request': request,
|
||||||
'group': vlangroup,
|
'group': vlangroup,
|
||||||
|
@ -699,9 +699,18 @@ class VLANGroupTest(APIViewTestCases.APIViewTestCase):
|
|||||||
"""
|
"""
|
||||||
Test retrieval of all available VLANs within a group.
|
Test retrieval of all available VLANs within a group.
|
||||||
"""
|
"""
|
||||||
self.add_permissions('ipam.view_vlangroup', 'ipam.view_vlan')
|
MIN_VID = 100
|
||||||
vlangroup = VLANGroup.objects.first()
|
MAX_VID = 199
|
||||||
|
|
||||||
|
self.add_permissions('ipam.view_vlangroup', 'ipam.view_vlan')
|
||||||
|
vlangroup = VLANGroup.objects.create(
|
||||||
|
name='VLAN Group X',
|
||||||
|
slug='vlan-group-x',
|
||||||
|
min_vid=MIN_VID,
|
||||||
|
max_vid=MAX_VID
|
||||||
|
)
|
||||||
|
|
||||||
|
# Create a set of VLANs within the group
|
||||||
vlans = (
|
vlans = (
|
||||||
VLAN(vid=10, name='VLAN 10', group=vlangroup),
|
VLAN(vid=10, name='VLAN 10', group=vlangroup),
|
||||||
VLAN(vid=20, name='VLAN 20', group=vlangroup),
|
VLAN(vid=20, name='VLAN 20', group=vlangroup),
|
||||||
@ -711,13 +720,17 @@ class VLANGroupTest(APIViewTestCases.APIViewTestCase):
|
|||||||
|
|
||||||
# Retrieve all available VLANs
|
# Retrieve all available VLANs
|
||||||
url = reverse('ipam-api:vlangroup-available-vlans', kwargs={'pk': vlangroup.pk})
|
url = reverse('ipam-api:vlangroup-available-vlans', kwargs={'pk': vlangroup.pk})
|
||||||
response = self.client.get(url, **self.header)
|
response = self.client.get(f'{url}?limit=0', **self.header)
|
||||||
|
self.assertEqual(len(response.data), MAX_VID - MIN_VID + 1)
|
||||||
self.assertEqual(len(response.data), 4094 - len(vlans))
|
|
||||||
available_vlans = {vlan['vid'] for vlan in response.data}
|
available_vlans = {vlan['vid'] for vlan in response.data}
|
||||||
for vlan in vlans:
|
for vlan in vlans:
|
||||||
self.assertNotIn(vlan.vid, available_vlans)
|
self.assertNotIn(vlan.vid, available_vlans)
|
||||||
|
|
||||||
|
# Retrieve a maximum number of available VLANs
|
||||||
|
url = reverse('ipam-api:vlangroup-available-vlans', kwargs={'pk': vlangroup.pk})
|
||||||
|
response = self.client.get(f'{url}?limit=10', **self.header)
|
||||||
|
self.assertEqual(len(response.data), 10)
|
||||||
|
|
||||||
def test_create_single_available_vlan(self):
|
def test_create_single_available_vlan(self):
|
||||||
"""
|
"""
|
||||||
Test the creation of a single available VLAN.
|
Test the creation of a single available VLAN.
|
||||||
|
Reference in New Issue
Block a user