dnmgmt/api/views.py

124 lines
3.4 KiB
Python

#from django.shortcuts import render
from django.http import JsonResponse
from django.core.exceptions import ValidationError
from django.contrib.auth.views import login_required
from whoisdb.models import ASBlock, ASNumber, InetNum
@login_required
def asblockFreeAS(request):
ret = {
"success": False,
"errorMsg": None,
"number": -1,
}
try:
blockName = request.GET.get('block', None)
if not blockName:
raise ValidationError("No block given")
try:
mnts = request.user.maintainer_set.all()
block = ASBlock.objects.filter(mnt_by__in=mnts).distinct().get(handle=blockName)
if block.asblock_set.count() > 0:
raise ValidationError("AS Block already has sub AS Blocks")
if block.asnumber_set.count() > 0:
num = block.asnumber_set.order_by("-number")[0].number + 1
if num > block.asEnd:
num = None
for n in range(block.asBegin, block.asEnd+1):
try:
ASNumber.objects.get(number=n)
except ASNumber.DoesNotExist:
num = n
break
if not num:
raise ValidationError("No free AS Number in block")
ret["number"] = num
else:
ret["number"] = block.asBegin
except ASBlock.DoesNotExist:
raise ValidationError("Could not get AS Block")
ret["success"] = True
except ValidationError as e:
ret["errorMsg"] = e.message
return JsonResponse(ret)
@login_required
def freeSubnet(request):
ret = {
"success": False,
"errorMsg": None,
"network": None,
}
try:
parentRangeName = request.GET.get('parentRange', None)
if not parentRangeName:
raise ValidationError("No subnet given")
parentRange = None
try:
mnts = request.user.maintainer_set.all()
parentRange = InetNum.objects.filter(mnt_by__in=mnts).distinct().get(handle=parentRangeName)
except InetNum.DoesNotExist:
raise ValidationError("Parent range does not exist / is not maintained by you")
prefixLen = 0
try:
prefixLen = request.GET.get("prefixLen", None)
if not prefixLen:
if parentRange.protocol == InetNum.IPv4:
prefixLen = 27
else:
prefixLen = 60
prefixLen = int(prefixLen)
if prefixLen < 8 or \
(parentRange.protocol == InetNum.IPv4 and prefixLen > 32) or \
(parentRange.protocol == InetNum.IPv6 and prefixLen > 128):
raise ValidationError("Given prefix length is out of range")
except ValueError:
raise ValidationError("PrefixLen is not a number")
usableNet = None
# FIXME: use first biggest usable netblock...
#biggestNet = parentRange.inetnum_set.order_by("-address")
#if biggestNet
# candidateNet =
# and (biggestNet.getNetwork().broadcast_address.:
if not usableNet:
# search for free network
nets = list(parentRange.getNetwork().subnets())
for subRange in parentRange.inetnum_set.all():
newNet = None
for net in nets:
if subRange.getNetwork().network_address in net:
newNet = net
if not newNet:
# critical error, we want a 500 here
raise ValueError("Subnet not in range")
nets.remove(newNet)
nets.extend(newNet.address_exclude(subRange.getNetwork()))
nets = sorted(nets)
for net in nets:
if net.prefixlen <= prefixLen:
usableNet = net
break
if not usableNet:
raise ValidationError("No space left in given range")
ret["network"] = "%s/%s" % (usableNet.network_address, prefixLen)
ret["success"] = True
except ValidationError as e:
ret["errorMsg"] = e.message
return JsonResponse(ret)