From 8c311072b7bd7b4e2f5d2ce4ffb8e0ee1e1c1809 Mon Sep 17 00:00:00 2001 From: Ryan Tidwell Date: Tue, 14 Apr 2015 15:53:02 -0700 Subject: [PATCH] Block subnet create with mismatched IP versions Cherry picked from 615102520c0df3952347c3e176b60c0ddc97040b Change-Id: Ic0a3baf0e956505999d2473ae85ebac90e0970cd Closes-Bug: 1444146 --- neutron/db/db_base_plugin_v2.py | 9 +++++++++ neutron/tests/unit/db/test_db_base_plugin_v2.py | 15 +++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/neutron/db/db_base_plugin_v2.py b/neutron/db/db_base_plugin_v2.py index 24c251340..2d9aaf31a 100644 --- a/neutron/db/db_base_plugin_v2.py +++ b/neutron/db/db_base_plugin_v2.py @@ -1267,6 +1267,15 @@ class NeutronDbPluginV2(neutron_plugin_base_v2.NeutronPluginBaseV2, with context.session.begin(subtransactions=True): subnetpool = self._get_subnetpool(context, subnetpool_id) + ip_version = s.get('ip_version') + has_ip_version = attributes.is_attr_set(ip_version) + if has_ip_version and ip_version != subnetpool.ip_version: + args = {'req_ver': str(s['ip_version']), + 'pool_ver': str(subnetpool.ip_version)} + reason = _("Cannot allocate IPv%(req_ver)s subnet from " + "IPv%(pool_ver)s subnet pool") % args + raise n_exc.BadRequest(resource='subnets', msg=reason) + network = self._get_network(context, s["network_id"]) allocator = subnet_alloc.SubnetAllocator(subnetpool) req = self._make_subnet_request(tenant_id, s, subnetpool) 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 0804e9047..a99099d3e 100644 --- a/neutron/tests/unit/db/test_db_base_plugin_v2.py +++ b/neutron/tests/unit/db/test_db_base_plugin_v2.py @@ -5297,6 +5297,21 @@ class TestSubnetPoolsV2(NeutronDbPluginV2TestCase): # Assert error self.assertEqual(res.status_int, 409) + def test_allocate_any_ipv4_subnet_ipv6_pool(self): + with self.network() as network: + sp = self._test_create_subnetpool(['2001:db8:1:2::/63'], + tenant_id=self._tenant_id, + name=self._POOL_NAME) + + # Request a specific subnet allocation + data = {'subnet': {'network_id': network['network']['id'], + 'subnetpool_id': sp['subnetpool']['id'], + 'ip_version': 4, + 'tenant_id': network['network']['tenant_id']}} + req = self.new_create_request('subnets', data) + res = req.get_response(self.api) + self.assertEqual(res.status_int, 400) + class DbModelTestCase(base.BaseTestCase): """DB model tests.""" -- 2.45.2