[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
objects.
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
This commit is contained in:
Abhishek Raut 2017-01-17 13:39:51 -08:00
parent 627964757b
commit f20ebba9ef
2 changed files with 2 additions and 2 deletions

View File

@ -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)

View File

@ -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
else: