diff --git a/uncloud_django_based/uncloud/uncloud_net/models.py b/uncloud_django_based/uncloud/uncloud_net/models.py index 2eaf92d..734c9e9 100644 --- a/uncloud_django_based/uncloud/uncloud_net/models.py +++ b/uncloud_django_based/uncloud/uncloud_net/models.py @@ -1,4 +1,5 @@ import uuid +import ipaddress from django.db import models from django.contrib.auth import get_user_model @@ -65,9 +66,13 @@ class VPNPool(UncloudModel): free_net = self.vpnnetworkreservation_set.objects.filter(vpnpool=self, status='free') - last_net = self.vpnnetworkreservation_set.objects.filter(vpnpool=self, + used_net = self.vpnnetworkreservation_set.objects.filter(vpnpool=self, status='used') + this_net = ipaddress.ip_network("{}/{}".format( + self.network, self.network_size)) + + if num_free_networks == 0: raise Exception("No free networks") @@ -84,10 +89,17 @@ class VPNPool(UncloudModel): next: """ - last_ip = last_net.address -# next_ip = + last_net = ipaddress.ip_network(self.used_networks.last()) + last_net_ip = last_net[0] + if last_net_ip.version == 6: + offset_to_next = 2**(128 - self.subnetwork_size) + elif last_net_ip.version == 4: + offset_to_next = 2**(32 - self.subnetwork_size) + next_net_ip = last_net_ip + offset_to_next + + return next_net_ip