]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Add check for cidr overrapping for adding external gateway
authorNachi Ueno <nachi@nttmcl.com>
Fri, 21 Sep 2012 20:52:16 +0000 (20:52 +0000)
committerNachi Ueno <nachi@nttmcl.com>
Wed, 31 Oct 2012 20:46:36 +0000 (20:46 +0000)
Fixes bug 1053633
Also add check for cidr overrapping between external gateway and
interfaces

Change-Id: I5bfb2fd96ea467b63e940893979a912caf550deb

quantum/db/l3_db.py
quantum/tests/unit/test_l3_plugin.py

index 8b78bf1e2c358e1fec248df4899e8c059b6707a6..21c340955b5845c323161d504a43cbd5b2073caf 100644 (file)
@@ -193,6 +193,12 @@ class L3_NAT_db_mixin(l3.RouterPluginBase):
 
         if network_id is not None and (gw_port is None or
                                        gw_port['network_id'] != network_id):
+            subnets = self._get_subnets_by_network(context,
+                                                   network_id)
+            for subnet in subnets:
+                self._check_for_dup_router_subnet(context, router_id,
+                                                  network_id, subnet['id'])
+
             # Port has no 'tenant-id', as it is hidden from user
             gw_port = self.create_port(context.elevated(), {
                 'port':
@@ -250,8 +256,7 @@ class L3_NAT_db_mixin(l3.RouterPluginBase):
         try:
             rport_qry = context.session.query(models_v2.Port)
             rports = rport_qry.filter_by(
-                device_id=router_id,
-                device_owner=DEVICE_OWNER_ROUTER_INTF,).all()
+                device_id=router_id).all()
             # its possible these ports on on the same network, but
             # different subnet
             new_cidr = self._get_subnet(context, subnet_id)['cidr']
index c404992560f59301f29ee15eab3ff09249ecf9fb..eae343dea46a6e49efcdaa927d701ba28c5355c9 100644 (file)
@@ -561,6 +561,40 @@ class L3NatDBTestCase(test_db_plugin.QuantumDbPluginV2TestCase):
                                                       None,
                                                       p1['port']['id'])
 
+    def test_router_add_gateway_dup_subnet1(self):
+        with self.router() as r:
+            with self.subnet() as s:
+                body = self._router_interface_action('add',
+                                                     r['router']['id'],
+                                                     s['subnet']['id'],
+                                                     None)
+                self._set_net_external(s['subnet']['network_id'])
+                self._add_external_gateway_to_router(
+                    r['router']['id'],
+                    s['subnet']['network_id'],
+                    expected_code=exc.HTTPBadRequest.code)
+                body = self._router_interface_action('remove',
+                                                     r['router']['id'],
+                                                     s['subnet']['id'],
+                                                     None)
+
+    def test_router_add_gateway_dup_subnet2(self):
+        with self.router() as r:
+            with self.subnet() as s:
+                self._set_net_external(s['subnet']['network_id'])
+                self._add_external_gateway_to_router(
+                    r['router']['id'],
+                    s['subnet']['network_id'])
+                self._router_interface_action('add',
+                                              r['router']['id'],
+                                              s['subnet']['id'],
+                                              None,
+                                              expected_code=exc.
+                                              HTTPBadRequest.code)
+                self._remove_external_gateway_from_router(
+                    r['router']['id'],
+                    s['subnet']['network_id'])
+
     def test_router_add_interface_overlapped_cidr(self):
         with self.router() as r:
             with self.subnet(cidr='10.0.1.0/24') as s1:
@@ -763,7 +797,7 @@ class L3NatDBTestCase(test_db_plugin.QuantumDbPluginV2TestCase):
 
     @contextlib.contextmanager
     def floatingip_with_assoc(self, port_id=None, fmt='json'):
-        with self.subnet() as public_sub:
+        with self.subnet(cidr='11.0.0.0/24') as public_sub:
             self._set_net_external(public_sub['subnet']['network_id'])
             with self.port() as private_port:
                 with self.router() as r:
@@ -794,7 +828,7 @@ class L3NatDBTestCase(test_db_plugin.QuantumDbPluginV2TestCase):
 
     @contextlib.contextmanager
     def floatingip_no_assoc(self, private_sub, fmt='json'):
-        with self.subnet() as public_sub:
+        with self.subnet(cidr='12.0.0.0/24') as public_sub:
             self._set_net_external(public_sub['subnet']['network_id'])
             with self.router() as r:
                 self._add_external_gateway_to_router(
@@ -831,7 +865,7 @@ class L3NatDBTestCase(test_db_plugin.QuantumDbPluginV2TestCase):
 
     def test_floatingip_with_assoc_fails(self):
         fmt = 'json'
-        with self.subnet() as public_sub:
+        with self.subnet(cidr='200.0.0.1/24') as public_sub:
             self._set_net_external(public_sub['subnet']['network_id'])
             with self.port() as private_port:
                 with self.router() as r: