1
# Copyright 2012 OpenStack LLC.
4
# Licensed under the Apache License, Version 2.0 (the "License"); you may
5
# not use this file except in compliance with the License. You may obtain
6
# a copy of the License at
8
# http://www.apache.org/licenses/LICENSE-2.0
10
# Unless required by applicable law or agreed to in writing, software
11
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13
# License for the specific language governing permissions and limitations
18
from nova.api.openstack import extensions
19
from nova.api.openstack import wsgi
20
from nova.api.openstack import xmlutil
22
from nova import exception
23
from nova import quota
26
authorize = extensions.extension_authorizer('compute', 'quota_classes')
29
class QuotaClassTemplate(xmlutil.TemplateBuilder):
31
root = xmlutil.TemplateElement('quota_class_set',
32
selector='quota_class_set')
35
for resource in quota.quota_resources:
36
elem = xmlutil.SubTemplateElement(root, resource)
39
return xmlutil.MasterTemplate(root, 1)
42
class QuotaClassSetsController(object):
44
def _format_quota_set(self, quota_class, quota_set):
45
"""Convert the quota object to a result dict"""
47
result = dict(id=str(quota_class))
49
for resource in quota.quota_resources:
50
result[resource] = quota_set[resource]
52
return dict(quota_class_set=result)
54
@wsgi.serializers(xml=QuotaClassTemplate)
55
def show(self, req, id):
56
context = req.environ['nova.context']
59
db.sqlalchemy.api.authorize_quota_class_context(context, id)
60
return self._format_quota_set(id,
61
quota.get_class_quotas(context, id))
62
except exception.NotAuthorized:
63
raise webob.exc.HTTPForbidden()
65
@wsgi.serializers(xml=QuotaClassTemplate)
66
def update(self, req, id, body):
67
context = req.environ['nova.context']
70
for key in body['quota_class_set'].keys():
71
if key in quota.quota_resources:
72
value = int(body['quota_class_set'][key])
74
db.quota_class_update(context, quota_class, key, value)
75
except exception.QuotaClassNotFound:
76
db.quota_class_create(context, quota_class, key, value)
77
except exception.AdminRequired:
78
raise webob.exc.HTTPForbidden()
79
return {'quota_class_set': quota.get_class_quotas(context,
83
class Quota_classes(extensions.ExtensionDescriptor):
84
"""Quota classes management support"""
87
alias = "os-quota-class-sets"
88
namespace = ("http://docs.openstack.org/compute/ext/"
89
"quota-classes-sets/api/v1.1")
90
updated = "2012-03-12T00:00:00+00:00"
92
def get_resources(self):
95
res = extensions.ResourceExtension('os-quota-class-sets',
96
QuotaClassSetsController())