Skip to content

Commit

Permalink
Fixes #2181: Raise validation error on invalid prefix_length when all…
Browse files Browse the repository at this point in the history
…ocating next-available prefix
  • Loading branch information
jeremystretch committed Jun 29, 2018
1 parent d98aa03 commit af54d96
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions netbox/ipam/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from django.shortcuts import get_object_or_404
from rest_framework import status
from rest_framework.decorators import detail_route
from rest_framework.exceptions import PermissionDenied
from rest_framework.exceptions import PermissionDenied, ValidationError
from rest_framework.response import Response

from extras.api.views import CustomFieldModelViewSet
Expand Down Expand Up @@ -98,7 +98,23 @@ def available_prefixes(self, request, pk=None):
requested_prefixes = request.data if isinstance(request.data, list) else [request.data]

# Allocate prefixes to the requested objects based on availability within the parent
for requested_prefix in requested_prefixes:
for i, requested_prefix in enumerate(requested_prefixes):

# Validate requested prefix size
if 'prefix_length' not in requested_prefix:
raise ValidationError("Item {}: prefix_length field missing".format(i))
elif not isinstance(requested_prefix['prefix_length'], int):
raise ValidationError("Item {}: Invalid prefix length ({})".format(
i, requested_prefix['prefix_length']
))
elif prefix.family == 4 and requested_prefix['prefix_length'] > 32:
raise ValidationError("Item {}: Invalid prefix length ({}) for IPv4".format(
i, requested_prefix['prefix_length']
))
elif prefix.family == 6 and requested_prefix['prefix_length'] > 128:
raise ValidationError("Item {}: Invalid prefix length ({}) for IPv6".format(
i, requested_prefix['prefix_length']
))

# Find the first available prefix equal to or larger than the requested size
for available_prefix in available_prefixes.iter_cidrs():
Expand Down

0 comments on commit af54d96

Please sign in to comment.