[vpn] add selector for size

This commit is contained in:
Nico Schottelius 2020-12-13 11:43:49 +01:00
parent 10d5a72c5a
commit 5716cae900
3 changed files with 10 additions and 10 deletions

View file

@ -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" ]

View file

@ -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 ])