From 6af780c1e4086e8fd323539fbd515a68c3620ced Mon Sep 17 00:00:00 2001 From: junboli Date: Fri, 22 Dec 2017 14:14:52 +0800 Subject: [PATCH] Fix default and detailed share type result not correct When request default share type and show share type detailed, the share type results is not correct, because the values of attribute required_extra_specs is always empty. Change-Id: I68fc4e2ba30bbd87b5417fe48688edd96cbcee5d Closes-Bug: #1733742 --- manila/api/v2/share_types.py | 19 ++++++++++++++++++- manila/share/share_types.py | 13 ++++++++++--- manila/tests/api/v1/test_shares.py | 7 +++++++ manila/tests/api/v2/test_share_types.py | 3 +++ manila/tests/api/v2/test_shares.py | 6 ++++++ 5 files changed, 44 insertions(+), 4 deletions(-) diff --git a/manila/api/v2/share_types.py b/manila/api/v2/share_types.py index 2767bf040b..9431ca9e54 100644 --- a/manila/api/v2/share_types.py +++ b/manila/api/v2/share_types.py @@ -15,6 +15,7 @@ """The share type API controller module..""" +from oslo_log import log from oslo_utils import strutils from oslo_utils import uuidutils import six @@ -31,6 +32,9 @@ from manila import rpc from manila.share import share_types +LOG = log.getLogger(__name__) + + class ShareTypesController(wsgi.Controller): """The share types API controller for the OpenStack API.""" @@ -68,7 +72,7 @@ class ShareTypesController(wsgi.Controller): """Return a single share type item.""" context = req.environ['manila.context'] try: - share_type = share_types.get_share_type(context, id) + share_type = self._show_share_type_details(context, id) except exception.NotFound: msg = _("Share type not found.") raise exc.HTTPNotFound(explanation=msg) @@ -77,6 +81,19 @@ class ShareTypesController(wsgi.Controller): req.cache_db_share_type(share_type) return self._view_builder.show(req, share_type) + def _show_share_type_details(self, context, id): + share_type = share_types.get_share_type(context, id) + required_extra_specs = {} + try: + required_extra_specs = share_types.get_valid_required_extra_specs( + share_type['extra_specs']) + except exception.InvalidExtraSpec: + LOG.exception('Share type %(share_type_id)s has invalid required' + ' extra specs.', {'share_type_id': id}) + + share_type['required_extra_specs'] = required_extra_specs + return share_type + @wsgi.Controller.authorize def default(self, req): """Return default volume type.""" diff --git a/manila/share/share_types.py b/manila/share/share_types.py index 0aa818cd71..d2239a54fe 100644 --- a/manila/share/share_types.py +++ b/manila/share/share_types.py @@ -166,15 +166,22 @@ def get_default_share_type(ctxt=None): if ctxt is None: ctxt = context.get_admin_context() + share_type = {} try: - return get_share_type_by_name(ctxt, name) + share_type = get_share_type_by_name(ctxt, name) + required_extra_specs = get_valid_required_extra_specs( + share_type['extra_specs']) + share_type['required_extra_specs'] = required_extra_specs + return share_type except exception.ShareTypeNotFoundByName as e: # Couldn't find share type with the name in default_share_type # flag, record this issue and move on # TODO(zhiteng) consider add notification to warn admin LOG.exception('Default share type is not found, ' - 'please check default_share_type config: %s', - e) + 'please check default_share_type config: %s', e) + except exception.InvalidExtraSpec as ex: + LOG.exception('Default share type has invalid required extra' + ' specs: %s', ex) def get_share_type_extra_specs(share_type_id, key=False): diff --git a/manila/tests/api/v1/test_shares.py b/manila/tests/api/v1/test_shares.py index 5ef01d2723..ffaa2f30cd 100644 --- a/manila/tests/api/v1/test_shares.py +++ b/manila/tests/api/v1/test_shares.py @@ -78,7 +78,14 @@ class ShareAPITest(test.TestCase): self.vt = { 'id': 'fake_volume_type_id', 'name': 'fake_volume_type_name', + 'required_extra_specs': { + 'driver_handles_share_servers': 'False' + }, + 'extra_specs': { + 'driver_handles_share_servers': 'False' + } } + CONF.set_default("default_share_type", None) def _get_expected_share_detailed_response(self, values=None, admin=False): diff --git a/manila/tests/api/v2/test_share_types.py b/manila/tests/api/v2/test_share_types.py index a9ab762662..ac6fd64c4b 100644 --- a/manila/tests/api/v2/test_share_types.py +++ b/manila/tests/api/v2/test_share_types.py @@ -188,6 +188,9 @@ class ShareTypesAPITest(test.TestCase): self.assertEqual(2, len(res_dict)) self.assertEqual('1', res_dict['share_type']['id']) self.assertEqual('share_type_1', res_dict['share_type']['name']) + expect = {constants.ExtraSpecs.DRIVER_HANDLES_SHARE_SERVERS: "true"} + self.assertEqual(expect, + res_dict['share_type']['required_extra_specs']) policy.check_policy.assert_called_once_with( req.environ['manila.context'], self.resource_name, 'show') diff --git a/manila/tests/api/v2/test_shares.py b/manila/tests/api/v2/test_shares.py index e7437e55cf..0d837cdb20 100644 --- a/manila/tests/api/v2/test_shares.py +++ b/manila/tests/api/v2/test_shares.py @@ -87,6 +87,12 @@ class ShareAPITest(test.TestCase): self.vt = { 'id': 'fake_volume_type_id', 'name': 'fake_volume_type_name', + 'required_extra_specs': { + 'driver_handles_share_servers': 'False' + }, + 'extra_specs': { + 'driver_handles_share_servers': 'False' + } } self.snapshot = { 'id': '2',