uncloud/uncloud_net/selectors.py
2020-12-13 17:59:35 +01:00

60 lines
1.8 KiB
Python

from django.db import transaction
from django.db.models import Count, F
from .models import *
# def get_num_used_networks(pool):
# return pool.wireguardvpn_set.count()
def get_suitable_pools(subnetwork_mask):
"""
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.
"""
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_mask=subnetwork_mask)
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
"""
pools = WireGuardVPNPool.objects.annotate(num_reservations=Count('wireguardvpn'),
max_reservations=2**(F('subnetwork_mask')-F('network_mask'))).filter(
num_reservations__lt=F('max_reservations'))
# Need to return set of tuples, see
# https://docs.djangoproject.com/en/3.1/ref/models/fields/#field-choices
return set([ (pool.subnetwork_mask, 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
#