diff --git a/openstack_dashboard/dashboards/admin/defaults/tabs.py b/openstack_dashboard/dashboards/admin/defaults/tabs.py index 081f3658b3..b00521089c 100644 --- a/openstack_dashboard/dashboards/admin/defaults/tabs.py +++ b/openstack_dashboard/dashboards/admin/defaults/tabs.py @@ -19,7 +19,6 @@ from django.utils.translation import ugettext_lazy as _ # noqa from horizon import exceptions from horizon import tabs -from openstack_dashboard.api import base from openstack_dashboard.usage import quotas from openstack_dashboard.dashboards.admin.defaults import tables @@ -34,14 +33,7 @@ class DefaultQuotasTab(tabs.TableTab): def get_quotas_data(self): request = self.tab_group.request try: - quota_set = quotas.get_default_quota_data(request) - data = quota_set.items - # There is no API to get the default system quotas in - # Neutron (cf. LP#1204956). Remove the network-related - # quotas from the list for now to avoid confusion - if base.is_service_enabled(self.request, 'network'): - data = [quota for quota in data - if quota.name not in ['floating_ips', 'fixed_ips']] + data = quotas.get_default_quota_data(request) except Exception: data = [] exceptions.handle(self.request, _('Unable to get quota info.')) diff --git a/openstack_dashboard/dashboards/admin/defaults/tests.py b/openstack_dashboard/dashboards/admin/defaults/tests.py index 0396defbb9..dbb63b2e4a 100644 --- a/openstack_dashboard/dashboards/admin/defaults/tests.py +++ b/openstack_dashboard/dashboards/admin/defaults/tests.py @@ -27,50 +27,38 @@ INDEX_URL = reverse('horizon:admin:defaults:index') class ServicesViewTests(test.BaseAdminViewTests): def test_index(self): - self.mox.StubOutWithMock(api.nova, 'default_quota_get') - self.mox.StubOutWithMock(api.cinder, 'default_quota_get') - api.nova.default_quota_get(IsA(http.HttpRequest), - self.tenant.id).AndReturn(self.quotas.nova) - api.cinder.default_quota_get(IsA(http.HttpRequest), self.tenant.id) \ - .AndReturn(self.cinder_quotas.first()) + self._test_index(neutron_enabled=True) - self.mox.ReplayAll() - - res = self.client.get(INDEX_URL) - - self.assertTemplateUsed(res, 'admin/defaults/index.html') - - quotas_tab = res.context['tab_group'].get_tab('quotas') - self.assertQuerysetEqual(quotas_tab._tables['quotas'].data, - ['', - '', - '', - '', - '', - '', - '', - '', - '', - '', - ''], - ordered=False) - - @test.create_stubs({api.base: ('is_service_enabled',), - api.nova: ('default_quota_get', 'service_list',), - api.cinder: ('default_quota_get',)}) def test_index_with_neutron_disabled(self): + self._test_index(neutron_enabled=False) + + def test_index_with_neutron_sg_disabled(self): + self._test_index(neutron_enabled=True, + neutron_sg_enabled=False) + + def _test_index(self, neutron_enabled=True, neutron_sg_enabled=True): # Neutron does not have an API for getting default system # quotas. When not using Neutron, the floating ips quotas # should be in the list. + self.mox.StubOutWithMock(api.nova, 'default_quota_get') + self.mox.StubOutWithMock(api.cinder, 'default_quota_get') + self.mox.StubOutWithMock(api.base, 'is_service_enabled') + if neutron_enabled: + self.mox.StubOutWithMock(api.neutron, 'is_extension_supported') + api.base.is_service_enabled(IsA(http.HttpRequest), 'volume') \ .AndReturn(True) api.base.is_service_enabled(IsA(http.HttpRequest), 'network') \ - .MultipleTimes().AndReturn(False) + .MultipleTimes().AndReturn(neutron_enabled) api.nova.default_quota_get(IsA(http.HttpRequest), self.tenant.id).AndReturn(self.quotas.nova) api.cinder.default_quota_get(IsA(http.HttpRequest), self.tenant.id) \ .AndReturn(self.cinder_quotas.first()) + if neutron_enabled: + api.neutron.is_extension_supported( + IsA(http.HttpRequest), + 'security-group').AndReturn(neutron_sg_enabled) self.mox.ReplayAll() @@ -79,20 +67,28 @@ class ServicesViewTests(test.BaseAdminViewTests): self.assertTemplateUsed(res, 'admin/defaults/index.html') quotas_tab = res.context['tab_group'].get_tab('quotas') + expected_tabs = ['', + '', + '', + '', + '', + '', + '', + '', + '', + '', + '', + '', + ''] + if neutron_enabled: + expected_tabs.remove('') + expected_tabs.remove('') + if neutron_sg_enabled: + expected_tabs.remove('') + expected_tabs.remove('') + self.assertQuerysetEqual(quotas_tab._tables['quotas'].data, - ['', - '', - '', - '', - '', - '', - '', - '', - '', - '', - '', - '', - ''], + expected_tabs, ordered=False) diff --git a/openstack_dashboard/dashboards/admin/overview/tests.py b/openstack_dashboard/dashboards/admin/overview/tests.py index c26f11dfe5..bdf3c80837 100644 --- a/openstack_dashboard/dashboards/admin/overview/tests.py +++ b/openstack_dashboard/dashboards/admin/overview/tests.py @@ -40,7 +40,9 @@ class UsageViewTests(test.BaseAdminViewTests): @test.create_stubs({api.nova: ('usage_list', 'tenant_absolute_limits', ), api.keystone: ('tenant_list',), - api.network: ('tenant_floating_ip_list',)}) + api.neutron: ('is_extension_supported',), + api.network: ('tenant_floating_ip_list', + 'security_group_list')}) def test_usage(self): now = timezone.now() usage_obj = api.nova.NovaUsage(self.usages.first()) @@ -56,8 +58,12 @@ class UsageViewTests(test.BaseAdminViewTests): .AndReturn([usage_obj]) api.nova.tenant_absolute_limits(IsA(http.HttpRequest)) \ .AndReturn(self.limits['absolute']) + api.neutron.is_extension_supported(IsA(http.HttpRequest), + 'security-group').AndReturn(True) api.network.tenant_floating_ip_list(IsA(http.HttpRequest)) \ .AndReturn(self.floating_ips.list()) + api.network.security_group_list(IsA(http.HttpRequest)) \ + .AndReturn(self.q_secgroups.list()) self.mox.ReplayAll() res = self.client.get(reverse('horizon:admin:overview:index')) @@ -79,7 +85,9 @@ class UsageViewTests(test.BaseAdminViewTests): @test.create_stubs({api.nova: ('usage_list', 'tenant_absolute_limits', ), api.keystone: ('tenant_list',), - api.network: ('tenant_floating_ip_list',)}) + api.neutron: ('is_extension_supported',), + api.network: ('tenant_floating_ip_list', + 'security_group_list')}) def test_usage_csv(self): now = timezone.now() usage_obj = [api.nova.NovaUsage(u) for u in self.usages.list()] @@ -95,8 +103,12 @@ class UsageViewTests(test.BaseAdminViewTests): .AndReturn(usage_obj) api.nova.tenant_absolute_limits(IsA(http.HttpRequest))\ .AndReturn(self.limits['absolute']) + api.neutron.is_extension_supported(IsA(http.HttpRequest), + 'security-group').AndReturn(True) api.network.tenant_floating_ip_list(IsA(http.HttpRequest)) \ .AndReturn(self.floating_ips.list()) + api.network.security_group_list(IsA(http.HttpRequest)) \ + .AndReturn(self.q_secgroups.list()) self.mox.ReplayAll() csv_url = reverse('horizon:admin:overview:index') + "?format=csv" diff --git a/openstack_dashboard/dashboards/project/overview/tests.py b/openstack_dashboard/dashboards/project/overview/tests.py index 79f50a2821..8fbfa84cb6 100644 --- a/openstack_dashboard/dashboards/project/overview/tests.py +++ b/openstack_dashboard/dashboards/project/overview/tests.py @@ -36,12 +36,25 @@ INDEX_URL = reverse('horizon:project:overview:index') class UsageViewTests(test.TestCase): + def _stub_neutron_api_calls(self, neutron_sg_enabled=True): + self.mox.StubOutWithMock(api.neutron, 'is_extension_supported') + self.mox.StubOutWithMock(api.network, 'tenant_floating_ip_list') + if neutron_sg_enabled: + self.mox.StubOutWithMock(api.network, 'security_group_list') + api.neutron.is_extension_supported( + IsA(http.HttpRequest), + 'security-group').AndReturn(neutron_sg_enabled) + api.network.tenant_floating_ip_list(IsA(http.HttpRequest)) \ + .AndReturn(self.floating_ips.list()) + if neutron_sg_enabled: + api.network.security_group_list(IsA(http.HttpRequest)) \ + .AndReturn(self.q_secgroups.list()) + def test_usage(self): now = timezone.now() usage_obj = api.nova.NovaUsage(self.usages.first()) self.mox.StubOutWithMock(api.nova, 'usage_get') self.mox.StubOutWithMock(api.nova, 'tenant_absolute_limits') - self.mox.StubOutWithMock(api.network, 'tenant_floating_ip_list') api.nova.usage_get(IsA(http.HttpRequest), self.tenant.id, datetime.datetime(now.year, now.month, @@ -52,8 +65,7 @@ class UsageViewTests(test.TestCase): .AndReturn(usage_obj) api.nova.tenant_absolute_limits(IsA(http.HttpRequest)) \ .AndReturn(self.limits['absolute']) - api.network.tenant_floating_ip_list(IsA(http.HttpRequest)) \ - .AndReturn(self.floating_ips.list()) + self._stub_neutron_api_calls() self.mox.ReplayAll() res = self.client.get(reverse('horizon:project:overview:index')) @@ -95,7 +107,6 @@ class UsageViewTests(test.TestCase): now = timezone.now() self.mox.StubOutWithMock(api.nova, 'usage_get') self.mox.StubOutWithMock(api.nova, 'tenant_absolute_limits') - self.mox.StubOutWithMock(api.network, 'tenant_floating_ip_list') api.nova.usage_get(IsA(http.HttpRequest), self.tenant.id, datetime.datetime(now.year, now.month, @@ -106,8 +117,7 @@ class UsageViewTests(test.TestCase): .AndRaise(exc) api.nova.tenant_absolute_limits(IsA(http.HttpRequest))\ .AndReturn(self.limits['absolute']) - api.network.tenant_floating_ip_list(IsA(http.HttpRequest)) \ - .AndReturn(self.floating_ips.list()) + self._stub_neutron_api_calls() self.mox.ReplayAll() url = reverse('horizon:project:overview:index') @@ -121,7 +131,6 @@ class UsageViewTests(test.TestCase): usage_obj = api.nova.NovaUsage(self.usages.first()) self.mox.StubOutWithMock(api.nova, 'usage_get') self.mox.StubOutWithMock(api.nova, 'tenant_absolute_limits') - self.mox.StubOutWithMock(api.network, 'tenant_floating_ip_list') start = datetime.datetime(now.year, now.month, now.day, 0, 0, 0, 0) end = datetime.datetime(now.year, now.month, now.day, 23, 59, 59, 0) api.nova.usage_get(IsA(http.HttpRequest), @@ -129,8 +138,7 @@ class UsageViewTests(test.TestCase): start, end).AndReturn(usage_obj) api.nova.tenant_absolute_limits(IsA(http.HttpRequest))\ .AndReturn(self.limits['absolute']) - api.network.tenant_floating_ip_list(IsA(http.HttpRequest)) \ - .AndReturn(self.floating_ips.list()) + self._stub_neutron_api_calls() self.mox.ReplayAll() res = self.client.get(reverse('horizon:project:overview:index') + "?format=csv") @@ -141,7 +149,6 @@ class UsageViewTests(test.TestCase): now = timezone.now() self.mox.StubOutWithMock(api.nova, 'usage_get') self.mox.StubOutWithMock(api.nova, 'tenant_absolute_limits') - self.mox.StubOutWithMock(api.network, 'tenant_floating_ip_list') start = datetime.datetime(now.year, now.month, now.day, 0, 0, 0, 0) end = datetime.datetime(now.year, now.month, now.day, 23, 59, 59, 0) api.nova.usage_get(IsA(http.HttpRequest), @@ -149,8 +156,7 @@ class UsageViewTests(test.TestCase): start, end).AndRaise(self.exceptions.nova) api.nova.tenant_absolute_limits(IsA(http.HttpRequest))\ .AndReturn(self.limits['absolute']) - api.network.tenant_floating_ip_list(IsA(http.HttpRequest)) \ - .AndReturn(self.floating_ips.list()) + self._stub_neutron_api_calls() self.mox.ReplayAll() res = self.client.get(reverse('horizon:project:overview:index')) @@ -162,7 +168,6 @@ class UsageViewTests(test.TestCase): usage_obj = api.nova.NovaUsage(self.usages.first()) self.mox.StubOutWithMock(api.nova, 'usage_get') self.mox.StubOutWithMock(api.nova, 'tenant_absolute_limits') - self.mox.StubOutWithMock(api.network, 'tenant_floating_ip_list') start = datetime.datetime(now.year, now.month, now.day, 0, 0, 0, 0) end = datetime.datetime(now.year, now.month, now.day, 23, 59, 59, 0) api.nova.usage_get(IsA(http.HttpRequest), @@ -170,8 +175,7 @@ class UsageViewTests(test.TestCase): start, end).AndReturn(usage_obj) api.nova.tenant_absolute_limits(IsA(http.HttpRequest))\ .AndRaise(self.exceptions.nova) - api.network.tenant_floating_ip_list(IsA(http.HttpRequest)) \ - .AndReturn(self.floating_ips.list()) + self._stub_neutron_api_calls() self.mox.ReplayAll() res = self.client.get(reverse('horizon:project:overview:index')) @@ -183,7 +187,6 @@ class UsageViewTests(test.TestCase): usage_obj = api.nova.NovaUsage(self.usages.first()) self.mox.StubOutWithMock(api.nova, 'usage_get') self.mox.StubOutWithMock(api.nova, 'tenant_absolute_limits') - self.mox.StubOutWithMock(api.network, 'tenant_floating_ip_list') start = datetime.datetime(now.year, now.month, now.day, 0, 0, 0, 0) end = datetime.datetime(now.year, now.month, now.day, 23, 59, 59, 0) api.nova.usage_get(IsA(http.HttpRequest), @@ -191,8 +194,7 @@ class UsageViewTests(test.TestCase): start, end).AndReturn(usage_obj) api.nova.tenant_absolute_limits(IsA(http.HttpRequest))\ .AndReturn(self.limits['absolute']) - api.network.tenant_floating_ip_list(IsA(http.HttpRequest)) \ - .AndReturn(self.floating_ips.list()) + self._stub_neutron_api_calls() self.mox.ReplayAll() res = self.client.get(reverse('horizon:project:overview:index')) @@ -200,14 +202,19 @@ class UsageViewTests(test.TestCase): self.assertTrue(isinstance(res.context['usage'], usage.ProjectUsage)) @override_settings(OPENSTACK_NEUTRON_NETWORK={'enable_quotas': True}) - def test_usage_with_neutron_floating_ips(self): + def test_usage_with_neutron(self): + self._test_usage_with_neutron(neutron_sg_enabled=True) + + @override_settings(OPENSTACK_NEUTRON_NETWORK={'enable_quotas': True}) + def test_usage_with_neutron_nova_security_group(self): + self._test_usage_with_neutron(neutron_sg_enabled=False) + + def _test_usage_with_neutron(self, neutron_sg_enabled=True): now = timezone.now() usage_obj = api.nova.NovaUsage(self.usages.first()) self.mox.StubOutWithMock(api.nova, 'usage_get') self.mox.StubOutWithMock(api.nova, 'tenant_absolute_limits') - self.mox.StubOutWithMock(api.neutron, 'is_extension_supported') self.mox.StubOutWithMock(api.neutron, 'tenant_quota_get') - self.mox.StubOutWithMock(api.network, 'tenant_floating_ip_list') start = datetime.datetime(now.year, now.month, now.day, 0, 0, 0, 0) end = datetime.datetime(now.year, now.month, now.day, 23, 59, 59, 0) api.nova.usage_get(IsA(http.HttpRequest), @@ -215,17 +222,24 @@ class UsageViewTests(test.TestCase): start, end).AndReturn(usage_obj) api.nova.tenant_absolute_limits(IsA(http.HttpRequest))\ .AndReturn(self.limits['absolute']) + self._stub_neutron_api_calls(neutron_sg_enabled) + # NOTE: api.neutron.is_extension_supported is stubbed out in + # _stub_neutron_api_calls. api.neutron.is_extension_supported(IsA(http.HttpRequest), 'quotas') \ .AndReturn(True) api.neutron.tenant_quota_get(IsA(http.HttpRequest), self.tenant.id) \ .AndReturn(self.neutron_quotas.first()) - api.network.tenant_floating_ip_list(IsA(http.HttpRequest)) \ - .AndReturn(self.floating_ips.list()) self.mox.ReplayAll() res = self.client.get(reverse('horizon:project:overview:index')) self.assertContains(res, 'Floating IPs') + self.assertContains(res, 'Security Groups') - # Make sure the floating IPs limit comes from Neutron (50 vs. 10) - max_floating_ips = res.context['usage'].limits['maxTotalFloatingIps'] + res_limits = res.context['usage'].limits + # Make sure the floating IPs comes from Neutron (50 vs. 10) + max_floating_ips = res_limits['maxTotalFloatingIps'] self.assertEqual(max_floating_ips, 50) + if neutron_sg_enabled: + # Make sure the security group limit comes from Neutron (20 vs. 10) + max_security_groups = res_limits['maxSecurityGroups'] + self.assertEqual(max_security_groups, 20) diff --git a/openstack_dashboard/test/test_data/neutron_data.py b/openstack_dashboard/test/test_data/neutron_data.py index de40fc398a..652f58c2ba 100644 --- a/openstack_dashboard/test/test_data/neutron_data.py +++ b/openstack_dashboard/test/test_data/neutron_data.py @@ -519,13 +519,14 @@ def data(TEST): #------------------------------------------------------------ # Quotas - quota_data = dict(floatingip='50', - network='10', - port='50', - router='10', - security_groups='10', - security_group_rules='100', - subnet='10') + quota_data = {'network': '10', + 'subnet': '10', + 'port': '50', + 'router': '10', + 'floatingip': '50', + 'security_group': '20', + 'security_group_rule': '100', + } TEST.neutron_quotas.add(base.QuotaSet(quota_data)) #------------------------------------------------------------ diff --git a/openstack_dashboard/usage/base.py b/openstack_dashboard/usage/base.py index c18c3b3665..1de3b7f36c 100644 --- a/openstack_dashboard/usage/base.py +++ b/openstack_dashboard/usage/base.py @@ -103,40 +103,74 @@ class BaseUsage(object): 'end': init[1]}) return self.form + def _get_neutron_usage(self, limits, resource_name): + resource_map = { + 'floatingip': { + 'api': api.network.tenant_floating_ip_list, + 'limit_name': 'totalFloatingIpsUsed', + 'message': _('Unable to retrieve floating IP addresses.') + }, + 'security_group': { + 'api': api.network.security_group_list, + 'limit_name': 'totalSecurityGroupsUsed', + 'message': _('Unable to retrieve security gruops.') + } + } + + resource = resource_map[resource_name] + try: + method = resource['api'] + current_used = len(method(self.request)) + except Exception: + current_used = 0 + msg = resource['message'] + exceptions.handle(self.request, msg) + + limits[resource['limit_name']] = current_used + + def _set_neutron_limit(self, limits, neutron_quotas, resource_name): + limit_name_map = { + 'floatingip': 'maxTotalFloatingIps', + 'security_group': 'maxSecurityGroups', + } + if neutron_quotas is None: + resource_max = float("inf") + else: + resource_max = getattr(neutron_quotas.get(resource_name), + 'limit', float("inf")) + if resource_max == -1: + resource_max = float("inf") + + limits[limit_name_map[resource_name]] = resource_max + def get_neutron_limits(self): if not api.base.is_service_enabled(self.request, 'network'): return - # Retrieve number of floating IPs currently allocated - try: - floating_ips_current = len( - api.network.tenant_floating_ip_list(self.request)) - except Exception: - floating_ips_current = 0 - msg = _('Unable to retrieve floating IP addresses.') - exceptions.handle(self.request, msg) + neutron_sg_used = \ + api.neutron.is_security_group_extension_supported(self.request) - self.limits['totalFloatingIpsUsed'] = floating_ips_current + self._get_neutron_usage(self.limits, 'floatingip') + if neutron_sg_used: + self._get_neutron_usage(self.limits, 'security_group') # Quotas are an optional extension in Neutron. If it isn't # enabled, assume the floating IP limit is infinite. - if not api.neutron.is_quotas_extension_supported(self.request): - self.limits['maxTotalFloatingIps'] = float("inf") - return + if api.neutron.is_quotas_extension_supported(self.request): + try: + neutron_quotas = api.neutron.tenant_quota_get(self.request, + self.project_id) + except Exception: + neutron_quotas = None + msg = _('Unable to retrieve network quota information.') + exceptions.handle(self.request, msg) + else: + neutron_quotas = None - try: - neutron_quotas = api.neutron.tenant_quota_get(self.request, - self.project_id) - floating_ips_max = getattr(neutron_quotas.get('floatingip'), - 'limit', float("inf")) - if floating_ips_max == -1: - floating_ips_max = float("inf") - except Exception: - floating_ips_max = float("inf") - msg = _('Unable to retrieve network quota information.') - exceptions.handle(self.request, msg) - - self.limits['maxTotalFloatingIps'] = floating_ips_max + self._set_neutron_limit(self.limits, neutron_quotas, 'floatingip') + if neutron_sg_used: + self._set_neutron_limit(self.limits, neutron_quotas, + 'security_group') def get_limits(self): try: diff --git a/openstack_dashboard/usage/quotas.py b/openstack_dashboard/usage/quotas.py index 2b7b319084..289b5b7df8 100644 --- a/openstack_dashboard/usage/quotas.py +++ b/openstack_dashboard/usage/quotas.py @@ -106,10 +106,23 @@ def _get_quota_data(request, method_name, disabled_quotas=None, def get_default_quota_data(request, disabled_quotas=None, tenant_id=None): - return _get_quota_data(request, - "default_quota_get", - disabled_quotas=disabled_quotas, - tenant_id=tenant_id) + qs = _get_quota_data(request, + "default_quota_get", + disabled_quotas=disabled_quotas, + tenant_id=tenant_id) + + # Remove quotas information for resources provided by Neutron. + # TODO(amotoki): There is no API to get the default system quotas + # in Neutron (cf. LP#1204956), so we need to remove such quotas + # information from quotas set. + # This should be handled in _get_quota_data() eventually. + if base.is_service_enabled(request, 'network'): + if neutron.is_security_group_extension_supported(request): + sg_fields = ['security_groups', 'security_group_rules'] + qs = [quota for quota in qs + if quota.name not in sg_fields] + + return qs def get_tenant_quota_data(request, disabled_quotas=None, tenant_id=None):