Enable search_by_tags to use only scope or tag
Enable more refined searches and not enforce that a scope and a tag are required. Change-Id: I542dd84a6690410744a22328001ac9c2de0a53b6
This commit is contained in:
parent
989e777ab4
commit
f1b1dcd98b
@ -1135,6 +1135,30 @@ class TestNsxSearch(nsxlib_testcase.NsxClientTestCase):
|
|||||||
self.nsxlib.search_by_tags(tags=user_tags)
|
self.nsxlib.search_by_tags(tags=user_tags)
|
||||||
search.assert_called_with('search?query=%s' % query)
|
search.assert_called_with('search?query=%s' % query)
|
||||||
|
|
||||||
|
def test_nsx_search_tags_scope_only(self):
|
||||||
|
"""Test search of resources with the specified tag."""
|
||||||
|
with mock.patch.object(self.nsxlib.client, 'url_get') as search:
|
||||||
|
user_tags = [{'scope': 'user'}]
|
||||||
|
query = self.nsxlib._build_query(tags=user_tags)
|
||||||
|
self.nsxlib.search_by_tags(tags=user_tags)
|
||||||
|
search.assert_called_with('search?query=%s' % query)
|
||||||
|
|
||||||
|
def test_nsx_search_tags_tag_only(self):
|
||||||
|
"""Test search of resources with the specified tag."""
|
||||||
|
with mock.patch.object(self.nsxlib.client, 'url_get') as search:
|
||||||
|
user_tags = [{'tag': 'k8s'}]
|
||||||
|
query = self.nsxlib._build_query(tags=user_tags)
|
||||||
|
self.nsxlib.search_by_tags(tags=user_tags)
|
||||||
|
search.assert_called_with('search?query=%s' % query)
|
||||||
|
|
||||||
|
def test_nsx_search_tags_tag_and_scope(self):
|
||||||
|
"""Test search of resources with the specified tag."""
|
||||||
|
with mock.patch.object(self.nsxlib.client, 'url_get') as search:
|
||||||
|
user_tags = [{'tag': 'k8s'}, {'scope': 'user'}]
|
||||||
|
query = self.nsxlib._build_query(tags=user_tags)
|
||||||
|
self.nsxlib.search_by_tags(tags=user_tags)
|
||||||
|
search.assert_called_with('search?query=%s' % query)
|
||||||
|
|
||||||
def test_nsx_search_tags_and_resource_type(self):
|
def test_nsx_search_tags_and_resource_type(self):
|
||||||
"""Test search of specified resource with the specified tag."""
|
"""Test search of specified resource with the specified tag."""
|
||||||
with mock.patch.object(self.nsxlib.client, 'url_get') as search:
|
with mock.patch.object(self.nsxlib.client, 'url_get') as search:
|
||||||
|
@ -188,13 +188,22 @@ class NsxLibBase(object):
|
|||||||
operation=msg,
|
operation=msg,
|
||||||
details='')
|
details='')
|
||||||
|
|
||||||
def _build_query(self, tags):
|
def _build_tag_query(self, tag):
|
||||||
try:
|
# Validate that the correct keys are used
|
||||||
return " AND ".join(['tags.scope:%(scope)s AND '
|
if set(tag.keys()) - set(('scope', 'tag')):
|
||||||
'tags.tag:%(tag)s' % item for item in tags])
|
reason = _("Only 'scope' and 'tag' keys are supported")
|
||||||
except KeyError as e:
|
|
||||||
reason = _('Missing key:%s in tags') % str(e)
|
|
||||||
raise exceptions.NsxSearchInvalidQuery(reason=reason)
|
raise exceptions.NsxSearchInvalidQuery(reason=reason)
|
||||||
|
_scope = tag.get('scope')
|
||||||
|
_tag = tag.get('tag')
|
||||||
|
if _scope and _tag:
|
||||||
|
return 'tags.scope:%s AND tags.tag:%s' % (_scope, _tag)
|
||||||
|
elif _scope:
|
||||||
|
return 'tags.scope:%s' % _scope
|
||||||
|
else:
|
||||||
|
return 'tags.tag:%s' % _tag
|
||||||
|
|
||||||
|
def _build_query(self, tags):
|
||||||
|
return " AND ".join([self._build_tag_query(item) for item in tags])
|
||||||
|
|
||||||
def get_tag_limits(self):
|
def get_tag_limits(self):
|
||||||
try:
|
try:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user