From c98a2cf238b987386cb48dc567683965aae324f8 Mon Sep 17 00:00:00 2001 From: Valeriy Ponomaryov Date: Thu, 27 Mar 2014 15:59:39 +0200 Subject: [PATCH] Added extension that provides used resources in absolute limits Change-Id: Ic042c382bddb8d52518ce3afc75786b1386c1231 --- manila/api/contrib/used_limits.py | 61 ++++++++++++++++++++ manila/tests/api/contrib/test_used_limits.py | 61 ++++++++++++++++++++ manila/tests/policy.json | 4 +- 3 files changed, 125 insertions(+), 1 deletion(-) create mode 100644 manila/api/contrib/used_limits.py create mode 100644 manila/tests/api/contrib/test_used_limits.py diff --git a/manila/api/contrib/used_limits.py b/manila/api/contrib/used_limits.py new file mode 100644 index 0000000000..0809d3b326 --- /dev/null +++ b/manila/api/contrib/used_limits.py @@ -0,0 +1,61 @@ +# Copyright 2014 Mirantis Inc. +# +# 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 manila.api import extensions +from manila.api.openstack import wsgi +from manila 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['manila.context'] + authorize(context) + + quotas = QUOTAS.get_project_quotas(context, + context.project_id, + usages=True) + + quota_map = { + 'totalSharesUsed': 'shares', + 'totalSnapshotsUsed': 'snapshots', + 'totalShareNetworksUsed': 'share_networks', + '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/share/ext/used-limits/api/v1.0" + updated = "2014-03-27T00:00:00+00:00" + + def get_controller_extensions(self): + controller = UsedLimitsController() + extension = extensions.ControllerExtension(self, 'limits', controller) + return [extension] diff --git a/manila/tests/api/contrib/test_used_limits.py b/manila/tests/api/contrib/test_used_limits.py new file mode 100644 index 0000000000..c988ecb83b --- /dev/null +++ b/manila/tests/api/contrib/test_used_limits.py @@ -0,0 +1,61 @@ +# Copyright 2014 Mirantis Inc. +# 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. + +import mock + +from manila.api.contrib import used_limits +from manila.api.openstack import wsgi +from manila import quota +from manila import test +from manila.tests.api import fakes + + +class FakeRequest(object): + def __init__(self, context): + self.environ = {'manila.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 = { + 'totalSharesUsed': 'shares', + 'totalSnapshotsUsed': 'snapshots', + 'totalShareNetworksUsed': 'share_networks', + 'totalGigabytesUsed': 'gigabytes', + } + limits = {} + for display_name, q in quota_map.iteritems(): + limits[q] = {'limit': 2, 'in_use': 1, } + + def stub_get_project_quotas(*args, **kwargs): + return limits + + with mock.patch.object(quota.QUOTAS, 'get_project_quotas', + mock.Mock(side_effect=stub_get_project_quotas)): + + 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']) diff --git a/manila/tests/policy.json b/manila/tests/policy.json index e13b3bfd12..e560c7bbf7 100644 --- a/manila/tests/policy.json +++ b/manila/tests/policy.json @@ -17,5 +17,7 @@ "share:delete_share_metadata": [], "share:update_share_metadata": [], "share_extension:share_admin_actions:reset_status": [["rule:admin_api"]], - "share_extension:snapshot_admin_actions:reset_status": [["rule:admin_api"]] + "share_extension:snapshot_admin_actions:reset_status": [["rule:admin_api"]], + + "limits_extension:used_limits": [] }