Implement quota classes.

Nova's quota class support allows entire classes of quotas to be
associated with projects, which makes it easier to set specific
quotas across multiple projects.  This change adds client-side
support for manipulating quota classes.

Change-Id: I7ee14d16aa51957dcdc1ea5c7a9d5b6bd1656f33
This commit is contained in:
Kevin L. Mitchell 2012-03-14 16:14:56 -05:00
parent bb2876b4d5
commit c444900d31
6 changed files with 291 additions and 0 deletions

View File

@ -9,6 +9,7 @@ from novaclient.v1_1 import hosts
from novaclient.v1_1 import images
from novaclient.v1_1 import keypairs
from novaclient.v1_1 import limits
from novaclient.v1_1 import quota_classes
from novaclient.v1_1 import quotas
from novaclient.v1_1 import security_group_rules
from novaclient.v1_1 import security_groups
@ -61,6 +62,7 @@ class Client(object):
self.volume_snapshots = volume_snapshots.SnapshotManager(self)
self.volume_types = volume_types.VolumeTypeManager(self)
self.keypairs = keypairs.KeypairManager(self)
self.quota_classes = quota_classes.QuotaClassSetManager(self)
self.quotas = quotas.QuotaSetManager(self)
self.security_groups = security_groups.SecurityGroupManager(self)
self.security_group_rules = \

View File

@ -0,0 +1,59 @@
# Copyright 2012 OpenStack LLC.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from novaclient import base
class QuotaClassSet(base.Resource):
@property
def id(self):
"""QuotaClassSet does not have a 'id' attribute but base.Resource
needs it to self-refresh and QuotaSet is indexed by class_name"""
return self.class_name
def update(self, *args, **kwargs):
self.manager.update(self.class_name, *args, **kwargs)
class QuotaClassSetManager(base.ManagerWithFind):
resource_class = QuotaClassSet
def get(self, class_name):
return self._get("/os-quota-class-sets/%s" % (class_name),
"quota_class_set")
def update(self, class_name, metadata_items=None,
injected_file_content_bytes=None, volumes=None, gigabytes=None,
ram=None, floating_ips=None, instances=None,
injected_files=None, cores=None):
body = {'quota_class_set': {
'class_name': class_name,
'metadata_items': metadata_items,
'injected_file_content_bytes': injected_file_content_bytes,
'volumes': volumes,
'gigabytes': gigabytes,
'ram': ram,
'floating_ips': floating_ips,
'instances': instances,
'injected_files': injected_files,
'cores': cores}}
for key in body['quota_class_set'].keys():
if body['quota_class_set'][key] is None:
body['quota_class_set'].pop(key)
self._update('/os-quota-class-sets/%s' % (class_name), body)

View File

@ -1652,3 +1652,138 @@ def do_ssh(cs, args):
print "ERROR: No %s %s address found." % (address_type,
pretty_version)
return
_quota_resources = ['instances', 'cores', 'ram', 'volumes', 'gigabytes',
'floating_ips', 'metadata_items', 'injected_files',
'injected_file_content_bytes']
def _quota_show(quotas):
quota_dict = {}
for resource in _quota_resources:
quota_dict[resource] = getattr(quotas, resource, None)
utils.print_dict(quota_dict)
def _quota_update(manager, identifier, args):
updates = {}
for resource in _quota_resources:
val = getattr(args, resource, None)
if val is not None:
updates[resource] = val
if updates:
manager.update(identifier, **updates)
@utils.arg('tenant', metavar='<tenant_id>',
help='UUID of tenant to list the quotas for.')
def do_quota_show(cs, args):
"""List the quotas for a tenant."""
_quota_show(cs.quotas.get(args.tenant))
@utils.arg('tenant', metavar='<tenant_id>',
help='UUID of tenant to list the default quotas for.')
def do_quota_defaults(cs, args):
"""List the default quotas for a tenant."""
_quota_show(cs.quotas.defaults(args.tenant))
@utils.arg('tenant', metavar='<tenant_id>',
help='UUID of tenant to set the quotas for.')
@utils.arg('--instances',
metavar='<instances>',
type=int, default=None,
help='New value for the "instances" quota.')
@utils.arg('--cores',
metavar='<cores>',
type=int, default=None,
help='New value for the "cores" quota.')
@utils.arg('--ram',
metavar='<ram>',
type=int, default=None,
help='New value for the "ram" quota.')
@utils.arg('--volumes',
metavar='<volumes>',
type=int, default=None,
help='New value for the "volumes" quota.')
@utils.arg('--gigabytes',
metavar='<gigabytes>',
type=int, default=None,
help='New value for the "gigabytes" quota.')
@utils.arg('--floating-ips', '--floating_ips',
metavar='<floating_ips>',
type=int, default=None,
help='New value for the "floating_ips" quota.')
@utils.arg('--metadata-items', '--metadata_items',
metavar='<metadata_items>',
type=int, default=None,
help='New value for the "metadata_items" quota.')
@utils.arg('--injected-files', '--injected_files',
metavar='<injected_files>',
type=int, default=None,
help='New value for the "injected_files" quota.')
@utils.arg('--injected-file-content-bytes', '--injected_file_content_bytes',
metavar='<injected_file_content_bytes>',
type=int, default=None,
help='New value for the "injected_file_content_bytes" quota.')
def do_quota_update(cs, args):
"""Update the quotas for a tenant."""
_quota_update(cs.quotas, args.tenant, args)
@utils.arg('class_name', metavar='<class>',
help='Name of quota class to list the quotas for.')
def do_quota_class_show(cs, args):
"""List the quotas for a quota class."""
_quota_show(cs.quota_classes.get(args.class_name))
@utils.arg('class_name', metavar='<class>',
help='Name of quota class to set the quotas for.')
@utils.arg('--instances',
metavar='<instances>',
type=int, default=None,
help='New value for the "instances" quota.')
@utils.arg('--cores',
metavar='<cores>',
type=int, default=None,
help='New value for the "cores" quota.')
@utils.arg('--ram',
metavar='<ram>',
type=int, default=None,
help='New value for the "ram" quota.')
@utils.arg('--volumes',
metavar='<volumes>',
type=int, default=None,
help='New value for the "volumes" quota.')
@utils.arg('--gigabytes',
metavar='<gigabytes>',
type=int, default=None,
help='New value for the "gigabytes" quota.')
@utils.arg('--floating-ips', '--floating_ips',
metavar='<floating_ips>',
type=int, default=None,
help='New value for the "floating_ips" quota.')
@utils.arg('--metadata-items', '--metadata_items',
metavar='<metadata_items>',
type=int, default=None,
help='New value for the "metadata_items" quota.')
@utils.arg('--injected-files', '--injected_files',
metavar='<injected_files>',
type=int, default=None,
help='New value for the "injected_files" quota.')
@utils.arg('--injected-file-content-bytes', '--injected_file_content_bytes',
metavar='<injected_file_content_bytes>',
type=int, default=None,
help='New value for the "injected_file_content_bytes" quota.')
def do_quota_class_update(cs, args):
"""Update the quotas for a quota class."""
_quota_update(cs.quota_classes, args.class_name, args)

