add new /sizes endpoint

This commit is contained in:
Nico Schottelius 2020-12-20 21:45:47 +01:00
parent cdab685269
commit ece2bca831
6 changed files with 52 additions and 20 deletions

View file

@ -69,7 +69,7 @@ router.register(r'v1/user/register', authviews.AccountManagementViewSet, basenam
# Net # Net
router.register(r'v2/net/wireguardvpn', netviews.WireGuardVPNViewSet, basename='wireguardvpnnetwork') router.register(r'v2/net/wireguardvpn', netviews.WireGuardVPNViewSet, basename='wireguardvpnnetwork')
#router.register(r'v1/admin/vpnreservation', netviews.VPNNetworkReservationViewSet, basename='vpnnetreservation') router.register(r'v2/net/wireguardvpnsizes', netviews.WireGuardVPNSizes, basename='wireguardvpnnetworksizes')

View file

@ -0,0 +1,18 @@
# Generated by Django 3.1 on 2020-12-20 18:37
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('uncloud_net', '0004_auto_20201213_1734'),
]
operations = [
migrations.AlterField(
model_name='wireguardvpn',
name='wireguard_public_key',
field=models.CharField(max_length=48, unique=True),
),
]

View file

@ -1,12 +1,7 @@
from django.db import transaction from django.db import transaction
from django.db.models import Count, F from django.db.models import Count, F
from .models import * from .models import *
# def get_num_used_networks(pool):
# return pool.wireguardvpn_set.count()
def get_suitable_pools(subnetwork_mask): def get_suitable_pools(subnetwork_mask):
""" """
Find suitable pools for a certain network size. Find suitable pools for a certain network size.
@ -44,17 +39,5 @@ def allowed_vpn_network_reservation_size():
# Need to return set of tuples, see # Need to return set of tuples, see
# https://docs.djangoproject.com/en/3.1/ref/models/fields/#field-choices # https://docs.djangoproject.com/en/3.1/ref/models/fields/#field-choices
return set([ (pool.subnetwork_mask, pool.subnetwork_mask) for pool in pools ]) # return set([ (pool.subnetwork_mask, pool.subnetwork_mask) for pool in pools ])
return set([pool.subnetwork_mask for pool in pools ])
#def get_next_vpnnetwork(pool):
# get all associated networks
# look for the lowest free number
# return that
# select last used one
# try to increment by one -> get new network
# if that fails search through the existing vpns for the first unused number
#

View file

@ -6,6 +6,8 @@ from rest_framework import serializers
from .models import * from .models import *
from .services import * from .services import *
from .selectors import *
class WireGuardVPNSerializer(serializers.ModelSerializer): class WireGuardVPNSerializer(serializers.ModelSerializer):
address = serializers.CharField(read_only=True) address = serializers.CharField(read_only=True)
@ -23,6 +25,14 @@ class WireGuardVPNSerializer(serializers.ModelSerializer):
} }
def validate_network_mask(self, value):
msg = _(f"No pool for network size {value}")
sizes = allowed_vpn_network_reservation_size()
if not value in sizes:
raise serializers.ValidationError(msg)
def validate_wireguard_public_key(self, value): def validate_wireguard_public_key(self, value):
msg = _("Supplied key is not a valid wireguard public key") msg = _("Supplied key is not a valid wireguard public key")
@ -41,3 +51,12 @@ class WireGuardVPNSerializer(serializers.ModelSerializer):
raise serializers.ValidationError(msg) raise serializers.ValidationError(msg)
return value return value
class WireGuardVPNSizesSerializer(serializers.Serializer):
size = serializers.IntegerField(min_value=0, max_value=128)
# sizes = serializers.ListField(
# child=serializers.IntegerField(min_value=0, max_value=128)
# )

View file

@ -7,6 +7,8 @@ from .tasks import *
@transaction.atomic @transaction.atomic
def create_wireguard_vpn(owner, public_key, network_mask): def create_wireguard_vpn(owner, public_key, network_mask):
pool = get_suitable_pools(network_mask)[0] pool = get_suitable_pools(network_mask)[0]
count = pool.wireguardvpn_set.count() count = pool.wireguardvpn_set.count()

View file

@ -56,3 +56,13 @@ class WireGuardVPNCreateView(LoginRequiredMixin, SuccessMessageMixin, CreateView
def get_success_message(self, cleaned_data): def get_success_message(self, cleaned_data):
return self.success_message % dict(cleaned_data, return self.success_message % dict(cleaned_data,
the_prefix = self.object.prefix) the_prefix = self.object.prefix)
class WireGuardVPNSizes(viewsets.ViewSet):
def list(self, request):
sizes = allowed_vpn_network_reservation_size()
print(sizes)
sizes = [ { 'size': size } for size in sizes ]
print(sizes)
return Response(WireGuardVPNSizesSerializer(sizes, many=True).data)