From 476072d49a41cd8461aa302591f828627760b67e Mon Sep 17 00:00:00 2001 From: Matthias Runge Date: Tue, 18 Dec 2012 14:00:48 +0100 Subject: [PATCH] Avoid cinder calls, when cinder is unavailable When a volume service is not available, then a cinder client can not be created. This patch skips the calls, so that dashboard doesn't break any more. This is a backport to Folsom release, as this turned out to be a major issue. Fixes bug 1084137 Change-Id: I8f2f8b0b131b4bb5319d74f6da48671f146d7e00 --- horizon/api/base.py | 9 +++ horizon/api/nova.py | 22 ++++++-- .../nova/images_and_snapshots/views.py | 14 +++-- horizon/test.py | 10 ++++ horizon/tests/api_tests/cinder_tests.py | 56 +++++++++++++++++++ 5 files changed, 101 insertions(+), 10 deletions(-) create mode 100644 horizon/tests/api_tests/cinder_tests.py diff --git a/horizon/api/base.py b/horizon/api/base.py index 15f9f9eeb47..929221cbcbe 100644 --- a/horizon/api/base.py +++ b/horizon/api/base.py @@ -116,3 +116,12 @@ def url_for(request, service_type, admin=False, endpoint_type=None): raise exceptions.ServiceCatalogException(service_type) else: raise exceptions.ServiceCatalogException(service_type) + + +def is_service_enabled(request, service_type, service_name=None): + service = get_service_from_catalog(request.user.service_catalog, + service_type) + if service and service_name: + return service['name'] == service_name + else: + return service is not None diff --git a/horizon/api/nova.py b/horizon/api/nova.py index 3bf209f2e9b..6861f5a58e3 100644 --- a/horizon/api/nova.py +++ b/horizon/api/nova.py @@ -210,15 +210,21 @@ def novaclient(request): def cinderclient(request): insecure = getattr(settings, 'OPENSTACK_SSL_NO_VERIFY', False) + cinder_url = "" + try: + cinder_url = url_for(request, 'volume') + except exceptions.ServiceCatalogException: + LOG.debug('no volume service configured.') + return None LOG.debug('cinderclient connection created using token "%s" and url "%s"' % - (request.user.token.id, url_for(request, 'volume'))) + (request.user.token.id, cinder_url)) c = cinder_client.Client(request.user.username, request.user.token.id, project_id=request.user.tenant_id, - auth_url=url_for(request, 'volume'), + auth_url=cinder_url, insecure=insecure) c.client.auth_token = request.user.token.id - c.client.management_url = url_for(request, 'volume') + c.client.management_url = cinder_url return c @@ -513,7 +519,10 @@ def volume_list(request, search_opts=None): To see all volumes in the cloud as an admin you can pass in a special search option: {'all_tenants': 1} """ - return cinderclient(request).volumes.list(search_opts=search_opts) + c_client = cinderclient(request) + if c_client is None: + return [] + return c_client.volumes.list(search_opts=search_opts) def volume_get(request, volume_id): @@ -565,7 +574,10 @@ def volume_snapshot_get(request, snapshot_id): def volume_snapshot_list(request): - return cinderclient(request).volume_snapshots.list() + c_client = cinderclient(request) + if c_client is None: + return [] + return c_client.volume_snapshots.list() def volume_snapshot_create(request, volume_id, name, description): diff --git a/horizon/dashboards/nova/images_and_snapshots/views.py b/horizon/dashboards/nova/images_and_snapshots/views.py index bade47e4e07..b382265a185 100644 --- a/horizon/dashboards/nova/images_and_snapshots/views.py +++ b/horizon/dashboards/nova/images_and_snapshots/views.py @@ -28,6 +28,7 @@ from django.utils.translation import ugettext_lazy as _ from horizon import api +from horizon.api.base import is_service_enabled from horizon import exceptions from horizon import tables from horizon import tabs @@ -74,12 +75,15 @@ def get_snapshots_data(self): return snaps def get_volume_snapshots_data(self): - try: - snapshots = api.volume_snapshot_list(self.request) - except: + if is_service_enabled(self.request, 'volume'): + try: + snapshots = api.volume_snapshot_list(self.request) + except: + snapshots = [] + exceptions.handle(self.request, _("Unable to retrieve " + "volume snapshots.")) + else: snapshots = [] - exceptions.handle(self.request, _("Unable to retrieve " - "volume snapshots.")) return snapshots diff --git a/horizon/test.py b/horizon/test.py index f2977b1f8bf..d6cfa518830 100644 --- a/horizon/test.py +++ b/horizon/test.py @@ -36,6 +36,7 @@ from novaclient.v1_1 import client as nova_client from quantumclient.v2_0 import client as quantum_client from swiftclient import client as swift_client +from cinderclient import client as cinder_client from selenium.webdriver.firefox.webdriver import WebDriver @@ -293,12 +294,14 @@ def fake_keystoneclient(request, admin=False): self._original_keystoneclient = api.keystone.keystoneclient self._original_novaclient = api.nova.novaclient self._original_quantumclient = api.quantum.quantumclient + self._original_cinderclient = api.nova.cinderclient # Replace the clients with our stubs. api.glance.glanceclient = lambda request: self.stub_glanceclient() api.keystone.keystoneclient = fake_keystoneclient api.nova.novaclient = lambda request: self.stub_novaclient() api.quantum.quantumclient = lambda request: self.stub_quantumclient() + api.nova.cinderclient = lambda request: self.stub_cinderclient() def tearDown(self): super(APITestCase, self).tearDown() @@ -306,6 +309,7 @@ def tearDown(self): api.nova.novaclient = self._original_novaclient api.keystone.keystoneclient = self._original_keystoneclient api.quantum.quantumclient = self._original_quantumclient + api.nova.cinderclient = self._original_cinderclient def stub_novaclient(self): if not hasattr(self, "novaclient"): @@ -313,6 +317,12 @@ def stub_novaclient(self): self.novaclient = self.mox.CreateMock(nova_client.Client) return self.novaclient + def stub_cinderclient(self): + if not hasattr(self, "cinderclient"): + self.mox.StubOutWithMock(cinder_client, 'Client') + self.cinderclient = self.mox.CreateMock(cinder_client.Client) + return self.cinderclient + def stub_keystoneclient(self): if not hasattr(self, "keystoneclient"): self.mox.StubOutWithMock(keystone_client, 'Client') diff --git a/horizon/tests/api_tests/cinder_tests.py b/horizon/tests/api_tests/cinder_tests.py new file mode 100644 index 00000000000..ab3ce7547e1 --- /dev/null +++ b/horizon/tests/api_tests/cinder_tests.py @@ -0,0 +1,56 @@ +# vim: tabstop=4 shiftwidth=4 softtabstop=4 + +# Copyright 2012 Red Hat, Inc. +# +# 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 horizon import api +from horizon import test + + +class CinderApiTests(test.APITestCase): + def test_volume_list(self): + search_opts = {'all_tenants': 1} + volumes = self.volumes.list() + cinderclient = self.stub_cinderclient() + cinderclient.volumes = self.mox.CreateMockAnything() + cinderclient.volumes.list(search_opts=search_opts,).AndReturn(volumes) + self.mox.ReplayAll() + + # No assertions are necessary. Verification is handled by mox. + api.nova.volume_list(self.request, search_opts=search_opts) + + def test_volume_snapshot_list(self): + volume_snapshots = self.volume_snapshots.list() + cinderclient = self.stub_cinderclient() + cinderclient.volume_snapshots = self.mox.CreateMockAnything() + cinderclient.volume_snapshots.list().AndReturn(volume_snapshots) + self.mox.ReplayAll() + + api.nova.volume_snapshot_list(self.request) + + def test_volume_snapshot_list_no_volume_configured(self): + # remove volume from service catalog + catalog = self.service_catalog + for service in catalog: + if service["type"] == "volume": + self.service_catalog.remove(service) + volume_snapshots = self.volume_snapshots.list() + + cinderclient = self.stub_cinderclient() + cinderclient.volume_snapshots = self.mox.CreateMockAnything() + cinderclient.volume_snapshots.list().AndReturn(volume_snapshots) + self.mox.ReplayAll() + + api.nova.volume_snapshot_list(self.request)