From bbb14264cba5c83461cd82ce09df7a15464fcbe9 Mon Sep 17 00:00:00 2001 From: Dustin Schoenbrun Date: Wed, 2 Aug 2017 14:07:57 -0400 Subject: [PATCH] Add exception for no default share type configured There is a particular scenario when creating a new Manila share, whether through creating a new share or managing an existing one, where if no share type was explicitly specified and no default share type was configured the error message would not substitute the share type ID into the exception message. This is because, in this case, there was no share type ID to substitute into the string. This patch creates a new exception for explicitly handling the case where there is no default share type and no share type is explicitly given. It gives an appropriate error message about there not being a default share type and that there was no share type explicitly given as well as what would need to be done to remedy the situation. Change-Id: I068b55d4b77dc24b53fe93d870bb84e1aef661a5 Closes-Bug: #1700346 --- manila/db/sqlalchemy/api.py | 4 ++++ manila/exception.py | 5 +++++ manila/tests/db/sqlalchemy/test_api.py | 4 ++++ manila/tests/test_exception.py | 5 +++++ ...exception-for-no-default-share-type-b1dd9bbe8c9cb3df.yaml | 5 +++++ 5 files changed, 23 insertions(+) create mode 100644 releasenotes/notes/bug-1700346-new-exception-for-no-default-share-type-b1dd9bbe8c9cb3df.yaml diff --git a/manila/db/sqlalchemy/api.py b/manila/db/sqlalchemy/api.py index 2974d193b2..06ffea5f61 100644 --- a/manila/db/sqlalchemy/api.py +++ b/manila/db/sqlalchemy/api.py @@ -3761,6 +3761,10 @@ def _share_type_get(context, id, session=None, inactive=False, first()) if not result: + # The only way that id could be None is if the default share type is + # not configured and no other share type was specified. + if id is None: + raise exception.DefaultShareTypeNotConfigured() raise exception.ShareTypeNotFound(share_type_id=id) share_type = _dict_with_specs(result) diff --git a/manila/exception.py b/manila/exception.py index 2c0bdef5c1..c78a46fda3 100644 --- a/manila/exception.py +++ b/manila/exception.py @@ -649,6 +649,11 @@ class ShareTypeDoesNotExist(NotFound): message = _("Share Type %(share_type)s does not exist.") +class DefaultShareTypeNotConfigured(NotFound): + message = _("No default share type is configured. Either configure a " + "default share type or explicitly specify a share type.") + + class ShareGroupTypeExists(ManilaException): message = _("Share group type %(type_id)s already exists.") diff --git a/manila/tests/db/sqlalchemy/test_api.py b/manila/tests/db/sqlalchemy/test_api.py index 027721a047..6549fe624a 100644 --- a/manila/tests/db/sqlalchemy/test_api.py +++ b/manila/tests/db/sqlalchemy/test_api.py @@ -2752,6 +2752,10 @@ class ShareTypeAPITestCase(test.TestCase): self.assertIsNone(result) + def test_share_type_get_with_none_id(self): + self.assertRaises(exception.DefaultShareTypeNotConfigured, + db_api.share_type_get, self.ctxt, None) + class MessagesDatabaseAPITestCase(test.TestCase): diff --git a/manila/tests/test_exception.py b/manila/tests/test_exception.py index d882d6f3b3..4dd40e5b26 100644 --- a/manila/tests/test_exception.py +++ b/manila/tests/test_exception.py @@ -486,6 +486,11 @@ class ManilaExceptionResponseCode404(test.TestCase): self.assertIn(share_type_id, e.msg) self.assertIn(extra_specs_key, e.msg) + def test_default_share_type_not_configured(self): + # Verify response code for exception.DefaultShareTypeNotConfigured + e = exception.DefaultShareTypeNotConfigured() + self.assertEqual(404, e.code) + def test_instance_not_found(self): # verify response code for exception.InstanceNotFound instance_id = "fake_instance_id" diff --git a/releasenotes/notes/bug-1700346-new-exception-for-no-default-share-type-b1dd9bbe8c9cb3df.yaml b/releasenotes/notes/bug-1700346-new-exception-for-no-default-share-type-b1dd9bbe8c9cb3df.yaml new file mode 100644 index 0000000000..812a17737a --- /dev/null +++ b/releasenotes/notes/bug-1700346-new-exception-for-no-default-share-type-b1dd9bbe8c9cb3df.yaml @@ -0,0 +1,5 @@ +--- +fixes: + - A new exception will be thrown when a default share type was not + configured and no other share type was specified on any sort of + share creation.