From daf7b3aacf1481c967db4706e7cfa7be09976c52 Mon Sep 17 00:00:00 2001
From: paulali <paulali@protonmail.com>
Date: Wed, 24 Mar 2021 20:39:05 +0000
Subject: [PATCH] Adds a clear error message to operator-set limits

When an operator sets a limit that is greater than 2147483647, an
explicit error message "Quota limit should not exceed 2147483647"
is communicated. This is better than the initial error message
which was "ERROR: The server has either erred or is
incapable of performing the requested operation. (HTTP 500)."

Closes-Bug: #1908352
Change-Id: I0d301c5f527c3e16e14021b679c85bd51833a1d6
---
 manila/db/sqlalchemy/api.py                   |  8 ++++++-
 ...plicit-error-message-c33c7b75a7e49257.yaml |  7 ++++++
 manila/tests/api/v2/test_quota_sets.py        | 24 +++++++++++++++++++
 3 files changed, 38 insertions(+), 1 deletion(-)
 create mode 100644 manila/db/sqlalchemy/releasenotes/notes/bug-1908352-add-explicit-error-message-c33c7b75a7e49257.yaml

diff --git a/manila/db/sqlalchemy/api.py b/manila/db/sqlalchemy/api.py
index ec4470e955..d18240293b 100644
--- a/manila/db/sqlalchemy/api.py
+++ b/manila/db/sqlalchemy/api.py
@@ -678,7 +678,13 @@ def quota_create(context, project_id, resource, limit, user_id=None,
     quota_ref.hard_limit = limit
     session = get_session()
     with session.begin():
-        quota_ref.save(session)
+        try:
+            quota_ref.save(session)
+        except Exception as e:
+            if "out of range" in str(e).lower():
+                msg = _("Quota limit should not exceed 2147483647")
+                raise exception.InvalidInput(reason=msg)
+            raise
     return quota_ref
 
 
diff --git a/manila/db/sqlalchemy/releasenotes/notes/bug-1908352-add-explicit-error-message-c33c7b75a7e49257.yaml b/manila/db/sqlalchemy/releasenotes/notes/bug-1908352-add-explicit-error-message-c33c7b75a7e49257.yaml
new file mode 100644
index 0000000000..76201a4fd0
--- /dev/null
+++ b/manila/db/sqlalchemy/releasenotes/notes/bug-1908352-add-explicit-error-message-c33c7b75a7e49257.yaml
@@ -0,0 +1,7 @@
+---
+fixes:
+  - |
+    When a quota value greater than 2147483647 is set, the error
+    message "ERROR: Invalid input received:Quota limit should not
+    exceed 2147483647. (HTTP 400)" is communicated to the user.
+
diff --git a/manila/tests/api/v2/test_quota_sets.py b/manila/tests/api/v2/test_quota_sets.py
index c9005fddd5..aa3f92ff0b 100644
--- a/manila/tests/api/v2/test_quota_sets.py
+++ b/manila/tests/api/v2/test_quota_sets.py
@@ -447,6 +447,30 @@ class QuotaSetsControllerTest(test.TestCase):
             mock_policy_update_check_call, mock_policy_show_check_call])
         quota_sets.db.share_type_get_by_name_or_id.assert_not_called()
 
+    @ddt.data(_get_request(True, True), _get_request(True, False))
+    def test_update_quota_with_value_greater_than_2147483647(self, req):
+        value = 2147483648
+        body = {'quota_set': {'tenant_id': self.project_id, 'shares': value}}
+
+        if req == _get_request(True, True):
+            self.mock_policy_update_check_call = mock.call(
+                req.environ['manila.context'], self.resource_name, 'update')
+            self.assertRaises(
+                webob.exc.HTTPBadRequest,
+                self.controller.update,
+                req, self.project_id, body
+            )
+            self.mock_policy_check.assert_called_once_with(
+                req.environ['manila.context'], self.resource_name, 'update')
+
+        if req == _get_request(True, False):
+            self.assertRaises(
+                webob.exc.HTTPBadRequest,
+                self.controller.update,
+                req, self.project_id, body
+            )
+            self.mock_policy_check.assert_not_called()
+
     @ddt.data('2.39', '2.40')
     def test_update_share_type_quota(self, microversion):
         self.mock_object(