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.