uncloud/uncloud_net/selectors.py

43 lines
1.3 KiB
Python

from django.db import transaction
from django.db.models import Count, F
from .models import *
def get_suitable_pool(subnetwork_size):
"""
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 VPNPool.objects.annotate(
num_reservations=Count('vpnnetworkreservation'),
max_reservations=2**(F('subnetwork_size')-F('network_size'))).filter(
num_reservations__lt=F('max_reservations'),
subnetwork_size=subnetwork_size)
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 = VPNPool.objects.annotate(num_reservations=Count('vpnnetworkreservation'),
max_reservations=2**(F('subnetwork_size')-F('network_size'))).filter(
num_reservations__lt=F('max_reservations'))
return set([ pool.subnetwork_size for pool in pools ])