From 28f293db407cf4bd0ad7fb71b20d03ce4022ec97 Mon Sep 17 00:00:00 2001 From: "jeremy.zhang" Date: Thu, 28 Sep 2017 14:15:25 +0800 Subject: [PATCH] Add backups v3 views This patch adds v3 views to backups and moves the metadata feature (min_version: 3.43) to backups v3 views, to avoid modifying the base viewbuilder class of backups. Also adds unit test for showing backup metadata. Change-Id: I884aebf76a67b215bb37ebe4b9a384d14abb315c --- cinder/api/v3/backups.py | 3 +++ cinder/api/v3/views/backups.py | 32 ++++++++++++++++++++++++ cinder/api/views/backups.py | 5 +--- cinder/tests/unit/api/v3/test_backups.py | 15 +++++++++++ 4 files changed, 51 insertions(+), 4 deletions(-) create mode 100644 cinder/api/v3/views/backups.py diff --git a/cinder/api/v3/backups.py b/cinder/api/v3/backups.py index a98a8a2e945..43c6c3c2c6f 100644 --- a/cinder/api/v3/backups.py +++ b/cinder/api/v3/backups.py @@ -21,6 +21,7 @@ from webob import exc from cinder.api.contrib import backups as backups_v2 from cinder.api import microversions as mv from cinder.api.openstack import wsgi +from cinder.api.v3.views import backups as backup_views from cinder.backup import api as backup_api from cinder import exception from cinder.i18n import _ @@ -32,6 +33,8 @@ LOG = logging.getLogger(__name__) class BackupsController(backups_v2.BackupsController): """The backups API controller for the OpenStack API V3.""" + _view_builder_class = backup_views.ViewBuilder + @wsgi.Controller.api_version(mv.BACKUP_UPDATE) def update(self, req, id, body): """Update a backup.""" diff --git a/cinder/api/v3/views/backups.py b/cinder/api/v3/views/backups.py new file mode 100644 index 00000000000..2a428ec64c7 --- /dev/null +++ b/cinder/api/v3/views/backups.py @@ -0,0 +1,32 @@ +# Copyright 2017 FiberHome Telecommunication Technologies CO.,LTD +# All Rights Reserved. +# +# 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 cinder.api import microversions as mv +from cinder.api.views import backups as views_v2 + + +class ViewBuilder(views_v2.ViewBuilder): + """Model a backups API V3 response as a python dictionary.""" + + def detail(self, request, backup): + """Detailed view of a single backup.""" + backup_ref = super(ViewBuilder, self).detail(request, backup) + + # Add metadata if min version is greater than or equal to + # BACKUP_METADATA. + req_version = request.api_version_request + if req_version.matches(mv.BACKUP_METADATA): + backup_ref['backup']['metadata'] = backup.metadata + return backup_ref diff --git a/cinder/api/views/backups.py b/cinder/api/views/backups.py index a4fec2abf55..df39696bb14 100644 --- a/cinder/api/views/backups.py +++ b/cinder/api/views/backups.py @@ -14,7 +14,6 @@ # under the License. from cinder.api import common -from cinder.api import microversions as mv class ViewBuilder(common.ViewBuilder): @@ -78,9 +77,7 @@ class ViewBuilder(common.ViewBuilder): 'data_timestamp': backup.data_timestamp, } } - req_version = request.api_version_request - if req_version.matches(mv.BACKUP_METADATA): - backup_dict['backup']['metadata'] = backup.metadata + return backup_dict def _list_view(self, func, request, backups, backup_count): diff --git a/cinder/tests/unit/api/v3/test_backups.py b/cinder/tests/unit/api/v3/test_backups.py index d055c6513d6..549a7140b50 100644 --- a/cinder/tests/unit/api/v3/test_backups.py +++ b/cinder/tests/unit/api/v3/test_backups.py @@ -146,3 +146,18 @@ class BackupsControllerAPITestCase(test.TestCase): self.assertEqual(new_name, backup.display_name) self.assertEqual(new_description, backup.display_description) + + @ddt.data(mv.get_prior_version(mv.BACKUP_METADATA), + mv.BACKUP_METADATA) + def test_backup_show_with_metadata(self, version): + backup = test_utils.create_backup( + self.ctxt, display_name='test_backup_metadata', + status=fields.BackupStatus.AVAILABLE) + # show backup metadata + url = '/v3/%s/backups/%s' % (fake.PROJECT_ID, backup.id) + req = fakes.HTTPRequest.blank(url, version=version) + backup_get = self.controller.show(req, backup.id)['backup'] + if version == mv.get_prior_version(mv.BACKUP_METADATA): + self.assertNotIn('metadata', backup_get) + else: + self.assertIn('metadata', backup_get)