Fix error 500 during segment range creation

When new network segment range is created for "vlan" network,
specify physical network is required.
It wasn't checked so there was ugly error 500 returned to the user
and ugly stacktrace in neutron logs.

Now it is validated and error 400 with proper description of
what is missing is returned.

Change-Id: Id3809a62f0e5d74949b34788b08af2e2aec879d8
Closes-Bug: #1823297
This commit is contained in:
Slawek Kaplonski 2019-04-05 10:26:36 +02:00
parent 02ce7aeebc
commit 84ba64a6a6
2 changed files with 27 additions and 3 deletions

View File

@ -13,6 +13,7 @@
# under the License.
from neutron_lib.api.definitions import network_segment_range as range_def
from neutron_lib.api import validators
from neutron_lib import constants as const
from neutron_lib.db import api as db_api
from neutron_lib import exceptions as lib_exc
@ -40,6 +41,20 @@ def is_network_segment_range_enabled():
for p in ['network_segment_range', network_segment_range_class])
def _get_physical_network(network_segment_range):
if network_segment_range.get('network_type') != const.TYPE_VLAN:
return None
physical_network = network_segment_range.get(
"physical_network", const.ATTR_NOT_SPECIFIED)
if not validators.is_attr_set(physical_network):
message = _("Network type %s requires 'physical_network' to be "
"specified while creating new range") % const.TYPE_VLAN
raise lib_exc.BadRequest(resource=range_def.RESOURCE_NAME,
msg=message)
return physical_network
class NetworkSegmentRangePlugin(ext_range.NetworkSegmentRangePluginBase):
"""Implements Neutron Network Segment Range Service plugin."""
@ -77,9 +92,7 @@ class NetworkSegmentRangePlugin(ext_range.NetworkSegmentRangePluginBase):
filters = {
'default': False,
'network_type': network_segment_range['network_type'],
'physical_network': (network_segment_range['physical_network']
if network_segment_range['network_type'] ==
const.TYPE_VLAN else None),
'physical_network': _get_physical_network(network_segment_range)
}
range_objs = obj_network_segment_range.NetworkSegmentRange.get_objects(
context, **filters)

View File

@ -175,6 +175,17 @@ class TestNetworkSegmentRange(testlib_api.SqlTestCase):
self.context,
network_segment_range)
def test_create_network_segment_range_missing_physical_network_for_vlan(
self):
test_range = self._vlan_range.copy()
test_range.pop("physical_network")
network_segment_range = {'network_segment_range': test_range}
self.assertRaises(
exc.NeutronException,
self.plugin.create_network_segment_range,
self.context,
network_segment_range)
def test_update_network_segment_range(self):
test_range = self._vlan_range
network_segment_range = {'network_segment_range': test_range}