~gandelman-a/ubuntu/precise/nova/UCA_2012.2.1

« back to all changes in this revision

Viewing changes to nova/api/openstack/compute/contrib/quota_classes.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2012-05-24 13:12:53 UTC
  • mfrom: (1.1.55)
  • Revision ID: package-import@ubuntu.com-20120524131253-ommql08fg1en06ut
Tags: 2012.2~f1-0ubuntu1
* New upstream release.
* Prepare for quantal:
  - Dropped debian/patches/upstream/0006-Use-project_id-in-ec2.cloud._format_image.patch
  - Dropped debian/patches/upstream/0005-Populate-image-properties-with-project_id-again.patch
  - Dropped debian/patches/upstream/0004-Fixed-bug-962840-added-a-test-case.patch
  - Dropped debian/patches/upstream/0003-Allow-unprivileged-RADOS-users-to-access-rbd-volumes.patch
  - Dropped debian/patches/upstream/0002-Stop-libvirt-test-from-deleting-instances-dir.patch
  - Dropped debian/patches/upstream/0001-fix-bug-where-nova-ignores-glance-host-in-imageref.patch 
  - Dropped debian/patches/0001-fix-useexisting-deprecation-warnings.patch
* debian/control: Add python-keystone as a dependency. (LP: #907197)
* debian/patches/kombu_tests_timeout.patch: Refreshed.
* debian/nova.conf, debian/nova-common.postinst: Convert to new ini
  file configuration
* debian/patches/nova-manage_flagfile_location.patch: Refreshed

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2012 OpenStack LLC.
 
2
# All Rights Reserved.
 
3
#
 
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
 
7
#
 
8
#         http://www.apache.org/licenses/LICENSE-2.0
 
9
#
 
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
 
14
#    under the License.
 
15
 
 
16
import webob
 
17
 
 
18
from nova.api.openstack import extensions
 
19
from nova.api.openstack import wsgi
 
20
from nova.api.openstack import xmlutil
 
21
from nova import db
 
22
from nova import exception
 
23
from nova import quota
 
24
 
 
25
 
 
26
authorize = extensions.extension_authorizer('compute', 'quota_classes')
 
27
 
 
28
 
 
29
class QuotaClassTemplate(xmlutil.TemplateBuilder):
 
30
    def construct(self):
 
31
        root = xmlutil.TemplateElement('quota_class_set',
 
32
                                       selector='quota_class_set')
 
33
        root.set('id')
 
34
 
 
35
        for resource in quota.quota_resources:
 
36
            elem = xmlutil.SubTemplateElement(root, resource)
 
37
            elem.text = resource
 
38
 
 
39
        return xmlutil.MasterTemplate(root, 1)
 
40
 
 
41
 
 
42
class QuotaClassSetsController(object):
 
43
 
 
44
    def _format_quota_set(self, quota_class, quota_set):
 
45
        """Convert the quota object to a result dict"""
 
46
 
 
47
        result = dict(id=str(quota_class))
 
48
 
 
49
        for resource in quota.quota_resources:
 
50
            result[resource] = quota_set[resource]
 
51
 
 
52
        return dict(quota_class_set=result)
 
53
 
 
54
    @wsgi.serializers(xml=QuotaClassTemplate)
 
55
    def show(self, req, id):
 
56
        context = req.environ['nova.context']
 
57
        authorize(context)
 
58
        try:
 
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()
 
64
 
 
65
    @wsgi.serializers(xml=QuotaClassTemplate)
 
66
    def update(self, req, id, body):
 
67
        context = req.environ['nova.context']
 
68
        authorize(context)
 
69
        quota_class = id
 
70
        for key in body['quota_class_set'].keys():
 
71
            if key in quota.quota_resources:
 
72
                value = int(body['quota_class_set'][key])
 
73
                try:
 
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,
 
80
                                                          quota_class)}
 
81
 
 
82
 
 
83
class Quota_classes(extensions.ExtensionDescriptor):
 
84
    """Quota classes management support"""
 
85
 
 
86
    name = "QuotaClasses"
 
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"
 
91
 
 
92
    def get_resources(self):
 
93
        resources = []
 
94
 
 
95
        res = extensions.ResourceExtension('os-quota-class-sets',
 
96
                                           QuotaClassSetsController())
 
97
        resources.append(res)
 
98
 
 
99
        return resources