Merge "Remove used_limits extension from the V3 API"
This commit is contained in:
commit
da7cfbbb6b
|
@ -239,9 +239,6 @@
|
|||
"compute_extension:availability_zone:detail": "rule:admin_api",
|
||||
"compute_extension:v3:os-availability-zone:detail": "rule:admin_api",
|
||||
"compute_extension:used_limits_for_admin": "rule:admin_api",
|
||||
"compute_extension:v3:os-used-limits": "",
|
||||
"compute_extension:v3:os-used-limits:discoverable": "",
|
||||
"compute_extension:v3:os-used-limits:tenant": "rule:admin_api",
|
||||
"compute_extension:migrations:index": "rule:admin_api",
|
||||
"compute_extension:v3:os-migrations:index": "rule:admin_api",
|
||||
"compute_extension:v3:os-migrations:discoverable": "",
|
||||
|
|
|
@ -1,97 +0,0 @@
|
|||
# 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 nova.api.openstack import extensions
|
||||
from nova.api.openstack import wsgi
|
||||
from nova.api.openstack import xmlutil
|
||||
from nova import quota
|
||||
|
||||
|
||||
QUOTAS = quota.QUOTAS
|
||||
|
||||
|
||||
XMLNS = "http://docs.openstack.org/compute/ext/used_limits/api/v3"
|
||||
ALIAS = "os-used-limits"
|
||||
authorize = extensions.extension_authorizer('compute', 'v3:' + ALIAS)
|
||||
|
||||
|
||||
class UsedLimitsTemplate(xmlutil.TemplateBuilder):
|
||||
def construct(self):
|
||||
root = xmlutil.TemplateElement('limits', selector='limits')
|
||||
root.set('{%s}usedLimits' % XMLNS, '%s:usedLimits' % ALIAS)
|
||||
return xmlutil.SlaveTemplate(root, 1, nsmap={ALIAS: XMLNS})
|
||||
|
||||
|
||||
class UsedLimitsController(wsgi.Controller):
|
||||
|
||||
@staticmethod
|
||||
def _reserved(req):
|
||||
try:
|
||||
return int(req.GET['reserved'])
|
||||
except (ValueError, KeyError):
|
||||
return False
|
||||
|
||||
@wsgi.extends
|
||||
def index(self, req, resp_obj):
|
||||
context = req.environ['nova.context']
|
||||
authorize(context)
|
||||
resp_obj.attach(xml=UsedLimitsTemplate())
|
||||
project_id = self._project_id(context, req)
|
||||
quotas = QUOTAS.get_project_quotas(context, project_id, usages=True)
|
||||
quota_map = {
|
||||
'totalRAMUsed': 'ram',
|
||||
'totalCoresUsed': 'cores',
|
||||
'totalInstancesUsed': 'instances',
|
||||
'totalFloatingIpsUsed': 'floating_ips',
|
||||
'totalSecurityGroupsUsed': 'security_groups',
|
||||
}
|
||||
used_limits = {}
|
||||
for display_name, quota in quota_map.iteritems():
|
||||
if quota in quotas:
|
||||
reserved = (quotas[quota]['reserved']
|
||||
if self._reserved(req) else 0)
|
||||
used_limits[display_name] = quotas[quota]['in_use'] + reserved
|
||||
|
||||
resp_obj.obj['limits']['absolute'].update(used_limits)
|
||||
|
||||
def _project_id(self, context, req):
|
||||
if 'tenant_id' in req.GET:
|
||||
tenant_id = req.GET.get('tenant_id')
|
||||
target = {
|
||||
'project_id': tenant_id,
|
||||
'user_id': context.user_id
|
||||
}
|
||||
authorize(context, target=target, action='tenant')
|
||||
return tenant_id
|
||||
return context.project_id
|
||||
|
||||
|
||||
class UsedLimits(extensions.V3APIExtensionBase):
|
||||
"""Provide data on limited resources that are being used."""
|
||||
|
||||
name = "UsedLimits"
|
||||
alias = ALIAS
|
||||
namespace = XMLNS
|
||||
version = 1
|
||||
|
||||
def get_controller_extensions(self):
|
||||
controller = UsedLimitsController()
|
||||
limits_ext = extensions.ControllerExtension(self, 'limits',
|
||||
controller=controller)
|
||||
return [limits_ext]
|
||||
|
||||
def get_resources(self):
|
||||
return []
|
|
@ -1,250 +0,0 @@
|
|||
# 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 nova.api.openstack.compute import limits
|
||||
from nova.api.openstack.compute.plugins.v3 import used_limits
|
||||
from nova.api.openstack import extensions
|
||||
from nova.api.openstack import wsgi
|
||||
import nova.context
|
||||
from nova import exception
|
||||
from nova import quota
|
||||
from nova import test
|
||||
|
||||
|
||||
class FakeRequest(object):
|
||||
def __init__(self, context, reserved=False):
|
||||
self.environ = {'nova.context': context}
|
||||
self.reserved = reserved
|
||||
self.GET = {'reserved': 1} if reserved else {}
|
||||
|
||||
|
||||
class UsedLimitsTestCase(test.NoDBTestCase):
|
||||
def setUp(self):
|
||||
"""Run before each test."""
|
||||
super(UsedLimitsTestCase, self).setUp()
|
||||
self.controller = used_limits.UsedLimitsController()
|
||||
|
||||
self.fake_context = nova.context.RequestContext('fake', 'fake')
|
||||
self.mox.StubOutWithMock(used_limits, 'authorize')
|
||||
self.authorize = used_limits.authorize
|
||||
|
||||
def _do_test_used_limits(self, reserved):
|
||||
fake_req = FakeRequest(self.fake_context, reserved=reserved)
|
||||
obj = {
|
||||
"limits": {
|
||||
"rate": [],
|
||||
"absolute": {},
|
||||
},
|
||||
}
|
||||
res = wsgi.ResponseObject(obj)
|
||||
quota_map = {
|
||||
'totalRAMUsed': 'ram',
|
||||
'totalCoresUsed': 'cores',
|
||||
'totalInstancesUsed': 'instances',
|
||||
'totalFloatingIpsUsed': 'floating_ips',
|
||||
'totalSecurityGroupsUsed': 'security_groups',
|
||||
}
|
||||
limits = {}
|
||||
for display_name, q in quota_map.iteritems():
|
||||
limits[q] = {'limit': len(display_name),
|
||||
'in_use': len(display_name) / 2,
|
||||
'reserved': len(display_name) / 3}
|
||||
|
||||
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.authorize(self.fake_context)
|
||||
self.mox.ReplayAll()
|
||||
|
||||
self.controller.index(fake_req, res)
|
||||
abs_limits = res.obj['limits']['absolute']
|
||||
for used_limit, value in abs_limits.iteritems():
|
||||
r = limits[quota_map[used_limit]]['reserved'] if reserved else 0
|
||||
self.assertEqual(value,
|
||||
limits[quota_map[used_limit]]['in_use'] + r)
|
||||
|
||||
def test_used_limits_basic(self):
|
||||
self._do_test_used_limits(False)
|
||||
|
||||
def test_used_limits_with_reserved(self):
|
||||
self._do_test_used_limits(True)
|
||||
|
||||
def test_admin_can_fetch_limits_for_a_given_tenant_id(self):
|
||||
project_id = "123456"
|
||||
user_id = "A1234"
|
||||
tenant_id = 'abcd'
|
||||
self.fake_context.project_id = project_id
|
||||
self.fake_context.user_id = user_id
|
||||
obj = {
|
||||
"limits": {
|
||||
"rate": [],
|
||||
"absolute": {},
|
||||
},
|
||||
}
|
||||
target = {
|
||||
"project_id": tenant_id,
|
||||
"user_id": user_id
|
||||
}
|
||||
fake_req = FakeRequest(self.fake_context)
|
||||
fake_req.GET = {'tenant_id': tenant_id}
|
||||
self.authorize(self.fake_context)
|
||||
self.authorize(self.fake_context, target=target, action='tenant')
|
||||
self.mox.StubOutWithMock(quota.QUOTAS, 'get_project_quotas')
|
||||
quota.QUOTAS.get_project_quotas(self.fake_context, '%s' % tenant_id,
|
||||
usages=True).AndReturn({})
|
||||
self.mox.ReplayAll()
|
||||
res = wsgi.ResponseObject(obj)
|
||||
self.controller.index(fake_req, res)
|
||||
|
||||
def test_admin_can_fetch_used_limits_for_own_project(self):
|
||||
project_id = "123456"
|
||||
user_id = "A1234"
|
||||
self.fake_context.project_id = project_id
|
||||
self.fake_context.user_id = user_id
|
||||
obj = {
|
||||
"limits": {
|
||||
"rate": [],
|
||||
"absolute": {},
|
||||
},
|
||||
}
|
||||
fake_req = FakeRequest(self.fake_context)
|
||||
fake_req.GET = {}
|
||||
self.mox.StubOutWithMock(extensions, 'extension_authorizer')
|
||||
self.mox.StubOutWithMock(quota.QUOTAS, 'get_project_quotas')
|
||||
self.authorize(self.fake_context)
|
||||
quota.QUOTAS.get_project_quotas(self.fake_context, '%s' % project_id,
|
||||
usages=True).AndReturn({})
|
||||
self.mox.ReplayAll()
|
||||
res = wsgi.ResponseObject(obj)
|
||||
self.controller.index(fake_req, res)
|
||||
|
||||
def test_non_admin_cannot_fetch_used_limits_for_any_other_project(self):
|
||||
project_id = "123456"
|
||||
user_id = "A1234"
|
||||
tenant_id = "abcd"
|
||||
self.fake_context.project_id = project_id
|
||||
self.fake_context.user_id = user_id
|
||||
obj = {
|
||||
"limits": {
|
||||
"rate": [],
|
||||
"absolute": {},
|
||||
},
|
||||
}
|
||||
target = {
|
||||
"project_id": tenant_id,
|
||||
"user_id": user_id
|
||||
}
|
||||
fake_req = FakeRequest(self.fake_context)
|
||||
fake_req.GET = {'tenant_id': tenant_id}
|
||||
self.authorize(self.fake_context)
|
||||
self.authorize(self.fake_context, target=target, action='tenant'). \
|
||||
AndRaise(exception.PolicyNotAuthorized(
|
||||
action="compute_extension:used_limits_for_admin"))
|
||||
self.mox.ReplayAll()
|
||||
res = wsgi.ResponseObject(obj)
|
||||
self.assertRaises(exception.PolicyNotAuthorized, self.controller.index,
|
||||
fake_req, res)
|
||||
|
||||
def test_used_limits_fetched_for_context_project_id(self):
|
||||
project_id = "123456"
|
||||
self.fake_context.project_id = project_id
|
||||
obj = {
|
||||
"limits": {
|
||||
"rate": [],
|
||||
"absolute": {},
|
||||
},
|
||||
}
|
||||
fake_req = FakeRequest(self.fake_context)
|
||||
self.mox.StubOutWithMock(quota.QUOTAS, 'get_project_quotas')
|
||||
self.authorize(self.fake_context)
|
||||
quota.QUOTAS.get_project_quotas(self.fake_context, project_id,
|
||||
usages=True).AndReturn({})
|
||||
self.mox.ReplayAll()
|
||||
res = wsgi.ResponseObject(obj)
|
||||
self.controller.index(fake_req, res)
|
||||
|
||||
def test_used_ram_added(self):
|
||||
fake_req = FakeRequest(self.fake_context)
|
||||
obj = {
|
||||
"limits": {
|
||||
"rate": [],
|
||||
"absolute": {
|
||||
"maxTotalRAMSize": 512,
|
||||
},
|
||||
},
|
||||
}
|
||||
res = wsgi.ResponseObject(obj)
|
||||
|
||||
def stub_get_project_quotas(context, project_id, usages=True):
|
||||
return {'ram': {'limit': 512, 'in_use': 256}}
|
||||
|
||||
self.stubs.Set(quota.QUOTAS, "get_project_quotas",
|
||||
stub_get_project_quotas)
|
||||
self.authorize(self.fake_context)
|
||||
self.mox.ReplayAll()
|
||||
|
||||
self.controller.index(fake_req, res)
|
||||
abs_limits = res.obj['limits']['absolute']
|
||||
self.assertIn('totalRAMUsed', abs_limits)
|
||||
self.assertEqual(abs_limits['totalRAMUsed'], 256)
|
||||
|
||||
def test_no_ram_quota(self):
|
||||
fake_req = FakeRequest(self.fake_context)
|
||||
obj = {
|
||||
"limits": {
|
||||
"rate": [],
|
||||
"absolute": {},
|
||||
},
|
||||
}
|
||||
res = wsgi.ResponseObject(obj)
|
||||
|
||||
def stub_get_project_quotas(context, project_id, usages=True):
|
||||
return {}
|
||||
|
||||
self.stubs.Set(quota.QUOTAS, "get_project_quotas",
|
||||
stub_get_project_quotas)
|
||||
self.authorize(self.fake_context)
|
||||
self.mox.ReplayAll()
|
||||
|
||||
self.controller.index(fake_req, res)
|
||||
abs_limits = res.obj['limits']['absolute']
|
||||
self.assertNotIn('totalRAMUsed', abs_limits)
|
||||
|
||||
def test_used_limits_xmlns(self):
|
||||
fake_req = FakeRequest(self.fake_context)
|
||||
obj = {
|
||||
"limits": {
|
||||
"rate": [],
|
||||
"absolute": {},
|
||||
},
|
||||
}
|
||||
res = wsgi.ResponseObject(obj, xml=limits.LimitsTemplate)
|
||||
res.preserialize('xml')
|
||||
|
||||
def stub_get_project_quotas(context, project_id, usages=True):
|
||||
return {}
|
||||
|
||||
self.stubs.Set(quota.QUOTAS, "get_project_quotas",
|
||||
stub_get_project_quotas)
|
||||
self.authorize(self.fake_context)
|
||||
self.mox.ReplayAll()
|
||||
|
||||
self.controller.index(fake_req, res)
|
||||
response = res.serialize(None, 'xml')
|
||||
self.assertIn(used_limits.XMLNS, response.body)
|
|
@ -282,8 +282,6 @@ policy_data = """
|
|||
"compute_extension:availability_zone:detail": "is_admin:True",
|
||||
"compute_extension:v3:os-availability-zone:detail": "is_admin:True",
|
||||
"compute_extension:used_limits_for_admin": "is_admin:True",
|
||||
"compute_extension:v3:os-used-limits": "",
|
||||
"compute_extension:v3:os-used-limits:tenant": "is_admin:True",
|
||||
"compute_extension:migrations:index": "is_admin:True",
|
||||
"compute_extension:v3:os-migrations:index": "is_admin:True",
|
||||
"compute_extension:os-assisted-volume-snapshots:create": "",
|
||||
|
|
|
@ -107,7 +107,6 @@ nova.api.v3.extensions =
|
|||
services = nova.api.openstack.compute.plugins.v3.services:Services
|
||||
shelve = nova.api.openstack.compute.plugins.v3.shelve:Shelve
|
||||
simple_tenant_usage = nova.api.openstack.compute.plugins.v3.simple_tenant_usage:SimpleTenantUsage
|
||||
used_limits = nova.api.openstack.compute.plugins.v3.used_limits:UsedLimits
|
||||
versions = nova.api.openstack.compute.plugins.v3.versions:Versions
|
||||
|
||||
nova.api.v3.extensions.server.create =
|
||||
|
|
Loading…
Reference in New Issue