Add sort_keys retrieving function

This patch provides a tool function to retrieve valid sort keys
from `attr_info` dict. And it's necessary prerequisite to fix
bug-1659175.

Related-bug: #1659175
Change-Id: I5ef7a0730122011f5ef18c1b4e2c0bfe82ed585b
This commit is contained in:
pandatt 2019-05-09 10:48:48 +08:00 committed by Brian Haley
parent b04ea0d031
commit 65264a936a
3 changed files with 41 additions and 0 deletions

View File

@ -297,3 +297,16 @@ def _core_resource_attributes():
# populate core resources into singleton global
RESOURCES = _core_resource_attributes()
def retrieve_valid_sort_keys(attr_info):
"""Retrieve sort keys from `attr_info` dict.
Iterate the `attr_info`, filter and return the attributes that are
defined with `is_sort_key=True`.
:param attr_info: The attribute dict for common neutron resource.
:returns: Set of sort keys.
"""
return set(attr for attr, schema in attr_info.items()
if schema.get('is_sort_key', False))

View File

@ -321,3 +321,26 @@ class TestValidatePriviliges(base.BaseTestCase):
attributes._validate_privileges(ctx, res_dict)
except exc.HTTPBadRequest:
self.fail("HTTPBadRequest exception should not be raised.")
class TestRetrieveValidSortKeys(base.BaseTestCase):
def test_retrieve_valid_sort_keys(self):
attr_info = {
"id": {
"visible": True,
"is_sort_key": True
},
"name": {
"is_sort_key": True
},
"created_at": {
"is_sort_key": False
},
"tenant_id": {
"visible": True,
}
}
expect_val = set(["id", "name"])
actual_val = attributes.retrieve_valid_sort_keys(attr_info)
self.assertEqual(expect_val, actual_val)

View File

@ -0,0 +1,5 @@
---
features:
- |
A new method ``retrieve_valid_sort_keys`` was added to ``neutron_lib.api.attributes``.
This method can help retrieve valid sort keys from a given resource attribute map.