View File

@ -590,6 +590,39 @@ class FakeHTTPClient(base_client.HTTPClient):
'injected_files': 1,
'cores': 1}})
#
# Quota Classes
#
def get_os_quota_class_sets_test(self, **kw):
return (200, {'quota_class_set': {
'class_name': 'test',
'metadata_items': [],
'injected_file_content_bytes': 1,
'volumes': 1,
'gigabytes': 1,
'ram': 1,
'floating_ips': 1,
'instances': 1,
'injected_files': 1,
'cores': 1}})
def put_os_quota_class_sets_test(self, body, **kw):
assert body.keys() == ['quota_class_set']
fakes.assert_has_keys(body['quota_class_set'],
required=['class_name'])
return (200, {'quota_class_set': {
'class_name': 'test',
'metadata_items': [],
'injected_file_content_bytes': 1,
'volumes': 2,
'gigabytes': 1,
'ram': 1,
'floating_ips': 1,
'instances': 1,
'injected_files': 1,
'cores': 1}})
#
# Security Groups
#

View File

@ -0,0 +1,42 @@
# Copyright 2011 OpenStack LLC.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from tests import utils
from tests.v1_1 import fakes
cs = fakes.FakeClient()
class QuotaClassSetsTest(utils.TestCase):
def test_class_quotas_get(self):
class_name = 'test'
cs.quota_classes.get(class_name)
cs.assert_called('GET', '/os-quota-class-sets/%s' % class_name)
def test_update_quota(self):
q = cs.quota_classes.get('test')
q.update(volumes=2)
cs.assert_called('PUT', '/os-quota-class-sets/test')
def test_refresh_quota(self):
q = cs.quota_classes.get('test')
q2 = cs.quota_classes.get('test')
self.assertEqual(q.volumes, q2.volumes)
q2.volumes = 0
self.assertNotEqual(q.volumes, q2.volumes)
q2.get()
self.assertEqual(q.volumes, q2.volumes)

View File

@ -414,3 +414,23 @@ class ShellTest(utils.TestCase):
def test_host_reboot(self):
self.run_command('host-action sample-host --action reboot')
self.assert_called('GET', '/os-hosts/sample-host/reboot')
def test_quota_show(self):
self.run_command('quota-show test')
self.assert_called('GET', '/os-quota-sets/test')
def test_quota_defaults(self):
self.run_command('quota-defaults test')
self.assert_called('GET', '/os-quota-sets/test/defaults')
def test_quota_update(self):
self.run_command('quota-update test --instances=5')
self.assert_called('PUT', '/os-quota-sets/test')
def test_quota_class_show(self):
self.run_command('quota-class-show test')
self.assert_called('GET', '/os-quota-class-sets/test')
def test_quota_class_update(self):
self.run_command('quota-class-update test --instances=5')
self.assert_called('PUT', '/os-quota-class-sets/test')