From 267dbd292c5681daaa5659f8f2ce644a80a54a08 Mon Sep 17 00:00:00 2001
From: gengjh <gengjh@cn.ibm.com>
Date: Sat, 15 Jun 2013 10:07:49 +0800
Subject: [PATCH] Support force update quota

Once we have additional check when update quota in
https://review.openstack.org/#/c/25887/, we need provide --force option
when run 'nova quota-update'.

Since the change in nova server has been merged, we need re-enable the
changes in nova client side.

Fix bug 1160749

Change-Id: Iceb67c5816312fafed8a68e48a8a136c03d0bb5b
---
 novaclient/tests/v1_1/test_quotas.py |  9 +++++++++
 novaclient/tests/v1_1/test_shell.py  | 17 +++++++++++++++--
 novaclient/v1_1/quotas.py            |  5 +++--
 novaclient/v1_1/shell.py             | 15 ++++++++++++++-
 4 files changed, 41 insertions(+), 5 deletions(-)

diff --git a/novaclient/tests/v1_1/test_quotas.py b/novaclient/tests/v1_1/test_quotas.py
index a4d68f863..ce1c0388c 100644
--- a/novaclient/tests/v1_1/test_quotas.py
+++ b/novaclient/tests/v1_1/test_quotas.py
@@ -37,6 +37,15 @@ class QuotaSetsTest(utils.TestCase):
         cs.assert_called('PUT',
                    '/os-quota-sets/97f4c221bff44578b0300df4ef119353')
 
+    def test_force_update_quota(self):
+        q = cs.quotas.get('97f4c221bff44578b0300df4ef119353')
+        q.update(cores=2, force=True)
+        cs.assert_called(
+            'PUT', '/os-quota-sets/97f4c221bff44578b0300df4ef119353',
+            {'quota_set': {'force': True,
+                           'cores': 2,
+                           'tenant_id': '97f4c221bff44578b0300df4ef119353'}})
+
     def test_refresh_quota(self):
         q = cs.quotas.get('test')
         q2 = cs.quotas.get('test')
diff --git a/novaclient/tests/v1_1/test_shell.py b/novaclient/tests/v1_1/test_shell.py
index e9adfda1e..01fde3aa4 100644
--- a/novaclient/tests/v1_1/test_shell.py
+++ b/novaclient/tests/v1_1/test_shell.py
@@ -1123,8 +1123,21 @@ class ShellTest(utils.TestCase):
         self.run_command(
             'quota-update 97f4c221bff44578b0300df4ef119353'
             ' --instances=5')
-        self.assert_called('PUT',
-                           '/os-quota-sets/97f4c221bff44578b0300df4ef119353')
+        self.assert_called(
+            'PUT',
+            '/os-quota-sets/97f4c221bff44578b0300df4ef119353',
+            {'quota_set': {'instances': 5,
+                           'tenant_id': '97f4c221bff44578b0300df4ef119353'}})
+
+    def test_quota_force_update(self):
+        self.run_command(
+            'quota-update 97f4c221bff44578b0300df4ef119353'
+            ' --instances=5 --force')
+        self.assert_called(
+            'PUT', '/os-quota-sets/97f4c221bff44578b0300df4ef119353',
+            {'quota_set': {'force': True,
+                           'instances': 5,
+                           'tenant_id': '97f4c221bff44578b0300df4ef119353'}})
 
     def test_quota_update_fixed_ip(self):
         self.run_command(
diff --git a/novaclient/v1_1/quotas.py b/novaclient/v1_1/quotas.py
index 19f7d7190..e174c6f18 100644
--- a/novaclient/v1_1/quotas.py
+++ b/novaclient/v1_1/quotas.py
@@ -41,7 +41,7 @@ class QuotaSetManager(base.Manager):
                volumes=None, gigabytes=None,
                ram=None, floating_ips=None, fixed_ips=None, instances=None,
                injected_files=None, cores=None, key_pairs=None,
-               security_groups=None, security_group_rules=None):
+               security_groups=None, security_group_rules=None, force=None):
 
         body = {'quota_set': {
                 'tenant_id': tenant_id,
@@ -58,7 +58,8 @@ class QuotaSetManager(base.Manager):
                 'injected_files': injected_files,
                 'cores': cores,
                 'security_groups': security_groups,
-                'security_group_rules': security_group_rules}}
+                'security_group_rules': security_group_rules,
+                'force': force}}
 
         for key in body['quota_set'].keys():
             if body['quota_set'][key] is None:
diff --git a/novaclient/v1_1/shell.py b/novaclient/v1_1/shell.py
index 0bdabe4f1..fa854e3a4 100644
--- a/novaclient/v1_1/shell.py
+++ b/novaclient/v1_1/shell.py
@@ -31,6 +31,7 @@ from novaclient.openstack.common import timeutils
 from novaclient.openstack.common import uuidutils
 from novaclient import utils
 from novaclient.v1_1 import availability_zones
+from novaclient.v1_1 import quotas
 from novaclient.v1_1 import servers
 
 
@@ -2835,7 +2836,13 @@ def _quota_update(manager, identifier, args):
             updates[resource] = val
 
     if updates:
-        manager.update(identifier, **updates)
+        # default value of force is None to make sure this client
+        # will be compatibile with old nova server
+        force_update = getattr(args, 'force', None)
+        if isinstance(manager, quotas.QuotaSetManager):
+            manager.update(identifier, force=force_update, **updates)
+        else:
+            manager.update(identifier, **updates)
 
 
 @utils.arg('--tenant',
@@ -2944,6 +2951,12 @@ def do_quota_defaults(cs, args):
     type=int,
     default=None,
     help='New value for the "security-group-rules" quota.')
+@utils.arg('--force',
+    dest='force',
+    action="store_true",
+    default=None,
+    help='Whether force update the quota even if the already used'
+            ' and reserved exceeds the new quota')
 def do_quota_update(cs, args):
     """Update the quotas for a tenant."""