From 65264a936a6c9d0a8c4eae69acaee2c3ee54b5d6 Mon Sep 17 00:00:00 2001 From: pandatt Date: Thu, 9 May 2019 10:48:48 +0800 Subject: [PATCH] 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 --- neutron_lib/api/attributes.py | 13 +++++++++++ neutron_lib/tests/unit/api/test_attributes.py | 23 +++++++++++++++++++ ...s-from-attribute-map-ae53d67e0be2ace0.yaml | 5 ++++ 3 files changed, 41 insertions(+) create mode 100644 releasenotes/notes/add-retrieve-sort-keys-from-attribute-map-ae53d67e0be2ace0.yaml 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.