Support querying resources with tags plus attributes

Previously we can only query resources either by tags
or attributes. The patch support searching resources by tags
and user specified attributes

Change-Id: I43d9db2a53cd39f113abbdfc92694f6f9e66ce03
This commit is contained in:
Erica Liu 2020-02-06 15:19:46 -08:00
parent 03fd42824e
commit 846e7ea1a3
2 changed files with 29 additions and 4 deletions

View File

@ -1792,6 +1792,25 @@ 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(self.search_path % query) search.assert_called_with(self.search_path % query)
def test_nsx_search_tags_with_extra_attribute(self):
"""Test search of resource with specified tags and one attribute."""
with mock.patch.object(self.nsxlib.client, 'url_get') as search:
user_tags = [{'tag': 'k8s'}]
query = "%s AND %s" % (self.nsxlib._build_query(tags=user_tags),
'marked_for_delete:False')
self.nsxlib.search_by_tags(tags=user_tags, marked_for_delete=False)
search.assert_called_with(self.search_path % query)
def test_nsx_search_tags_with_multi_attributes(self):
"""Test search of resource with tags and multiple attributes."""
with mock.patch.object(self.nsxlib.client, 'url_get') as search:
user_tags = [{'tag': 'k8s'}]
query = "%s AND %s" % (self.nsxlib._build_query(tags=user_tags),
'tea:boo AND coffee:False')
self.nsxlib.search_by_tags(
tags=user_tags, tea='boo', coffee=False)
search.assert_called_with(self.search_path % query)
def test_nsx_search_by_resouce_type_and_attributes(self): def test_nsx_search_by_resouce_type_and_attributes(self):
with mock.patch.object(self.nsxlib.client, 'url_get') as search: with mock.patch.object(self.nsxlib.client, 'url_get') as search:
resource_type = 'HorseWithNoName' resource_type = 'HorseWithNoName'

View File

@ -138,7 +138,7 @@ class NsxLibBase(object):
# TODO(abhiraut): Revisit this method to generate complex boolean # TODO(abhiraut): Revisit this method to generate complex boolean
# queries to search resources. # queries to search resources.
def search_by_tags(self, tags, resource_type=None, cursor=None, def search_by_tags(self, tags, resource_type=None, cursor=None,
page_size=None): page_size=None, **extra_attrs):
"""Return the list of resources searched based on tags. """Return the list of resources searched based on tags.
Currently the query only supports AND boolean operator. Currently the query only supports AND boolean operator.
@ -150,6 +150,8 @@ class NsxLibBase(object):
:param cursor: Opaque cursor to be used for getting next page of :param cursor: Opaque cursor to be used for getting next page of
records (supplied by current result page). records (supplied by current result page).
:param page_size: Maximum number of results to return in this page. :param page_size: Maximum number of results to return in this page.
:param extra_attrs: Support querying by user specified attributes.
Multiple attributes will be ANDed.
""" """
if not tags: if not tags:
reason = _("Missing required argument 'tags'") reason = _("Missing required argument 'tags'")
@ -161,6 +163,9 @@ class NsxLibBase(object):
query += " AND %s" % query_tags query += " AND %s" % query_tags
else: else:
query = query_tags query = query_tags
if extra_attrs:
query += " AND %s" % " AND ".join(
['%s:%s' % (k, v) for (k, v) in extra_attrs.items()])
url = self._add_pagination_parameters(self._get_search_url() % query, url = self._add_pagination_parameters(self._get_search_url() % query,
cursor, page_size) cursor, page_size)
@ -209,13 +214,14 @@ class NsxLibBase(object):
return do_search(url) return do_search(url)
def search_all_by_tags(self, tags, resource_type=None): def search_all_by_tags(self, tags, resource_type=None, **extra_attrs):
"""Return all the results searched based on tags.""" """Return all the results searched based on tags."""
results = [] results = []
cursor = 0 cursor = 0
while True: while True:
response = self.search_by_tags(resource_type=resource_type, response = self.search_by_tags(
tags=tags, cursor=cursor) resource_type=resource_type, tags=tags, cursor=cursor,
**extra_attrs)
if not response['results']: if not response['results']:
return results return results
results.extend(response['results']) results.extend(response['results'])