]> review.fuel-infra Code Review - openstack-build/cinder-build.git/commitdiff
Recalculate allocated value of parent project
authorcheneydc <dongc@neunn.com>
Fri, 11 Dec 2015 06:28:44 +0000 (14:28 +0800)
committercheneydc <dongc@neunn.com>
Mon, 14 Dec 2015 13:56:15 +0000 (21:56 +0800)
After update the child project quota, the new value should
not be accumulated to the 'allocated' value of parent
project.

So 'allocated' value should subtract the original child
project quota when it's updated.

Closes-Bug: #1525057

Change-Id: Icff845a034eb582d452b90270ca5dbce4bf1b5b6

cinder/api/contrib/quotas.py
cinder/tests/unit/api/contrib/test_quotas.py

index e513f410b78da84d48deb45e775ac05357a19aad..11193650af464f224adeb3432d661f61dce5c356 100644 (file)
@@ -323,8 +323,13 @@ class QuotaSetsController(wsgi.Controller):
                 value = self._validate_quota_limit(body['quota_set'], key,
                                                    quota_values,
                                                    parent_project_quotas)
+                original_quota = 0
+                if quota_values.get(key):
+                    original_quota = quota_values[key]['limit']
+
                 allocated_quotas[key] = (
-                    parent_project_quotas[key].get('allocated', 0) + value)
+                    parent_project_quotas[key].get('allocated', 0) + value -
+                    original_quota)
             else:
                 value = self._validate_quota_limit(body['quota_set'], key)
             valid_quotas[key] = value
index 398f096d961500703fec65853a94d106f5f14f10..2d4e0ba6705dbc7969ace7ae703fead2d06dd829 100644 (file)
@@ -264,6 +264,25 @@ class QuotaSetsControllerTest(test.TestCase):
                          volumes=4, backups=4, tenant_id=None)
         result = self.controller.update(self.req, self.D.id, body)
 
+    def test_update_subproject_repetitive(self):
+        self.controller._get_project = mock.Mock()
+        self.controller._get_project.side_effect = self._get_project
+        # Update the project A volumes quota.
+        self.req.environ['cinder.context'].project_id = self.A.id
+        body = make_body(gigabytes=2000, snapshots=15,
+                         volumes=10, backups=5, tenant_id=None)
+        result = self.controller.update(self.req, self.A.id, body)
+        self.assertDictMatch(body, result)
+        # Update the quota of B to be equal to its parent quota
+        # three times should be successful, the quota will not be
+        # allocated to 'allocated' value of parent project
+        for i in range(0, 3):
+            self.req.environ['cinder.context'].project_id = self.A.id
+            body = make_body(gigabytes=2000, snapshots=15,
+                             volumes=10, backups=5, tenant_id=None)
+            result = self.controller.update(self.req, self.B.id, body)
+            self.assertDictMatch(body, result)
+
     def test_update_subproject_not_in_hierarchy(self):
         self.controller._get_project = mock.Mock()
         self.controller._get_project.side_effect = self._get_project