From cd3f3e51275a29a7c30c711370c8257c3d07aaea Mon Sep 17 00:00:00 2001 From: Akihiro Motoki Date: Sun, 3 Dec 2017 10:21:05 +0900 Subject: [PATCH] quota: Use neutron quota_details API to retrieve usage Previously neutron did not provide a way to retrieve resource usage in a single API, so we need to call list APIs and count the number of resources. Since Pike release, quota details API is provided and it returns both quota and usage (used + reserved). blueprint make-quotas-great-again Change-Id: I63d0e81654c18f0f235631922d64d1109233fcfa --- openstack_dashboard/api/neutron.py | 7 ++++++ .../dashboards/project/floating_ips/tests.py | 2 ++ .../test/unit/usage/test_quotas.py | 2 ++ openstack_dashboard/usage/quotas.py | 23 +++++++++++++++++++ 4 files changed, 34 insertions(+) diff --git a/openstack_dashboard/api/neutron.py b/openstack_dashboard/api/neutron.py index 15f548108c..86a8f44f75 100644 --- a/openstack_dashboard/api/neutron.py +++ b/openstack_dashboard/api/neutron.py @@ -1445,6 +1445,13 @@ def tenant_quota_update(request, tenant_id, **kwargs): return neutronclient(request).update_quota(tenant_id, quotas) +@profiler.trace +def tenant_quota_detail_get(request, tenant_id=None): + tenant_id = tenant_id or request.user.tenant_id + response = neutronclient(request).get('/quotas/%s/details' % tenant_id) + return response['quota'] + + @profiler.trace def agent_list(request, **params): agents = neutronclient(request).list_agents(**params) diff --git a/openstack_dashboard/dashboards/project/floating_ips/tests.py b/openstack_dashboard/dashboards/project/floating_ips/tests.py index e7fc4bd276..51f2d0bfef 100644 --- a/openstack_dashboard/dashboards/project/floating_ips/tests.py +++ b/openstack_dashboard/dashboards/project/floating_ips/tests.py @@ -333,6 +333,8 @@ class FloatingIpViewTests(test.TestCase): .AndReturn(True) api.neutron.is_router_enabled(IsA(http.HttpRequest)) \ .AndReturn(True) + api.neutron.is_extension_supported(IsA(http.HttpRequest), + 'quota_details').AndReturn(False) api.neutron.tenant_quota_get(IsA(http.HttpRequest), self.tenant.id) \ .AndReturn(self.neutron_quotas.first()) api.neutron.floating_ip_supported(IsA(http.HttpRequest)) \ diff --git a/openstack_dashboard/test/unit/usage/test_quotas.py b/openstack_dashboard/test/unit/usage/test_quotas.py index 5cd907723a..6af3bd0618 100644 --- a/openstack_dashboard/test/unit/usage/test_quotas.py +++ b/openstack_dashboard/test/unit/usage/test_quotas.py @@ -472,6 +472,8 @@ class QuotaTests(test.APITestCase): api.base.is_service_enabled(IsA(http.HttpRequest), 'compute') \ .MultipleTimes().AndReturn(True) + api.neutron.is_extension_supported(IsA(http.HttpRequest), + 'quota_details').AndReturn(False) api.neutron.tenant_quota_get(IsA(http.HttpRequest), '1') \ .AndReturn(self.neutron_quotas.first()) if 'networks' in targets: diff --git a/openstack_dashboard/usage/quotas.py b/openstack_dashboard/usage/quotas.py index 54aaf4711f..ce3925d5b3 100644 --- a/openstack_dashboard/usage/quotas.py +++ b/openstack_dashboard/usage/quotas.py @@ -355,6 +355,29 @@ def _get_tenant_network_usages(request, usages, disabled_quotas, tenant_id): if not enabled_quotas: return + if neutron.is_extension_supported(request, 'quota_details'): + details = neutron.tenant_quota_detail_get(request, tenant_id) + for name, neutron_name in ( + ('floating_ips', 'floatingip'), + ('security_groups', 'security_group'), + ('security_group_rules', 'security_group_rule'), + ('networks', 'network'), + ('subnets', 'subnet'), + ('ports', 'port'), + ('routers', 'router')): + if neutron_name in disabled_quotas: + continue + detail = details[neutron_name] + usages.add_quota(base.Quota(name, detail['limit'])) + usages.tally(name, detail['used'] + detail['reserved']) + else: + _get_tenant_network_usages_legacy( + request, usages, disabled_quotas, tenant_id) + + +def _get_tenant_network_usages_legacy(request, usages, disabled_quotas, + tenant_id): + enabled_quotas = NEUTRON_QUOTA_FIELDS - disabled_quotas qs = base.QuotaSet() _get_neutron_quota_data(request, qs, disabled_quotas, tenant_id) for quota in qs: