From a874b24278a24c180c9cd843991cb2c1c5223faa Mon Sep 17 00:00:00 2001 From: cheneydc Date: Fri, 11 Dec 2015 14:28:44 +0800 Subject: [PATCH] Recalculate allocated value of parent project 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 | 7 ++++++- cinder/tests/unit/api/contrib/test_quotas.py | 19 +++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/cinder/api/contrib/quotas.py b/cinder/api/contrib/quotas.py index e513f410b..11193650a 100644 --- a/cinder/api/contrib/quotas.py +++ b/cinder/api/contrib/quotas.py @@ -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 diff --git a/cinder/tests/unit/api/contrib/test_quotas.py b/cinder/tests/unit/api/contrib/test_quotas.py index 398f096d9..2d4e0ba67 100644 --- a/cinder/tests/unit/api/contrib/test_quotas.py +++ b/cinder/tests/unit/api/contrib/test_quotas.py @@ -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 -- 2.45.2