diff --git a/api-ref/source/v3/index.rst b/api-ref/source/v3/index.rst index 617c2400218..ba210fb4c91 100644 --- a/api-ref/source/v3/index.rst +++ b/api-ref/source/v3/index.rst @@ -41,6 +41,7 @@ Block Storage API V3 (CURRENT) .. include:: hosts.inc .. include:: limits.inc .. include:: messages.inc +.. include:: resource-filters.inc .. include:: qos-specs-v3-qos-specs.inc .. quota-sets should arguably live closer to limits, but that would mess up our nice alphabetical ordering diff --git a/api-ref/source/v3/parameters.yaml b/api-ref/source/v3/parameters.yaml index 54c59b60ab7..9ca0e0e5633 100644 --- a/api-ref/source/v3/parameters.yaml +++ b/api-ref/source/v3/parameters.yaml @@ -113,6 +113,12 @@ qos_id: in: path required: false type: string +resource: + description: | + Filter filters by resource name. + in: path + required: false + type: string snapshot_id_1: description: | The UUID of the snapshot. @@ -1586,6 +1592,18 @@ reset_status: in: body required: true type: object +resource_1: + description: | + Resource which the filters will be applied to. + in: body + required: true + type: string +resource_filters: + description: | + The resource filter array. + in: body + required: true + type: array resource_type: description: | The resource type corresponding to ``resource_uuid``. diff --git a/api-ref/source/v3/resource-filters.inc b/api-ref/source/v3/resource-filters.inc new file mode 100644 index 00000000000..0bd8bc1df80 --- /dev/null +++ b/api-ref/source/v3/resource-filters.inc @@ -0,0 +1,41 @@ +.. -*- rst -*- + +Resource Filters +================ + +Lists all resource filters, available since +microversion 3.33. + + +List resource filters +~~~~~~~~~~~~~~~~~~~~~ + +.. rest_method:: GET /v3/{project_id}/resource_filters + +List filters. + +Normal response codes: 200 +Error response codes: + + +Request +------- + +.. rest_parameters:: parameters.yaml + + - project_id: project_id_path + - resource: resource + +Response Parameters +------------------- + +.. rest_parameters:: parameters.yaml + + - filters: resource_filters + - resource: resource_1 + +Response Example +---------------- + +.. literalinclude:: ./samples/resource-filters-list-response.json + :language: javascript diff --git a/api-ref/source/v3/samples/resource-filters-list-response.json b/api-ref/source/v3/samples/resource-filters-list-response.json new file mode 100644 index 00000000000..6a1da8fbcb0 --- /dev/null +++ b/api-ref/source/v3/samples/resource-filters-list-response.json @@ -0,0 +1,21 @@ +{ + "resource_filters": [ + { + "filters": [ + "name", + "status", + "image_metadata", "bootable", + "migration_status" + ], + "resource": "volume" + }, + { + "filters": [ + "name", + "status", + "volume_id" + ], + "resource": "snapshot" + } + ] +} \ No newline at end of file diff --git a/cinder/api/openstack/api_version_request.py b/cinder/api/openstack/api_version_request.py index 370816d4ae9..215ac01234d 100644 --- a/cinder/api/openstack/api_version_request.py +++ b/cinder/api/openstack/api_version_request.py @@ -83,6 +83,9 @@ REST_API_VERSION_HISTORY = """ * 3.30 - Support sort snapshots with "name". * 3.31 - Add support for configure resource query filters. * 3.32 - Add set-log and get-log service actions. + * 3.33 - Add ``resource_filters`` API to retrieve configured + resource filters. + """ # The minimum and maximum versions of the API supported @@ -90,7 +93,7 @@ REST_API_VERSION_HISTORY = """ # minimum version of the API supported. # Explicitly using /v1 or /v2 endpoints will still work _MIN_API_VERSION = "3.0" -_MAX_API_VERSION = "3.32" +_MAX_API_VERSION = "3.33" _LEGACY_API_VERSION1 = "1.0" _LEGACY_API_VERSION2 = "2.0" diff --git a/cinder/api/openstack/rest_api_version_history.rst b/cinder/api/openstack/rest_api_version_history.rst index 088526070ac..16ec89b3f25 100644 --- a/cinder/api/openstack/rest_api_version_history.rst +++ b/cinder/api/openstack/rest_api_version_history.rst @@ -304,3 +304,7 @@ user documentation. 3.32 ---- Added ``set-log`` and ``get-log`` service actions. + +3.33 +---- + Add ``resource_filters`` API to retrieve configured resource filters. diff --git a/cinder/api/v3/resource_filters.py b/cinder/api/v3/resource_filters.py new file mode 100644 index 00000000000..955ae208980 --- /dev/null +++ b/cinder/api/v3/resource_filters.py @@ -0,0 +1,43 @@ +# 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. + +"""The resource filters api.""" + +from cinder.api import common +from cinder.api.openstack import wsgi +from cinder.api.v3.views import resource_filters as filter_views + + +FILTER_API_VERSION = '3.33' + + +class ResourceFiltersController(wsgi.Controller): + """The resource filter API controller for the OpenStack API.""" + + _view_builder_class = filter_views.ViewBuilder + + def __init__(self, ext_mgr=None): + """Initialize controller class.""" + self.ext_mgr = ext_mgr + super(ResourceFiltersController, self).__init__() + + @wsgi.Controller.api_version(FILTER_API_VERSION) + def index(self, req): + """Return a list of resource filters.""" + resource = req.params.get('resource', None) + filters = common.get_enabled_resource_filters(resource=resource) + return filter_views.ViewBuilder.list(filters) + + +def create_resource(ext_mgr): + """Create the wsgi resource for this controller.""" + return wsgi.Resource(ResourceFiltersController(ext_mgr)) diff --git a/cinder/api/v3/router.py b/cinder/api/v3/router.py index f83fb778458..53cadbebfa6 100644 --- a/cinder/api/v3/router.py +++ b/cinder/api/v3/router.py @@ -33,6 +33,7 @@ from cinder.api.v3 import group_specs from cinder.api.v3 import group_types from cinder.api.v3 import groups from cinder.api.v3 import messages +from cinder.api.v3 import resource_filters from cinder.api.v3 import snapshot_manage from cinder.api.v3 import snapshots from cinder.api.v3 import volume_manage @@ -186,3 +187,8 @@ class APIRouter(cinder.api.openstack.APIRouter): mapper.resource('worker', 'workers', controller=self.resources['workers'], collection={'cleanup': 'POST'}) + + self.resources['resource_filters'] = resource_filters.create_resource( + ext_mgr) + mapper.resource('resource_filter', 'resource_filters', + controller=self.resources['resource_filters']) diff --git a/cinder/api/v3/views/resource_filters.py b/cinder/api/v3/views/resource_filters.py new file mode 100644 index 00000000000..9376c841988 --- /dev/null +++ b/cinder/api/v3/views/resource_filters.py @@ -0,0 +1,35 @@ +# 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. + + +class ViewBuilder(object): + """Model an resource filters API response as a python dictionary.""" + + _collection_name = "resource_filters" + + @classmethod + def list(cls, filters): + """Build a view of a list of resource filters. + + .. code-block:: json + + { + "resource_filters": [{ + "resource": 'resource_1', + "filters": ['filter1', 'filter2', 'filter3'] + }] + } + """ + + return {'resource_filters': [{ + 'resource': fil[0], + 'filters': fil[1]} for fil in filters.items()]} diff --git a/cinder/tests/unit/api/v3/test_resource_filters.py b/cinder/tests/unit/api/v3/test_resource_filters.py new file mode 100644 index 00000000000..b5d55e48bf8 --- /dev/null +++ b/cinder/tests/unit/api/v3/test_resource_filters.py @@ -0,0 +1,61 @@ +# 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. + +""" +Tests for resource filters API. +""" + +import ddt + +from cinder.api import common +from cinder.api.v3 import resource_filters as v3_filters +from cinder import test +from cinder.tests.unit.api import fakes +from cinder.tests.unit import fake_constants as fake + +FILTERS_MICRO_VERSION = '3.33' + + +@ddt.ddt +class ResourceFiltersAPITestCase(test.TestCase): + """Test Case for filter API.""" + + def setUp(self): + super(ResourceFiltersAPITestCase, self).setUp() + self.controller = v3_filters.ResourceFiltersController() + + @ddt.data({'filters': {'volume': ['key1']}, + 'resource': 'volume', + 'expected_filters': [{'resource': 'volume', + 'filters': ['key1']}]}, + {'filters': {'volume': ['key1'], 'snapshot': ['key2']}, + 'resource': None, + 'expected_filters': [{'resource': 'volume', + 'filters': ['key1']}, + {'resource': 'snapshot', + 'filters': ['key2']}]}, + {'filters': {'volume': ['key1', 'key2']}, + 'resource': 'snapshot', + 'expected_filters': []}) + @ddt.unpack + def test_get_allowed_filters(self, filters, resource, expected_filters): + common._FILTERS_COLLECTION = filters + request_url = '/v3/%s/resource_filters' % fake.PROJECT_ID + if resource is not None: + request_url += '?resource=%s' % resource + req = fakes.HTTPRequest.blank(request_url, + version=FILTERS_MICRO_VERSION) + + result = self.controller.index(req) + + expected = {'resource_filters': expected_filters} + self.assertEqual(expected, result) diff --git a/releasenotes/notes/add-resource-filters-api-8g3dub1700qaye98.yaml b/releasenotes/notes/add-resource-filters-api-8g3dub1700qaye98.yaml new file mode 100644 index 00000000000..f51cddab35a --- /dev/null +++ b/releasenotes/notes/add-resource-filters-api-8g3dub1700qaye98.yaml @@ -0,0 +1,3 @@ +--- +features: + - Added ``resource_filters`` API to retrieve configured resource filters.