diff --git a/neutron_lib/api/attributes.py b/neutron_lib/api/attributes.py index 6a19aae28..4eb30acc8 100644 --- a/neutron_lib/api/attributes.py +++ b/neutron_lib/api/attributes.py @@ -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)) diff --git a/neutron_lib/tests/unit/api/test_attributes.py b/neutron_lib/tests/unit/api/test_attributes.py index c9d031399..496e366dd 100644 --- a/neutron_lib/tests/unit/api/test_attributes.py +++ b/neutron_lib/tests/unit/api/test_attributes.py @@ -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) diff --git a/releasenotes/notes/add-retrieve-sort-keys-from-attribute-map-ae53d67e0be2ace0.yaml b/releasenotes/notes/add-retrieve-sort-keys-from-attribute-map-ae53d67e0be2ace0.yaml new file mode 100644 index 000000000..19880e2f0 --- /dev/null +++ b/releasenotes/notes/add-retrieve-sort-keys-from-attribute-map-ae53d67e0be2ace0.yaml @@ -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.