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
This commit is contained in:
Chris Yeoh 2013-12-17 21:46:58 +10:30 committed by Christopher Yeoh
parent 77a9574238
commit db6c58b009
4 changed files with 67 additions and 25 deletions

View File

@ -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
#

View File

@ -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)

View File

@ -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}

View File

@ -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,