Browse Source

Check that new subnets don't intersect with existing ones

Baptiste Jonglez 11 years ago
parent
commit
908e46458b
1 changed files with 11 additions and 3 deletions
  1. 11 3
      coin/resources/models.py

+ 11 - 3
coin/resources/models.py

@@ -52,10 +52,18 @@ class IPSubnet(models.Model):
                 raise ValidationError('Unable to allocate an IP subnet in the specified pool: not enough space left.')
             self.inet = first_free.subnet(SUBNET_SIZE, 1).next()
         else:
-            # TODO:
+            pool = IPSet([self.ip_pool.inet])
+            subnet = IPSet([self.inet])
             # Check that we are included in the IP pool.
-            # Check that we don't conflict with existing subnets
-            pass
+            if not subnet.issubset(pool):
+                raise ValidationError('Subnet must be included in the IP pool.')
+            # Check that we don't conflict with existing subnets.
+            # TODO: use precise database query instead of querying all
+            # subnets and filtering in Python.
+            existing = IPSet((s.inet for s in self.ip_pool.ipsubnet_set.all()))
+            intersection = subnet.intersection(existing)
+            if intersection.size:
+                raise ValidationError('Subnet must not intersect with existing subnets.\nIntersected subnets: {}.'.format([str(p) for p in intersection.iter_cidrs()]))
 
     def __unicode__(self):
         return str(self.inet)