From: shihanzhang Date: Sat, 31 Oct 2015 09:01:48 +0000 (+0800) Subject: Check gateway ip when update subnet X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=935348ce929ccdedc76e2e5742f312af9bbb12b6;p=openstack-build%2Fneutron-build.git Check gateway ip when update subnet 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 --- diff --git a/neutron/db/db_base_plugin_v2.py b/neutron/db/db_base_plugin_v2.py index cebb92aa4..cb1b9439b 100644 --- a/neutron/db/db_base_plugin_v2.py +++ b/neutron/db/db_base_plugin_v2.py @@ -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 diff --git a/neutron/tests/unit/db/test_db_base_plugin_v2.py b/neutron/tests/unit/db/test_db_base_plugin_v2.py index 9be3545df..e07a3336b 100644 --- a/neutron/tests/unit/db/test_db_base_plugin_v2.py +++ b/neutron/tests/unit/db/test_db_base_plugin_v2.py @@ -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': [