~ttx/nova/d4-merge

« back to all changes in this revision

Viewing changes to nova/api/openstack/contrib/quotas.py

  • Committer: Thierry Carrez
  • Date: 2011-08-23 12:23:07 UTC
  • mfrom: (1130.75.258 nova)
  • Revision ID: thierry@openstack.org-20110823122307-f0vtuyg1ikc14n87
Merge diablo-4 development from trunk (rev1479)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
 
2
 
 
3
# Copyright 2011 OpenStack LLC.
 
4
# All Rights Reserved.
 
5
#
 
6
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
 
7
#    not use this file except in compliance with the License. You may obtain
 
8
#    a copy of the License at
 
9
#
 
10
#         http://www.apache.org/licenses/LICENSE-2.0
 
11
#
 
12
#    Unless required by applicable law or agreed to in writing, software
 
13
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 
14
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 
15
#    License for the specific language governing permissions and limitations
 
16
#    under the License.
 
17
 
 
18
import webob
 
19
 
 
20
from nova import db
 
21
from nova import exception
 
22
from nova import quota
 
23
from nova.api.openstack import extensions
 
24
 
 
25
 
 
26
class QuotaSetsController(object):
 
27
 
 
28
    def _format_quota_set(self, project_id, quota_set):
 
29
        """Convert the quota object to a result dict"""
 
30
 
 
31
        return {'quota_set': {
 
32
            'id': str(project_id),
 
33
            'metadata_items': quota_set['metadata_items'],
 
34
            'injected_file_content_bytes':
 
35
             quota_set['injected_file_content_bytes'],
 
36
            'volumes': quota_set['volumes'],
 
37
            'gigabytes': quota_set['gigabytes'],
 
38
            'ram': quota_set['ram'],
 
39
            'floating_ips': quota_set['floating_ips'],
 
40
            'instances': quota_set['instances'],
 
41
            'injected_files': quota_set['injected_files'],
 
42
            'cores': quota_set['cores'],
 
43
        }}
 
44
 
 
45
    def show(self, req, id):
 
46
        context = req.environ['nova.context']
 
47
        try:
 
48
            db.sqlalchemy.api.authorize_project_context(context, id)
 
49
            return self._format_quota_set(id,
 
50
                                        quota.get_project_quotas(context, id))
 
51
        except exception.NotAuthorized:
 
52
            return webob.Response(status_int=403)
 
53
 
 
54
    def update(self, req, id, body):
 
55
        context = req.environ['nova.context']
 
56
        project_id = id
 
57
        resources = ['metadata_items', 'injected_file_content_bytes',
 
58
                'volumes', 'gigabytes', 'ram', 'floating_ips', 'instances',
 
59
                'injected_files', 'cores']
 
60
        for key in body['quota_set'].keys():
 
61
            if key in resources:
 
62
                value = int(body['quota_set'][key])
 
63
                try:
 
64
                    db.quota_update(context, project_id, key, value)
 
65
                except exception.ProjectQuotaNotFound:
 
66
                    db.quota_create(context, project_id, key, value)
 
67
                except exception.AdminRequired:
 
68
                    return webob.Response(status_int=403)
 
69
        return {'quota_set': quota.get_project_quotas(context, project_id)}
 
70
 
 
71
    def defaults(self, req, id):
 
72
        return self._format_quota_set(id, quota._get_default_quotas())
 
73
 
 
74
 
 
75
class Quotas(extensions.ExtensionDescriptor):
 
76
 
 
77
    def get_name(self):
 
78
        return "Quotas"
 
79
 
 
80
    def get_alias(self):
 
81
        return "os-quota-sets"
 
82
 
 
83
    def get_description(self):
 
84
        return "Quotas management support"
 
85
 
 
86
    def get_namespace(self):
 
87
        return "http://docs.openstack.org/ext/quotas-sets/api/v1.1"
 
88
 
 
89
    def get_updated(self):
 
90
        return "2011-08-08T00:00:00+00:00"
 
91
 
 
92
    def get_resources(self):
 
93
        resources = []
 
94
 
 
95
        res = extensions.ResourceExtension('os-quota-sets',
 
96
                                            QuotaSetsController(),
 
97
                                            member_actions={'defaults': 'GET'})
 
98
        resources.append(res)
 
99
 
 
100
        return resources