]> review.fuel-infra Code Review - openstack-build/cinder-build.git/commitdiff
Cinder extension to add used resources in absolute limits
authorOlivier Pilotte <opilotte@iweb.com>
Thu, 3 Oct 2013 22:10:40 +0000 (18:10 -0400)
committerOlivier Pilotte <opilotte@iweb.com>
Wed, 16 Oct 2013 13:51:19 +0000 (09:51 -0400)
* totalVolumesUsed
* totalGigabytesUsed

DocImpact
Implements: blueprint cinder-used-resources-in-absolute-limits
Change-Id: I525a61043ce2f906c424738ab361ee7396f072b5

cinder/api/contrib/used_limits.py [new file with mode: 0644]
cinder/tests/api/contrib/test_used_limits.py [new file with mode: 0644]
cinder/tests/policy.json

diff --git a/cinder/api/contrib/used_limits.py b/cinder/api/contrib/used_limits.py
new file mode 100644 (file)
index 0000000..63e9af4
--- /dev/null
@@ -0,0 +1,60 @@
+# vim: tabstop=4 shiftwidth=4 softtabstop=4
+
+# Copyright 2012 OpenStack Foundation
+#
+#    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 cinder.api import extensions
+from cinder.api.openstack import wsgi
+from cinder import quota
+
+QUOTAS = quota.QUOTAS
+
+authorize = extensions.extension_authorizer('limits', 'used_limits')
+
+
+class UsedLimitsController(wsgi.Controller):
+
+    @wsgi.extends
+    def index(self, req, resp_obj):
+        context = req.environ['cinder.context']
+        authorize(context)
+
+        quotas = QUOTAS.get_project_quotas(context, context.project_id,
+                                           usages=True)
+
+        quota_map = {
+            'totalVolumesUsed': 'volumes',
+            'totalGigabytesUsed': 'gigabytes',
+        }
+
+        used_limits = {}
+        for display_name, quota in quota_map.iteritems():
+            if quota in quotas:
+                used_limits[display_name] = quotas[quota]['in_use']
+
+        resp_obj.obj['limits']['absolute'].update(used_limits)
+
+
+class Used_limits(extensions.ExtensionDescriptor):
+    """Provide data on limited resources that are being used."""
+
+    name = "UsedLimits"
+    alias = 'os-used-limits'
+    namespace = "http://docs.openstack.org/volume/ext/used-limits/api/v1.1"
+    updated = "2013-10-03T00:00:00+00:00"
+
+    def get_controller_extensions(self):
+        controller = UsedLimitsController()
+        extension = extensions.ControllerExtension(self, 'limits', controller)
+        return [extension]
diff --git a/cinder/tests/api/contrib/test_used_limits.py b/cinder/tests/api/contrib/test_used_limits.py
new file mode 100644 (file)
index 0000000..d102253
--- /dev/null
@@ -0,0 +1,67 @@
+# vim: tabstop=5 shiftwidth=4 softtabstop=4
+
+# Copyright 2012 OpenStack Foundation
+# 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 cinder.api.contrib import used_limits
+from cinder.api.openstack import wsgi
+from cinder import quota
+from cinder import test
+from cinder.tests.api import fakes
+
+
+class FakeRequest(object):
+    def __init__(self, context):
+        self.environ = {'cinder.context': context}
+
+
+class UsedLimitsTestCase(test.TestCase):
+    def setUp(self):
+        """Run before each test."""
+        super(UsedLimitsTestCase, self).setUp()
+        self.controller = used_limits.UsedLimitsController()
+
+    def test_used_limits(self):
+        fake_req = FakeRequest(fakes.FakeRequestContext('fake', 'fake'))
+        obj = {
+            "limits": {
+                "rate": [],
+                "absolute": {},
+            },
+        }
+        res = wsgi.ResponseObject(obj)
+        quota_map = {
+            'totalVolumesUsed': 'volumes',
+            'totalGigabytesUsed': 'gigabytes',
+        }
+
+        limits = {}
+        for display_name, q in quota_map.iteritems():
+            limits[q] = {'limit': 2,
+                         'in_use': 1}
+
+        def stub_get_project_quotas(context, project_id, usages=True):
+            return limits
+
+        self.stubs.Set(quota.QUOTAS, "get_project_quotas",
+                       stub_get_project_quotas)
+
+        self.mox.ReplayAll()
+
+        self.controller.index(fake_req, res)
+        abs_limits = res.obj['limits']['absolute']
+        for used_limit, value in abs_limits.iteritems():
+            self.assertEqual(value,
+                             limits[quota_map[used_limit]]['in_use'])
index c8a8a9347b89570044e1db6579c5c56c59819bdd..eee2b9cc862f291fb9fd36400f6b2f6ef4a732e2 100644 (file)
@@ -56,6 +56,8 @@
     "volume_extension:quotas:show": [],
     "volume_extension:quotas:update": [],
 
+    "limits_extension:used_limits": [],
+
     "snapshot_extension:snapshot_actions:update_snapshot_status": [],
 
     "volume:create_transfer": [],