usage: Ensure to count resources of a given project

When retrieving resource usage, the current code calls subnet_list
twice for shared=False/True, but 'shared' attribute of subnet is
not defined in the Networking API and actually there is no need to
use it (even though it works as expected accidentally).

What we need here is to specify tenant_id as a query parameter
with a single API to limit the scope to a given project.

The same way can be used for network_list() and router_list() calls
and it is more efficient. By doing so, we now need only one
network_list() call.

Change-Id: I40d61ed9cbae4b083e4f3cec9caa269e92daf306
Closes-Bug: #1663170
This commit is contained in:
MinSun 2017-02-10 09:00:35 +08:00 committed by Akihiro Motoki
parent 5d5dcf3e9e
commit af8a01b362
2 changed files with 10 additions and 95 deletions

View File

@ -346,80 +346,14 @@ class FloatingIpNeutronViewTests(FloatingIpViewTests):
.AndReturn(True)
api.neutron.tenant_quota_get(IsA(http.HttpRequest), self.tenant.id) \
.AndReturn(self.neutron_quotas.first())
api.neutron.router_list(IsA(http.HttpRequest)) \
api.neutron.router_list(IsA(http.HttpRequest),
tenant_id=self.tenant.id) \
.AndReturn(self.routers.list())
api.neutron.subnet_list(IsA(http.HttpRequest), shared=False) \
api.neutron.subnet_list(IsA(http.HttpRequest),
tenant_id=self.tenant.id) \
.AndReturn(self.subnets.list())
api.neutron.subnet_list(IsA(http.HttpRequest), shared=True) \
.AndReturn(list())
api.neutron.network_list(IsA(http.HttpRequest), shared=False) \
.AndReturn(self.networks.list())
api.neutron.network_list(IsA(http.HttpRequest), shared=True) \
.AndReturn(list())
api.network.floating_ip_supported(IsA(http.HttpRequest)) \
.AndReturn(True)
api.network.tenant_floating_ip_list(IsA(http.HttpRequest)) \
.MultipleTimes().AndReturn(self.floating_ips.list())
api.network.floating_ip_pools_list(IsA(http.HttpRequest)) \
.AndReturn(self.pools.list())
api.network.security_group_list(IsA(http.HttpRequest)) \
.AndReturn(self.security_groups.list())
self.mox.ReplayAll()
url = reverse('%s:allocate' % NAMESPACE)
res = self.client.get(url)
self.assertEqual(res.context['usages']['floating_ips']['quota'],
self.neutron_quotas.first().get('floatingip').limit)
@test.create_stubs({api.nova: ('tenant_quota_get', 'flavor_list',
'server_list'),
api.network: ('floating_ip_pools_list',
'floating_ip_supported',
'security_group_list',
'tenant_floating_ip_list'),
api.neutron: ('is_extension_supported',
'is_router_enabled',
'tenant_quota_get',
'network_list',
'router_list',
'subnet_list'),
api.base: ('is_service_enabled',),
api.cinder: ('is_volume_service_enabled',)})
@test.update_settings(OPENSTACK_NEUTRON_NETWORK={'enable_quotas': True})
def test_correct_quotas_displayed_shared_networks(self):
servers = [s for s in self.servers.list()
if s.tenant_id == self.request.user.tenant_id]
api.cinder.is_volume_service_enabled(IsA(http.HttpRequest)) \
.AndReturn(False)
api.base.is_service_enabled(IsA(http.HttpRequest), 'network') \
.MultipleTimes().AndReturn(True)
api.base.is_service_enabled(IsA(http.HttpRequest), 'compute') \
.MultipleTimes().AndReturn(True)
api.nova.tenant_quota_get(IsA(http.HttpRequest), '1') \
.AndReturn(self.quotas.first())
api.nova.flavor_list(IsA(http.HttpRequest)) \
.AndReturn(self.flavors.list())
search_opts = {'tenant_id': self.request.user.tenant_id}
api.nova.server_list(IsA(http.HttpRequest), search_opts=search_opts) \
.AndReturn([servers, False])
api.neutron.is_extension_supported(
IsA(http.HttpRequest), 'security-group').AndReturn(True)
api.neutron.is_extension_supported(IsA(http.HttpRequest), 'quotas') \
.AndReturn(True)
api.neutron.is_router_enabled(
IsA(http.HttpRequest)).AndReturn(True)
api.neutron.tenant_quota_get(IsA(http.HttpRequest), self.tenant.id) \
.AndReturn(self.neutron_quotas.first())
api.neutron.router_list(IsA(http.HttpRequest)) \
.AndReturn(self.routers.list())
api.neutron.subnet_list(IsA(http.HttpRequest), shared=False) \
.AndReturn(list())
api.neutron.subnet_list(IsA(http.HttpRequest), shared=True) \
.AndReturn(self.subnets.list())
api.neutron.network_list(IsA(http.HttpRequest), shared=False) \
.AndReturn(list())
api.neutron.network_list(IsA(http.HttpRequest), shared=True) \
api.neutron.network_list(IsA(http.HttpRequest),
tenant_id=self.tenant.id) \
.AndReturn(self.networks.list())
api.network.floating_ip_supported(IsA(http.HttpRequest)) \
.AndReturn(True)

View File

@ -337,34 +337,15 @@ def _get_tenant_network_usages(request, usages, disabled_quotas, tenant_id):
usages.tally('security_groups', len(security_groups))
if 'network' not in disabled_quotas:
networks = []
networks = neutron.network_list(request, shared=False)
if tenant_id:
networks = [net for net in networks if net.tenant_id == tenant_id]
networks = neutron.network_list(request, tenant_id=tenant_id)
usages.tally('networks', len(networks))
# get shared networks
shared_networks = neutron.network_list(request, shared=True)
if tenant_id:
shared_networks = [net for net in shared_networks
if net.tenant_id == tenant_id]
usages.tally('networks', len(shared_networks))
if 'subnet' not in disabled_quotas:
subnets = neutron.subnet_list(request, shared=False)
if tenant_id:
subnets = [sub for sub in subnets if sub.tenant_id == tenant_id]
# get shared subnets
shared_subnets = neutron.subnet_list(request, shared=True)
if tenant_id:
shared_subnets = [subnet for subnet in shared_subnets
if subnet.tenant_id == tenant_id]
usages.tally('subnets', len(subnets) + len(shared_subnets))
subnets = neutron.subnet_list(request, tenant_id=tenant_id)
usages.tally('subnets', len(subnets))
if 'router' not in disabled_quotas:
routers = []
routers = neutron.router_list(request)
if tenant_id:
routers = [rou for rou in routers if rou.tenant_id == tenant_id]
routers = neutron.router_list(request, tenant_id=tenant_id)
usages.tally('routers', len(routers))