[NSX Search]: Append resource_type while limiting scope for a resource

Currently the search API accepts resource_type as an optional param
which is used to limit the scope of the search to a given type of
resource. This in turn searches the backend for all fields of all
For example, a search for LogicalSwitch resource_type may return
NSGroups which contain a membership criteria for LogicalSwitch.
In this case NSGroups are returned since the target_type field for
the NSGroup is set to LogicalSwitch.
In order to correctly return only objects of type LogicalSwitch, the
query must have resource_type:LogicalSwitch instead of LogicalSwitch.

Change-Id: I0418c0a758b28ec46b77a7adaf2dbc3addac6da3
Abhishek Raut 6 years ago
parent 627964757b
commit f20ebba9ef

@ -796,7 +796,7 @@ class TestNsxSearch(nsxlib_testcase.NsxClientTestCase):
res_type = 'LogicalPort'
query = self.nsxlib._build_query(tags=user_tags)
# Add resource_type to the query
query = "%s AND %s" % (res_type, query)
query = "resource_type:%s AND %s" % (res_type, query)
self.nsxlib.search_by_tags(tags=user_tags, resource_type=res_type)
search.assert_called_with('search?query=%s' % query)

@ -118,7 +118,7 @@ class NsxLib(object):
raise exceptions.NsxSearchInvalidQuery(reason=reason)
# Query will return nothing if the same scope is repeated.
query_tags = self._build_query(tags)
query = resource_type
query = 'resource_type:%s' % resource_type if resource_type else None
if query:
query += " AND %s" % query_tags