Skip to content

Commit

Permalink
[config] Ensure Tag is unique
Browse files Browse the repository at this point in the history
As Tag fq_name consists in the type and value, the Tag should be unique.
But before that patch, the Tag fq_name was build after the API verified
the fq_name uniqueness.
Also fix the unit test which returned a false positive result.

Change-Id: I6aa03270cdd9e5bdc46c3d1e9b1dd6d2f8c9d842
Closes-Bug: #1731817
  • Loading branch information
Édouard Thuleau committed Nov 15, 2017
1 parent c1a4441 commit ebdade3
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 25 deletions.
39 changes: 30 additions & 9 deletions src/config/api-server/tests/test_tag.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,16 @@ def test_not_deallocate_tag_type_id_if_value_does_not_correspond(self):


class TestTag(TestTagBase):
STALE_LOCK_SECS = '0.2'

@classmethod
def setUpClass(cls):
super(TestTag, cls).setUpClass(
extra_config_knobs=[
('DEFAULTS', 'stale_lock_seconds', cls.STALE_LOCK_SECS),
]
)

def test_tag_name_is_composed_with_type_and_value(self):
name = 'fake-name-%s' % self.id()
type = 'fake_type-%s' % self.id()
Expand All @@ -139,6 +149,26 @@ def test_tag_name_is_composed_with_type_and_value(self):
self.assertEqual(tag.tag_type_name, type)
self.assertEqual(tag.tag_value, value)

def test_tag_is_unique(self):
project = Project('project-%s' % self.id())
self.api.project_create(project)
type = 'fake_type-%s' % self.id()
value = 'fake_value-%s' % self.id()
tag1 = Tag(tag_type_name=type, tag_value=value)
self.api.tag_create(tag1)
scoped_tag1 = Tag(tag_type_name=type, tag_value=value,
parent_obj=project)
self.api.tag_create(scoped_tag1)
gevent.sleep(float(self.STALE_LOCK_SECS))

tag2 = Tag(tag_type_name=type, tag_value=value)
with ExpectedException(exceptions.RefsExistError):
self.api.tag_create(tag2)
scoped_tag2 = Tag(tag_type_name=type, tag_value=value,
parent_obj=project)
with ExpectedException(exceptions.RefsExistError):
self.api.tag_create(scoped_tag2)

def test_tag_type_is_mandatory(self):
value = 'fake_value-%s' % self.id()
tag = Tag(tag_value=value)
Expand Down Expand Up @@ -302,15 +332,6 @@ def test_pre_defined_tag_type_is_not_deleted_even_if_not_use(self):
tag_type = self.api.tag_type_read(id=tag_type_uuid)
self.assertEqual(tag_type_uuid, tag_type.uuid)

def test_tag_is_unique_in_same_scope(self):
type = 'fake_type-%s' % self.id()
value = 'fake_value-%s' % self.id()
tag = Tag(tag_type_name=type, tag_value=value)
self.api.tag_create(tag)
with ExpectedException(exceptions.RefsExistError):
new_tag = Tag(tag_type_name=type, tag_value=value)
self.api.tag_create(new_tag)

def test_tag_type_is_allocated(self):
type = 'fake_type-%s' % self.id()
value = 'fake_value-%s' % self.id()
Expand Down
13 changes: 7 additions & 6 deletions src/config/api-server/vnc_cfg_api_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,13 @@ def http_resource_create(self, obj_type):
result = 'Bad reference in create: ' + result
raise cfgm_common.exceptions.HttpError(400, result)

get_context().set_state('PRE_DBE_ALLOC')
# type-specific hook
(ok, result) = r_class.pre_dbe_alloc(obj_type, obj_dict)
if not ok:
code, msg = result
raise cfgm_common.exceptions.HttpError(code, msg)

# common handling for all resource create
(ok, result) = self._post_common(obj_type, obj_dict)
if not ok:
Expand Down Expand Up @@ -556,12 +563,6 @@ def http_resource_create(self, obj_type):
quota_counter = []

def stateful_create():
get_context().set_state('PRE_DBE_ALLOC')
# type-specific hook
(ok, result) = r_class.pre_dbe_alloc(obj_type, obj_dict)
if not ok:
return (ok, result)

get_context().set_state('DBE_ALLOC')
# Alloc and Store id-mappings before creating entry on pubsub store.
# Else a subscriber can ask for an id mapping before we have stored it
Expand Down
10 changes: 0 additions & 10 deletions src/config/api-server/vnc_cfg_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -3380,16 +3380,6 @@ def pre_dbe_create(cls, tenant_name, obj_dict, db_conn):
if not ok:
return ok, result

# TODO(ethuleau): As we keep the virtual network ID allocation in
# schema and in the vnc API for one release overlap to
# prevent any upgrade issue, we still authorize to
# set or update the virtual network ID until release
# (3.2 + 1)
# # Does not authorize to set the security group ID as it's allocated
# # by the vnc server
# if obj_dict.get('security_group_id') is not None:
# return (False, (403, "Cannot set the security group ID"))

# Allocate security group ID if necessary
return cls._set_configured_security_group_id(obj_dict)
# end pre_dbe_create
Expand Down

0 comments on commit ebdade3

Please sign in to comment.