Check project_id when creating and updating NetworkSegmentRange

If "shared" is False, a "NetworkSegmentRange" object must have a
"project_id".

Change-Id: Ie61117d1a81ab1d0f740ea65bcc003ec88d7af1d
Related-Bug: #1835914
(cherry picked from commit 7875400f95)
This commit is contained in:
Rodolfo Alonso Hernandez 2019-07-17 17:24:42 +00:00
parent fe6120f0c3
commit 2d319eeadc
2 changed files with 45 additions and 0 deletions

View File

@ -78,6 +78,21 @@ class NetworkSegmentRange(base.NeutronDbObject):
pass
return db_utils.resource_fields(_dict, fields)
def _check_shared_project_id(self, action):
if self.shared is False and not self.project_id:
raise n_exc.ObjectActionError(
action=action,
reason='if NetworkSegmentRange is not shared, it must have a '
'project_id')
def create(self):
self._check_shared_project_id('create')
super(NetworkSegmentRange, self).create()
def update(self):
self._check_shared_project_id('update')
super(NetworkSegmentRange, self).update()
def _get_allocation_model_details(self):
model = models_map.get(self.network_type)
if model is not None:

View File

@ -16,6 +16,7 @@ import random
import mock
from neutron_lib import constants
from neutron_lib import exceptions as n_exc
from neutron_lib.utils import helpers
from oslo_utils import uuidutils
@ -136,3 +137,32 @@ class NetworkSegmentRangeDbObjectTestCase(obj_test_base.BaseDbObjectTestCase,
maximum=max(list(alloc_mapping.keys())))
ret_alloc_mapping = self._test_class._get_used_allocation_mapping(obj)
self.assertDictEqual(alloc_mapping, ret_alloc_mapping)
def _define_network_segment_range(self, shared=False,
remove_project_id=False):
attrs = self.get_random_object_fields(obj_cls=self._test_class)
obj = self._test_class(self.context, **attrs)
obj.shared = shared
obj.project_id = None if remove_project_id else obj.project_id
return obj
def test_create_not_shared_with_project_id(self):
obj = self._define_network_segment_range()
obj.create()
def test_create_not_shared_without_project_id(self):
obj = self._define_network_segment_range(remove_project_id=True)
self.assertRaises(n_exc.ObjectActionError, obj.create)
def test_update_not_shared_with_project_id(self):
obj = self._define_network_segment_range(shared=True)
obj.create()
obj.shared = False
obj.update()
def test_update_not_shared_without_project_id(self):
obj = self._define_network_segment_range(shared=True,
remove_project_id=True)
obj.create()
obj.shared = False
self.assertRaises(n_exc.ObjectActionError, obj.update)