Bug fix: filter topology by project_id also when there is a query

Previously, if vitrage topology show was called with a filter parameter,
the project_id was not added to the query so the returned topology
included resources of other projects as well.

Example:
vitrage topology show --filter "{ \"==\" : {\"vitrage_type\" : \"nova.instance\"} }"

Returned instances of other projects.

Change-Id: Icd6391530695fdb31b28eecd627e3ffc9427dd1d
This commit is contained in:
Ifat Afek 2018-03-20 10:33:07 +00:00
parent 7fc2d5e2a0
commit 683b7a3cdb
2 changed files with 25 additions and 5 deletions

View File

@ -90,8 +90,8 @@ RESOURCES_ALL_QUERY = {
class EntityGraphApisBase(object):
@staticmethod
def _get_query_with_project(vitrage_category, project_id, is_admin):
@classmethod
def _get_query_with_project(cls, vitrage_category, project_id, is_admin):
"""Generate query with tenant data
Creates query for entity graph which takes into consideration the
@ -111,6 +111,22 @@ class EntityGraphApisBase(object):
]
}
cls._add_project_to_query(query, project_id, is_admin)
return query
@staticmethod
def _add_project_to_query(query, project_id, is_admin):
"""Add project_id filter to the query
Each query should contain the project_id condition
:type query: string representing a json query
:type project_id: string
:type is_admin: boolean
:rtype: string representing a json query
"""
if is_admin:
project_query = \
{'or': [{'==': {VProps.PROJECT_ID: project_id}},
@ -119,9 +135,13 @@ class EntityGraphApisBase(object):
project_query = \
{'==': {VProps.PROJECT_ID: project_id}}
query['and'].append(project_query)
if 'and' in query:
query_with_project_id = query
query_with_project_id['and'].append(project_query)
else:
query_with_project_id = {'and': [project_query, query]}
return query
return query_with_project_id
def _filter_alarms(self, alarms, project_id):
"""Remove wrong alarms from the list

View File

@ -104,7 +104,7 @@ class TopologyApis(EntityGraphApisBase):
"""
if query:
q = query
q = self._add_project_to_query(query, project_id, is_admin_project)
else:
alarm_query = self._get_query_with_project(EntityCategory.ALARM,
project_id,