From 074cffcbd742bc6dead0eb07c5385a49ace610ee Mon Sep 17 00:00:00 2001 From: Nico Schottelius Date: Wed, 9 Dec 2020 21:20:33 +0100 Subject: [PATCH] Add selection for vpnnetworkreservations --- uncloud_net/selectors.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 uncloud_net/selectors.py diff --git a/uncloud_net/selectors.py b/uncloud_net/selectors.py new file mode 100644 index 0000000..024d44f --- /dev/null +++ b/uncloud_net/selectors.py @@ -0,0 +1,23 @@ +from django.db.models import Count +from .models import * + +@transaction.atomic +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)