From 2bb08765abf2d5f8ee60ed25e42fea0bab62dbbd Mon Sep 17 00:00:00 2001
From: wuyuting <wytdahu@gmail.com>
Date: Fri, 13 Feb 2015 10:16:24 +0800
Subject: [PATCH] Admin extends tenant's volume but change admin's quota

When admin extends a tenant's volume, the tenant's
quota-usage doesn't change. However, admin's quota-usage
has changed. The right practice is: extend whose volume,
whose quota-usage should be changed.

Change-Id: Ia1bed7f212cbee715e29698ebbfe3c67486a55f5
Closes-Bug: #1421492
---
 cinder/tests/unit/test_volume.py | 22 +++++++++++++++-------
 cinder/volume/api.py             |  4 +++-
 cinder/volume/manager.py         |  5 +++--
 3 files changed, 21 insertions(+), 10 deletions(-)

diff --git a/cinder/tests/unit/test_volume.py b/cinder/tests/unit/test_volume.py
index 06639a8d9..ffe2663f1 100644
--- a/cinder/tests/unit/test_volume.py
+++ b/cinder/tests/unit/test_volume.py
@@ -3412,6 +3412,8 @@ class VolumeTestCase(BaseVolumeTestCase):
         volume_api.extend(self.context, volume, 3)
         volume = db.volume_get(context.get_admin_context(), volume['id'])
         self.assertEqual('extending', volume['status'])
+        reserve.assert_called_once_with(self.context, gigabytes=1,
+                                        project_id=volume['project_id'])
 
         # Test the quota exceeded
         volume['status'] = 'available'
@@ -3477,13 +3479,19 @@ class VolumeTestCase(BaseVolumeTestCase):
         # Test driver success
         with mock.patch.object(self.volume.driver,
                                'extend_volume') as extend_volume:
-            extend_volume.return_value = fake_extend
-            volume['status'] = 'extending'
-            self.volume.extend_volume(self.context, volume['id'], '4',
-                                      fake_reservations)
-            volume = db.volume_get(context.get_admin_context(), volume['id'])
-            self.assertEqual(4, volume['size'])
-            self.assertEqual('available', volume['status'])
+            with mock.patch.object(QUOTAS, 'commit') as quotas_commit:
+                extend_volume.return_value = fake_extend
+                volume['status'] = 'extending'
+                self.volume.extend_volume(self.context, volume['id'], '4',
+                                          fake_reservations)
+                volume = db.volume_get(context.get_admin_context(),
+                                       volume['id'])
+                self.assertEqual(4, volume['size'])
+                self.assertEqual('available', volume['status'])
+                quotas_commit.assert_called_with(
+                    self.context,
+                    ['RESERVATION'],
+                    project_id=volume['project_id'])
 
         # clean up
         self.volume.delete_volume(self.context, volume['id'])
diff --git a/cinder/volume/api.py b/cinder/volume/api.py
index 8a2af8f5f..d4bde31fc 100644
--- a/cinder/volume/api.py
+++ b/cinder/volume/api.py
@@ -1165,7 +1165,9 @@ class API(base.Base):
             reserve_opts = {'gigabytes': size_increase}
             QUOTAS.add_volume_type_opts(context, reserve_opts,
                                         volume.get('volume_type_id'))
-            reservations = QUOTAS.reserve(context, **reserve_opts)
+            reservations = QUOTAS.reserve(context,
+                                          project_id=volume['project_id'],
+                                          **reserve_opts)
         except exception.OverQuota as exc:
             usages = exc.kwargs['usages']
             quotas = exc.kwargs['quotas']
diff --git a/cinder/volume/manager.py b/cinder/volume/manager.py
index ef18fcc45..3881c6c20 100644
--- a/cinder/volume/manager.py
+++ b/cinder/volume/manager.py
@@ -1566,6 +1566,7 @@ class VolumeManager(manager.SchedulerDependentManager):
                                       {'status': 'error_extending'})
 
         volume = self.db.volume_get(context, volume_id)
+        project_id = volume['project_id']
         size_increase = (int(new_size)) - volume['size']
         self._notify_about_volume_usage(context, volume, "resize.start")
         try:
@@ -1580,10 +1581,10 @@ class VolumeManager(manager.SchedulerDependentManager):
                                                   "to extend volume") %
                                                 volume_id)
             finally:
-                QUOTAS.rollback(context, reservations)
+                QUOTAS.rollback(context, reservations, project_id=project_id)
                 return
 
-        QUOTAS.commit(context, reservations)
+        QUOTAS.commit(context, reservations, project_id=project_id)
         volume = self.db.volume_update(context,
                                        volume['id'],
                                        {'size': int(new_size),
-- 
2.45.2