2020-12-13 10:38:41 +00:00
|
|
|
from django.db import transaction
|
|
|
|
from django.db.models import Count, F
|
|
|
|
|
|
|
|
|
2020-12-09 20:20:33 +00:00
|
|
|
from .models import *
|
|
|
|
|
2020-12-13 10:43:49 +00:00
|
|
|
def get_suitable_pool(subnetwork_mask):
|
2020-12-09 20:20:33 +00:00
|
|
|
"""
|
|
|
|
Find suitable pools for a certain network size.
|
|
|
|
|
|
|
|
First, filter for all pools that offer the requested subnetwork_size.
|
|
|
|
|
|
|
|
Then find those pools that are not fully exhausted:
|
|
|
|
|
|
|
|
The number of available networks in a pool is 2^(subnetwork_size-network_size.
|
|
|
|
|
|
|
|
The number of available networks in a pool is given by the number of VPNNetworkreservations.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
2020-12-13 10:43:49 +00:00
|
|
|
return WireGuardVPNPool.objects.annotate(
|
|
|
|
num_reservations=Count('wireguardvpn'),
|
|
|
|
max_reservations=2**(F('subnetwork_mask')-F('network_mask'))).filter(
|
2020-12-09 20:20:33 +00:00
|
|
|
num_reservations__lt=F('max_reservations'),
|
2020-12-13 10:43:49 +00:00
|
|
|
subnetwork_mask=subnetwork_mask)
|
2020-12-13 10:38:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
def allowed_vpn_network_reservation_size():
|
|
|
|
"""
|
|
|
|
Find all possible sizes of subnetworks that are available.
|
|
|
|
|
|
|
|
Select all pools with free networks.
|
|
|
|
|
|
|
|
Get their subnetwork sizes, reduce to a set
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
2020-12-13 10:43:49 +00:00
|
|
|
pools = WireGuardVPNPool.objects.annotate(num_reservations=Count('wireguardvpn'),
|
|
|
|
max_reservations=2**(F('subnetwork_mask')-F('network_mask'))).filter(
|
2020-12-13 10:38:41 +00:00
|
|
|
num_reservations__lt=F('max_reservations'))
|
|
|
|
|
2020-12-13 10:43:49 +00:00
|
|
|
return set([ pool.subnetwork_mask for pool in pools ])
|