From af8a01b362acd85aff34dfde0a7ace197007f768 Mon Sep 17 00:00:00 2001 From: MinSun Date: Fri, 10 Feb 2017 09:00:35 +0800 Subject: [PATCH] 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 --- .../dashboards/project/floating_ips/tests.py | 78 ++----------------- openstack_dashboard/usage/quotas.py | 27 +------ 2 files changed, 10 insertions(+), 95 deletions(-) diff --git a/openstack_dashboard/dashboards/project/floating_ips/tests.py b/openstack_dashboard/dashboards/project/floating_ips/tests.py index 5ae49f825d..d34c54939c 100644 --- a/openstack_dashboard/dashboards/project/floating_ips/tests.py +++ b/openstack_dashboard/dashboards/project/floating_ips/tests.py @@ -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) diff --git a/openstack_dashboard/usage/quotas.py b/openstack_dashboard/usage/quotas.py index 794f7ff2bf..d30723d544 100644 --- a/openstack_dashboard/usage/quotas.py +++ b/openstack_dashboard/usage/quotas.py @@ -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))