Merge "Fix UsedLimitsController's authorizer to soft"
This commit is contained in:
commit
5bc425235b
@ -18,7 +18,7 @@ from cinder import quota
|
|||||||
|
|
||||||
QUOTAS = quota.QUOTAS
|
QUOTAS = quota.QUOTAS
|
||||||
|
|
||||||
authorize = extensions.extension_authorizer('limits', 'used_limits')
|
authorize = extensions.soft_extension_authorizer('limits', 'used_limits')
|
||||||
|
|
||||||
|
|
||||||
class UsedLimitsController(wsgi.Controller):
|
class UsedLimitsController(wsgi.Controller):
|
||||||
@ -26,25 +26,24 @@ class UsedLimitsController(wsgi.Controller):
|
|||||||
@wsgi.extends
|
@wsgi.extends
|
||||||
def index(self, req, resp_obj):
|
def index(self, req, resp_obj):
|
||||||
context = req.environ['cinder.context']
|
context = req.environ['cinder.context']
|
||||||
authorize(context)
|
if authorize(context):
|
||||||
|
quotas = QUOTAS.get_project_quotas(context, context.project_id,
|
||||||
|
usages=True)
|
||||||
|
|
||||||
quotas = QUOTAS.get_project_quotas(context, context.project_id,
|
quota_map = {
|
||||||
usages=True)
|
'totalVolumesUsed': 'volumes',
|
||||||
|
'totalGigabytesUsed': 'gigabytes',
|
||||||
|
'totalSnapshotsUsed': 'snapshots',
|
||||||
|
'totalBackupsUsed': 'backups',
|
||||||
|
'totalBackupGigabytesUsed': 'backup_gigabytes'
|
||||||
|
}
|
||||||
|
|
||||||
quota_map = {
|
used_limits = {}
|
||||||
'totalVolumesUsed': 'volumes',
|
for display_name, single_quota in quota_map.items():
|
||||||
'totalGigabytesUsed': 'gigabytes',
|
if single_quota in quotas:
|
||||||
'totalSnapshotsUsed': 'snapshots',
|
used_limits[display_name] = quotas[single_quota]['in_use']
|
||||||
'totalBackupsUsed': 'backups',
|
|
||||||
'totalBackupGigabytesUsed': 'backup_gigabytes'
|
|
||||||
}
|
|
||||||
|
|
||||||
used_limits = {}
|
resp_obj.obj['limits']['absolute'].update(used_limits)
|
||||||
for display_name, single_quota in quota_map.items():
|
|
||||||
if single_quota in quotas:
|
|
||||||
used_limits[display_name] = quotas[single_quota]['in_use']
|
|
||||||
|
|
||||||
resp_obj.obj['limits']['absolute'].update(used_limits)
|
|
||||||
|
|
||||||
|
|
||||||
class Used_limits(extensions.ExtensionDescriptor):
|
class Used_limits(extensions.ExtensionDescriptor):
|
||||||
|
@ -13,9 +13,11 @@
|
|||||||
# License for the specific language governing permissions and limitations
|
# License for the specific language governing permissions and limitations
|
||||||
# under the License.
|
# under the License.
|
||||||
|
|
||||||
|
import mock
|
||||||
|
|
||||||
from cinder.api.contrib import used_limits
|
from cinder.api.contrib import used_limits
|
||||||
from cinder.api.openstack import wsgi
|
from cinder.api.openstack import wsgi
|
||||||
from cinder import quota
|
from cinder import exception
|
||||||
from cinder import test
|
from cinder import test
|
||||||
from cinder.tests.unit.api import fakes
|
from cinder.tests.unit.api import fakes
|
||||||
|
|
||||||
@ -31,7 +33,9 @@ class UsedLimitsTestCase(test.TestCase):
|
|||||||
super(UsedLimitsTestCase, self).setUp()
|
super(UsedLimitsTestCase, self).setUp()
|
||||||
self.controller = used_limits.UsedLimitsController()
|
self.controller = used_limits.UsedLimitsController()
|
||||||
|
|
||||||
def test_used_limits(self):
|
@mock.patch('cinder.quota.QUOTAS.get_project_quotas')
|
||||||
|
@mock.patch('cinder.policy.enforce')
|
||||||
|
def test_used_limits(self, _mock_policy_enforce, _mock_get_project_quotas):
|
||||||
fake_req = FakeRequest(fakes.FakeRequestContext('fake', 'fake'))
|
fake_req = FakeRequest(fakes.FakeRequestContext('fake', 'fake'))
|
||||||
obj = {
|
obj = {
|
||||||
"limits": {
|
"limits": {
|
||||||
@ -50,17 +54,30 @@ class UsedLimitsTestCase(test.TestCase):
|
|||||||
for display_name, q in quota_map.items():
|
for display_name, q in quota_map.items():
|
||||||
limits[q] = {'limit': 2,
|
limits[q] = {'limit': 2,
|
||||||
'in_use': 1}
|
'in_use': 1}
|
||||||
|
_mock_get_project_quotas.return_value = limits
|
||||||
|
|
||||||
def stub_get_project_quotas(context, project_id, usages=True):
|
# allow user to access used limits
|
||||||
return limits
|
_mock_policy_enforce.return_value = None
|
||||||
|
|
||||||
self.stubs.Set(quota.QUOTAS, "get_project_quotas",
|
|
||||||
stub_get_project_quotas)
|
|
||||||
|
|
||||||
self.mox.ReplayAll()
|
|
||||||
|
|
||||||
self.controller.index(fake_req, res)
|
self.controller.index(fake_req, res)
|
||||||
abs_limits = res.obj['limits']['absolute']
|
abs_limits = res.obj['limits']['absolute']
|
||||||
for used_limit, value in abs_limits.items():
|
for used_limit, value in abs_limits.items():
|
||||||
self.assertEqual(value,
|
self.assertEqual(value,
|
||||||
limits[quota_map[used_limit]]['in_use'])
|
limits[quota_map[used_limit]]['in_use'])
|
||||||
|
|
||||||
|
obj = {
|
||||||
|
"limits": {
|
||||||
|
"rate": [],
|
||||||
|
"absolute": {},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
res = wsgi.ResponseObject(obj)
|
||||||
|
|
||||||
|
# unallow user to access used limits
|
||||||
|
_mock_policy_enforce.side_effect = exception.NotAuthorized
|
||||||
|
|
||||||
|
self.controller.index(fake_req, res)
|
||||||
|
abs_limits = res.obj['limits']['absolute']
|
||||||
|
self.assertNotIn('totalVolumesUsed', abs_limits)
|
||||||
|
self.assertNotIn('totalGigabytesUsed', abs_limits)
|
||||||
|
self.assertNotIn('totalSnapshotsUsed', abs_limits)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user