Parcourir la source

Add default subnet size for IP pools

Baptiste Jonglez il y a 11 ans
Parent
commit
3c41019578
1 fichiers modifiés avec 5 ajouts et 6 suppressions
  1. 5 6
      coin/resources/models.py

+ 5 - 6
coin/resources/models.py

@@ -1,6 +1,7 @@
 # -*- coding: utf-8 -*-
 from django.db import models
 from django.core.exceptions import ValidationError
+from django.core.validators import MaxValueValidator
 from netfields import CidrAddressField, NetManager
 from netaddr import IPNetwork, IPSet
 
@@ -8,8 +9,9 @@ class IPPool(models.Model):
     """Pool of IP addresses (either v4 or v6)."""
     name = models.CharField(max_length=255, blank=False, null=False,
                             verbose_name='Name of the IP pool')
-    description = models.TextField(blank=True, null=False,
-                                   verbose_name='Description of the IP pool')
+    default_subnetsize = models.PositiveSmallIntegerField(blank=False,
+                                                          verbose_name='Default subnet size to allocate to subscribers in this pool',
+                                                          validators=[MaxValueValidator(64)])
     inet = CidrAddressField()
     objects = NetManager()
 
@@ -25,16 +27,13 @@ class IPSubnet(models.Model):
                                            related_name='ip_subnet')
 
     def clean(self):
-        # TODO: define default subnet sizes for IPv4 / IPv6. Or better,
-        # integrate it in the pool definition.
         if not self.inet:
             # Automatically allocate a free subnet
             pool = IPSet([self.ip_pool.inet])
             used = IPSet((s.inet for s in self.ip_pool.ipsubnet_set.all()))
             free = pool.difference(used)
-            SUBNET_SIZE = 32 if self.ip_pool.inet.version == 4 else 56
             # Generator for efficiency (we don't build the whole list)
-            available = (p for p in free.iter_cidrs() if p.prefixlen <= SUBNET_SIZE)
+            available = (p for p in free.iter_cidrs() if p.prefixlen <= self.ip_pool.default_subnetsize)
             # TODO: for IPv4, get rid of the network and broadcast
             # addresses? Not really needed nowadays, and we usually don't
             # have a real subnet in practice (i.e. Ethernet segment), but