From 5716cae9008fd607dacb5683eb1cd7ef840bb37b Mon Sep 17 00:00:00 2001 From: Nico Schottelius Date: Sun, 13 Dec 2020 11:43:49 +0100 Subject: [PATCH] [vpn] add selector for size --- uncloud_net/forms.py | 4 ++-- uncloud_net/selectors.py | 16 ++++++++-------- ...nnetwork_form.html => wireguardvpn_form.html} | 0 3 files changed, 10 insertions(+), 10 deletions(-) rename uncloud_net/templates/uncloud_net/{vpnnetwork_form.html => wireguardvpn_form.html} (100%) diff --git a/uncloud_net/forms.py b/uncloud_net/forms.py index 6125143..4bdcd79 100644 --- a/uncloud_net/forms.py +++ b/uncloud_net/forms.py @@ -4,9 +4,9 @@ from .models import * from .selectors import * class WireGuardVPNForm(forms.ModelForm): + network_size = forms.ChoiceField(choices=allowed_vpn_network_reservation_size) + class Meta: model = WireGuardVPN - network_size = forms.ChoiceField(choices=allowed_vpn_network_reservation_size) - fields = [ "wireguard_public_key" ] diff --git a/uncloud_net/selectors.py b/uncloud_net/selectors.py index 4888f60..b2f697c 100644 --- a/uncloud_net/selectors.py +++ b/uncloud_net/selectors.py @@ -4,7 +4,7 @@ from django.db.models import Count, F from .models import * -def get_suitable_pool(subnetwork_size): +def get_suitable_pool(subnetwork_mask): """ Find suitable pools for a certain network size. @@ -18,11 +18,11 @@ def get_suitable_pool(subnetwork_size): """ - return VPNPool.objects.annotate( - num_reservations=Count('vpnnetworkreservation'), - max_reservations=2**(F('subnetwork_size')-F('network_size'))).filter( + return WireGuardVPNPool.objects.annotate( + num_reservations=Count('wireguardvpn'), + max_reservations=2**(F('subnetwork_mask')-F('network_mask'))).filter( num_reservations__lt=F('max_reservations'), - subnetwork_size=subnetwork_size) + subnetwork_mask=subnetwork_mask) def allowed_vpn_network_reservation_size(): @@ -35,8 +35,8 @@ def allowed_vpn_network_reservation_size(): """ - pools = VPNPool.objects.annotate(num_reservations=Count('vpnnetworkreservation'), - max_reservations=2**(F('subnetwork_size')-F('network_size'))).filter( + pools = WireGuardVPNPool.objects.annotate(num_reservations=Count('wireguardvpn'), + max_reservations=2**(F('subnetwork_mask')-F('network_mask'))).filter( num_reservations__lt=F('max_reservations')) - return set([ pool.subnetwork_size for pool in pools ]) + return set([ pool.subnetwork_mask for pool in pools ]) diff --git a/uncloud_net/templates/uncloud_net/vpnnetwork_form.html b/uncloud_net/templates/uncloud_net/wireguardvpn_form.html similarity index 100% rename from uncloud_net/templates/uncloud_net/vpnnetwork_form.html rename to uncloud_net/templates/uncloud_net/wireguardvpn_form.html