]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Check gateway ip when update subnet
authorshihanzhang <shihanzhang@huawei.com>
Sat, 31 Oct 2015 09:01:48 +0000 (17:01 +0800)
committershihanzhang <shihanzhang@huawei.com>
Mon, 23 Nov 2015 06:27:31 +0000 (14:27 +0800)
If a IP address is None, the method netaddr.IPAddress will raise a
error as 'failed to detect a valid IP address from None'.
When it updates subnet, if gateway_ip is None, it should not call the
method validate_gw_out_of_pools.

Change-Id: Ibb007fe7e6402c174129074288b8af891182cd68
Closes-bug: #1511925

neutron/db/db_base_plugin_v2.py
neutron/tests/unit/db/test_db_base_plugin_v2.py

index cebb92aa43e337f35ba472117c662817edfe0f0c..cb1b9439b7ce73414900c5ccd30db315f6546df4 100644 (file)
@@ -734,13 +734,12 @@ class NeutronDbPluginV2(db_base_plugin_common.DbBasePluginCommon,
             s['allocation_pools'] = range_pools
 
         # If either gateway_ip or allocation_pools were specified
-        new_gateway_ip = s.get('gateway_ip')
-        gateway_ip_changed = (new_gateway_ip and
-                              new_gateway_ip != db_subnet.gateway_ip)
+        gateway_ip = s.get('gateway_ip', db_subnet.gateway_ip)
+        gateway_ip_changed = gateway_ip != db_subnet.gateway_ip
         if gateway_ip_changed or s.get('allocation_pools') is not None:
-            gateway_ip = new_gateway_ip or db_subnet.gateway_ip
             pools = range_pools if range_pools is not None else db_pools
-            self.ipam.validate_gw_out_of_pools(gateway_ip, pools)
+            if gateway_ip:
+                self.ipam.validate_gw_out_of_pools(gateway_ip, pools)
 
         if gateway_ip_changed:
             # Provide pre-update notification not to break plugins that don't
index 9be3545dfe28140e079c7c5979b2fe4bceda1c2d..e07a3336b606661fc4672b51d215015c825dce39 100644 (file)
@@ -4754,6 +4754,37 @@ class TestSubnetsV2(NeutronDbPluginV2TestCase):
             self.assertEqual(data['subnet']['host_routes'],
                              res['subnet']['host_routes'])
 
+    def _test_update_subnet(self, old_gw=None, new_gw=None,
+                            check_gateway=False):
+        allocation_pools = [{'start': '192.168.0.16', 'end': '192.168.0.254'}]
+        with self.network() as network:
+            with self.subnet(network=network,
+                             gateway_ip=old_gw,
+                             allocation_pools=allocation_pools,
+                             cidr='192.168.0.0/24') as subnet:
+                data = {
+                    'subnet': {
+                        'allocation_pools': [
+                            {'start': '192.168.0.10', 'end': '192.168.0.20'},
+                            {'start': '192.168.0.30', 'end': '192.168.0.40'}],
+                        'gateway_ip': new_gw}}
+                req = self.new_update_request('subnets', data,
+                                              subnet['subnet']['id'])
+                res = req.get_response(self.api)
+                self.assertEqual(200, res.status_code)
+                self._verify_updated_subnet_allocation_pools(
+                    res, with_gateway_ip=check_gateway)
+
+    def test_update_subnet_from_no_gw_to_no_gw(self):
+        self._test_update_subnet()
+
+    def test_update_subnet_from_gw_to_no_gw(self):
+        self._test_update_subnet(old_gw='192.168.0.15')
+
+    def test_update_subnet_from_gw_to_new_gw(self):
+        self._test_update_subnet(old_gw='192.168.0.15',
+                                 new_gw='192.168.0.9', check_gateway=True)
+
     def test_update_subnet_route_with_too_many_entries(self):
         with self.subnet() as subnet:
             data = {'subnet': {'host_routes': [