From db6c58b009e561fd4191b0e47ad18ce53d7b4dcc Mon Sep 17 00:00:00 2001
From: Chris Yeoh <cyeoh@au1.ibm.com>
Date: Tue, 17 Dec 2013 21:46:58 +1030
Subject: [PATCH] Adds quota usage support for the V3 API

Adds the ability to do a detailed query from the
os-quota-sets V3 API extension. This was an interface
added in V3 to allow for the querying of current quota
usage. Adds a shell command quota-usage which
uses this interface to show current quota usage as well
as the limit.

The absolute limits response was removed from
the limits extension in a9e29b7e9fbe14ab42f24802a6d2a457a6317ba3
and so the corresponding novaclient command is also removed.
The "quota-show-usage" command now shows the same information.

Differences between the V2 and V3 API are described here:
https://wiki.openstack.org/wiki/NovaAPIv2tov3

Partially implements blueprint v3-api

Change-Id: I5db72d42120f4ad7a86fbfce20382988f6bbf5d3
---
 novaclient/tests/v3/fakes.py       |  6 +++
 novaclient/tests/v3/test_quotas.py | 12 ++++++
 novaclient/v3/quotas.py            | 15 ++++++++
 novaclient/v3/shell.py             | 59 +++++++++++++++++-------------
 4 files changed, 67 insertions(+), 25 deletions(-)

diff --git a/novaclient/tests/v3/fakes.py b/novaclient/tests/v3/fakes.py
index 72422f5cf..4291b13cb 100644
--- a/novaclient/tests/v3/fakes.py
+++ b/novaclient/tests/v3/fakes.py
@@ -293,6 +293,12 @@ class FakeHTTPClient(fakes_v1_1.FakeHTTPClient):
                       'security_groups': 1,
                       'security_group_rules': 1}})
 
+    def get_os_quota_sets_test_detail(self, **kw):
+        return (200, {}, {'quota_set': {
+            'cores': {'reserved': 0, 'in_use': 0, 'limit': 10},
+            'instances': {'reserved': 0, 'in_use': 4, 'limit': 50},
+            'ram': {'reserved': 0, 'in_use': 1024, 'limit': 51200}}})
+
     #
     # Hypervisors
     #
diff --git a/novaclient/tests/v3/test_quotas.py b/novaclient/tests/v3/test_quotas.py
index 0f361a49f..1e4eef19b 100644
--- a/novaclient/tests/v3/test_quotas.py
+++ b/novaclient/tests/v3/test_quotas.py
@@ -31,3 +31,15 @@ class QuotaSetsTest(test_quotas.QuotaSetsTest):
             'PUT', '/os-quota-sets/97f4c221bff44578b0300df4ef119353',
             {'quota_set': {'force': True,
                            'cores': 2}})
+
+    def test_tenant_quotas_get_detail(self):
+        tenant_id = 'test'
+        self.cs.quotas.get(tenant_id, detail=True)
+        self.cs.assert_called('GET', '/os-quota-sets/%s/detail' % tenant_id)
+
+    def test_user_quotas_get_detail(self):
+        tenant_id = 'test'
+        user_id = 'fake_user'
+        self.cs.quotas.get(tenant_id, user_id=user_id, detail=True)
+        url = '/os-quota-sets/%s/detail?user_id=%s' % (tenant_id, user_id)
+        self.cs.assert_called('GET', url)
diff --git a/novaclient/v3/quotas.py b/novaclient/v3/quotas.py
index 19723fafa..b55842daa 100644
--- a/novaclient/v3/quotas.py
+++ b/novaclient/v3/quotas.py
@@ -23,5 +23,20 @@ class QuotaSet(quotas.QuotaSet):
 class QuotaSetManager(quotas.QuotaSetManager):
     resource_class = QuotaSet
 
+    def get(self, tenant_id, user_id=None, detail=False):
+        if detail:
+            detail_string = '/detail'
+        else:
+            detail_string = ''
+
+        if hasattr(tenant_id, 'tenant_id'):
+            tenant_id = tenant_id.tenant_id
+        if user_id:
+            url = '/os-quota-sets/%s%s?user_id=%s' % (tenant_id, detail_string,
+                                                      user_id)
+        else:
+            url = '/os-quota-sets/%s%s' % (tenant_id, detail_string)
+        return self._get(url, "quota_set")
+
     def _update_body(self, tenant_id, **kwargs):
         return {'quota_set': kwargs}
diff --git a/novaclient/v3/shell.py b/novaclient/v3/shell.py
index 0750b45f7..b183860b4 100644
--- a/novaclient/v3/shell.py
+++ b/novaclient/v3/shell.py
@@ -27,6 +27,8 @@ import os
 import sys
 import time
 
+import six
+
 from novaclient import exceptions
 from novaclient.openstack.common import strutils
 from novaclient.openstack.common import timeutils
@@ -2198,31 +2200,6 @@ def do_keypair_show(cs, args):
     _print_keypair(keypair)
 
 
-@utils.arg('--tenant',
-           #nova db searches by project_id
-           dest='tenant',
-           metavar='<tenant>',
-           nargs='?',
-           help='Display information from single tenant (Admin only).')
-@utils.arg('--reserved',
-           dest='reserved',
-           action='store_true',
-           default=False,
-           help='Include reservations count.')
-def do_absolute_limits(cs, args):
-    """Print a list of absolute limits for a user"""
-    limits = cs.limits.get(args.reserved, args.tenant).absolute
-    columns = ['Name', 'Value']
-    utils.print_list(limits, columns)
-
-
-def do_rate_limits(cs, args):
-    """Print a list of rate limits for a user"""
-    limits = cs.limits.get().rate
-    columns = ['Verb', 'URI', 'Value', 'Remain', 'Unit', 'Next_Available']
-    utils.print_list(limits, columns)
-
-
 @utils.arg('--start', metavar='<start>',
            help='Usage range start date ex 2012-01-20 (default: 4 weeks ago)',
            default=None)
@@ -2865,6 +2842,23 @@ def _quota_show(quotas):
     utils.print_dict(quota_dict)
 
 
+def _quota_usage(quotas):
+    class QuotaObj(object):
+        def __init__(self, resource, quota_dict):
+            setattr(self, 'resource', resource)
+            for (k, v) in six.iteritems(quota_dict):
+                setattr(self, k, v)
+
+    quota_list = []
+    for resource in _quota_resources:
+        try:
+            quota_list.append(QuotaObj(resource, getattr(quotas, resource)))
+        except AttributeError:
+            pass
+    utils.print_list(quota_list, ['resource', 'in use', 'limit'],
+                     sortby_index=0)
+
+
 def _quota_update(manager, identifier, args):
     updates = {}
     for resource in _quota_resources:
@@ -2891,6 +2885,21 @@ def do_quota_show(cs, args):
         _quota_show(cs.quotas.get(args.tenant))
 
 
+@utils.arg('--tenant',
+    metavar='<tenant-id>',
+    default=None,
+    help='ID of tenant to list the quotas for.')
+@utils.arg('--user',
+    metavar='<user-id>',
+    default=None,
+    help='ID of user to list the quotas for.')
+def do_quota_usage(cs, args):
+    """List the quotas for a tenant."""
+
+    tenant = args.tenant or cs.client.tenant_id
+    _quota_usage(cs.quotas.get(tenant, user_id=args.user, detail=True))
+
+
 @utils.arg('--tenant',
     metavar='<tenant-id>',
     default=None,