Skip to content

Commit

Permalink
Avoid cinder calls, when cinder is unavailable
Browse files Browse the repository at this point in the history
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
  • Loading branch information
mrunge committed Jan 10, 2013
1 parent 1d1fa85 commit 476072d
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 10 deletions.
9 changes: 9 additions & 0 deletions horizon/api/base.py
Expand Up @@ -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
22 changes: 17 additions & 5 deletions horizon/api/nova.py
Expand Up @@ -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


Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down
14 changes: 9 additions & 5 deletions horizon/dashboards/nova/images_and_snapshots/views.py
Expand Up @@ -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
Expand Down Expand Up @@ -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


Expand Down
10 changes: 10 additions & 0 deletions horizon/test.py
Expand Up @@ -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

Expand Down Expand Up @@ -293,26 +294,35 @@ 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()
api.glance.glanceclient = self._original_glanceclient
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"):
self.mox.StubOutWithMock(nova_client, 'Client')
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')
Expand Down
56 changes: 56 additions & 0 deletions 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)

0 comments on commit 476072d

Please sign in to comment.