From ea1d5c74b49f0baaf754058285b1c68091c38e12 Mon Sep 17 00:00:00 2001
From: James Palmer <jpalmer7698@gmail.com>
Date: Wed, 17 Feb 2021 14:39:22 -0600
Subject: [PATCH] Add support for Resource Filters

Introduct the resource_filters resource, its attributes,
and API Calls for interacting with resource_filters.

Task: 41813 Story: 2008619
Change-Id: I03f618648da1f11583fb0dae990e0163ddc0624e
Signed-off-by: James Palmer <jpalmer7698@gmail.com>
---
 openstack/block_storage/v3/_proxy.py          |  8 +++
 openstack/block_storage/v3/resource_filter.py | 33 ++++++++++++
 .../block_storage/v3/test_resource_filters.py | 24 +++++++++
 .../tests/unit/block_storage/v3/test_proxy.py |  5 ++
 .../block_storage/v3/test_resource_filter.py  | 52 +++++++++++++++++++
 5 files changed, 122 insertions(+)
 create mode 100644 openstack/block_storage/v3/resource_filter.py
 create mode 100644 openstack/tests/functional/block_storage/v3/test_resource_filters.py
 create mode 100644 openstack/tests/unit/block_storage/v3/test_resource_filter.py

diff --git a/openstack/block_storage/v3/_proxy.py b/openstack/block_storage/v3/_proxy.py
index c0276a74c..58d7e6bd7 100644
--- a/openstack/block_storage/v3/_proxy.py
+++ b/openstack/block_storage/v3/_proxy.py
@@ -15,6 +15,7 @@ from openstack.block_storage.v3 import availability_zone
 from openstack.block_storage.v3 import backup as _backup
 from openstack.block_storage.v3 import capabilities as _capabilities
 from openstack.block_storage.v3 import limits as _limits
+from openstack.block_storage.v3 import resource_filter as _resource_filter
 from openstack.block_storage.v3 import snapshot as _snapshot
 from openstack.block_storage.v3 import stats as _stats
 from openstack.block_storage.v3 import type as _type
@@ -589,6 +590,13 @@ class Proxy(_base_proxy.BaseBlockStorageProxy):
         """
         return resource.wait_for_delete(self, res, interval, wait)
 
+    def resource_filters(self, **query):
+        """Retrieve a generator of resource filters
+
+        :returns: A generator of resource filters.
+        """
+        return self._list(_resource_filter.ResourceFilter, **query)
+
     def _get_cleanup_dependencies(self):
         return {
             'block_storage': {
diff --git a/openstack/block_storage/v3/resource_filter.py b/openstack/block_storage/v3/resource_filter.py
new file mode 100644
index 000000000..f26b46c09
--- /dev/null
+++ b/openstack/block_storage/v3/resource_filter.py
@@ -0,0 +1,33 @@
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
+# not use this file except in compliance with the License. You may obtain
+# a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+
+from openstack import resource
+
+
+class ResourceFilter(resource.Resource):
+    """Resource Filter"""
+    resources_key = "resource_filters"
+    base_path = "/resource_filters"
+
+    _query_mapping = resource.QueryParameters('resource')
+
+    # Capabilities
+    allow_list = True
+
+    # resource_filters introduced in 3.33
+    _max_microversion = '3.33'
+
+    #: Properties
+    #: The list of filters that are applicable to the specified resource.
+    filters = resource.Body('filters', type=list)
+    #: The resource that the filters will be applied to.
+    resource = resource.Body('resource', type=str)
diff --git a/openstack/tests/functional/block_storage/v3/test_resource_filters.py b/openstack/tests/functional/block_storage/v3/test_resource_filters.py
new file mode 100644
index 000000000..7380ab1de
--- /dev/null
+++ b/openstack/tests/functional/block_storage/v3/test_resource_filters.py
@@ -0,0 +1,24 @@
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
+# not use this file except in compliance with the License. You may obtain
+# a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+
+
+from openstack.tests.functional.block_storage.v3 import base
+
+
+class ResourceFilters(base.BaseBlockStorageTest):
+
+    def test_get(self):
+        resource_filters = list(self.conn.block_storage.resource_filters())
+
+        for rf in resource_filters:
+            self.assertIsInstance(rf.filters, list)
+            self.assertIsInstance(rf.resource, str)
diff --git a/openstack/tests/unit/block_storage/v3/test_proxy.py b/openstack/tests/unit/block_storage/v3/test_proxy.py
index b625e6337..b117e3f1a 100644
--- a/openstack/tests/unit/block_storage/v3/test_proxy.py
+++ b/openstack/tests/unit/block_storage/v3/test_proxy.py
@@ -15,6 +15,7 @@ from openstack.block_storage.v3 import _proxy
 from openstack.block_storage.v3 import backup
 from openstack.block_storage.v3 import capabilities
 from openstack.block_storage.v3 import limits
+from openstack.block_storage.v3 import resource_filter
 from openstack.block_storage.v3 import snapshot
 from openstack.block_storage.v3 import stats
 from openstack.block_storage.v3 import type
@@ -240,3 +241,7 @@ class TestVolumeProxy(test_proxy_base.TestProxyBase):
 
     def test_capabilites_get(self):
         self.verify_get(self.proxy.get_capabilities, capabilities.Capabilities)
+
+    def test_resource_filters(self):
+        self.verify_list(self.proxy.resource_filters,
+                         resource_filter.ResourceFilter)
diff --git a/openstack/tests/unit/block_storage/v3/test_resource_filter.py b/openstack/tests/unit/block_storage/v3/test_resource_filter.py
new file mode 100644
index 000000000..21fcc17e4
--- /dev/null
+++ b/openstack/tests/unit/block_storage/v3/test_resource_filter.py
@@ -0,0 +1,52 @@
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
+# not use this file except in compliance with the License. You may obtain
+# a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+# # Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+
+from openstack.block_storage.v3 import resource_filter
+from openstack.tests.unit import base
+
+RESOURCE_FILTER = {
+    'filters': [
+        'name',
+        'status',
+        'image_metadata',
+        'bootable',
+        'migration_status'
+    ],
+    'resource': 'volume'
+}
+
+
+class TestResourceFilter(base.TestCase):
+
+    def test_basic(self):
+        resource = resource_filter.ResourceFilter()
+        self.assertEqual('resource_filters',
+                         resource.resources_key)
+        self.assertEqual('/resource_filters',
+                         resource.base_path)
+        self.assertFalse(resource.allow_create)
+        self.assertFalse(resource.allow_fetch)
+        self.assertFalse(resource.allow_commit)
+        self.assertFalse(resource.allow_delete)
+        self.assertTrue(resource.allow_list)
+
+        self.assertDictEqual({"resource": "resource",
+                              "limit": "limit",
+                              "marker": "marker"},
+                             resource._query_mapping._mapping)
+
+    def test_make_resource_filter(self):
+        resource = resource_filter.ResourceFilter(
+            **RESOURCE_FILTER)
+        self.assertEqual(
+            RESOURCE_FILTER['filters'], resource.filters)
+        self.assertEqual(
+            RESOURCE_FILTER['resource'], resource.resource)