Filter project_id for list limits

When list limits, if more than one filter is provided and
"project_id" is contained, the limits should be filtered
with "project_id" as well.

Closes-bug: #1780896
Change-Id: I7c231130358ab8d7b0b2f507c52d34366ca0060a
This commit is contained in:
wangxiyuan 2018-07-10 10:55:13 +08:00
parent 5a52e5aab7
commit 4cdb4a0b72
2 changed files with 25 additions and 2 deletions

View File

@ -314,8 +314,9 @@ class UnifiedLimit(base.UnifiedLimitDriverBase):
query,
hints)
old_format_data = [s.to_dict() for s in limits]
if hint_copy.filters and (not hint_copy.get_exact_filter_by_name(
'project_id') or len(hint_copy.filters) > 1):
project_filter = hint_copy.get_exact_filter_by_name('project_id')
if hint_copy.filters and (not project_filter
or len(hint_copy.filters) > 1):
# If the hints contain "service_id", "region_id" or
# "resource_name", we should combine the registered_limit table
# first to fetch these information.
@ -324,6 +325,9 @@ class UnifiedLimit(base.UnifiedLimitDriverBase):
limits = sql.filter_limit_query(RegisteredLimitModel,
query_new,
hint_copy)
if project_filter:
limits = limits.filter(
LimitModel.project_id == project_filter['value'])
new_format_data = [s.to_dict() for s in limits]
return old_format_data + new_format_data

View File

@ -614,6 +614,25 @@ class LimitTests(object):
res = PROVIDERS.unified_limit_api.list_limits(hints)
self.assertEqual(0, len(res))
def test_list_limit_by_multi_filter_with_project_id(self):
limit_1 = unit.new_limit_ref(
project_id=self.tenant_bar['id'],
service_id=self.service_one['id'],
region_id=self.region_one['id'],
resource_name='volume', resource_limit=10, id=uuid.uuid4().hex)
limit_2 = unit.new_limit_ref(
project_id=self.tenant_baz['id'],
service_id=self.service_one['id'],
region_id=self.region_two['id'],
resource_name='snapshot', resource_limit=10, id=uuid.uuid4().hex)
PROVIDERS.unified_limit_api.create_limits([limit_1, limit_2])
hints = driver_hints.Hints()
hints.add_filter('service_id', self.service_one['id'])
hints.add_filter('project_id', self.tenant_bar['id'])
res = PROVIDERS.unified_limit_api.list_limits(hints)
self.assertEqual(1, len(res))
def test_get_limit(self):
# create two limits
limit_1 = unit.new_limit_ref(