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.

Change-Id: Ic5d029302d10b453257b452ba78541febf8e4c96
Fixes bug 1084137
This commit is contained in:
Matthias Runge 2012-12-10 13:49:38 +01:00
parent c4746aacfa
commit 24367e6cea
5 changed files with 98 additions and 12 deletions

View File

@ -31,7 +31,7 @@ from cinderclient.v1 import client as cinder_client
from openstack_dashboard.api.base import url_for
from openstack_dashboard.api import nova, QuotaSet
from horizon import exceptions
LOG = logging.getLogger(__name__)
@ -42,15 +42,21 @@ VOLUME_STATE_AVAILABLE = "available"
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
@ -93,7 +99,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):
@ -106,7 +115,10 @@ def volume_snapshot_delete(request, snapshot_id):
def tenant_quota_get(request, tenant_id):
return QuotaSet(cinderclient(request).quotas.get(tenant_id))
c_client = cinderclient(request)
if c_client is None:
return QuotaSet()
return QuotaSet(c_client.quotas.get(tenant_id))
def tenant_quota_update(request, tenant_id, **kwargs):

View File

@ -22,6 +22,7 @@ from horizon import tabs
from openstack_dashboard.api import keystone
from openstack_dashboard.usage import quotas
from .tables import QuotasTable, ServicesTable
from openstack_dashboard.api.base import is_service_enabled
class DefaultQuotasTab(tabs.TableTab):
@ -32,8 +33,11 @@ class DefaultQuotasTab(tabs.TableTab):
def get_quotas_data(self):
request = self.tab_group.request
disabled_quotas = []
if not is_service_enabled(self.request, 'volume'):
disabled_quotas.extend(['volumes', 'gigabytes'])
try:
quota_set = quotas.get_default_quota_data(request)
quota_set = quotas.get_default_quota_data(request, disabled_quotas)
data = quota_set.items
except:
data = []

View File

@ -32,6 +32,7 @@ from horizon import tables
from horizon import tabs
from openstack_dashboard import api
from openstack_dashboard.api.base import is_service_enabled
from .images.tables import ImagesTable
from .snapshots.tables import SnapshotsTable
from .volume_snapshots.tables import VolumeSnapshotsTable
@ -75,12 +76,15 @@ class IndexView(tables.MultiTableView):
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

View File

@ -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 openstack_dashboard import api
from openstack_dashboard.test import helpers as 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.cinder.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.cinder.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.cinder.volume_snapshot_list(self.request)

View File

@ -34,6 +34,7 @@ from keystoneclient.v2_0 import client as keystone_client
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
import httplib2
import mox
@ -244,12 +245,14 @@ class APITestCase(TestCase):
self._original_keystoneclient = api.keystone.keystoneclient
self._original_novaclient = api.nova.novaclient
self._original_quantumclient = api.quantum.quantumclient
self._original_cinderclient = api.cinder.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.cinder.cinderclient = lambda request: self.stub_cinderclient()
def tearDown(self):
super(APITestCase, self).tearDown()
@ -257,6 +260,7 @@ class APITestCase(TestCase):
api.nova.novaclient = self._original_novaclient
api.keystone.keystoneclient = self._original_keystoneclient
api.quantum.quantumclient = self._original_quantumclient
api.cinder.cinderclient = self._original_cinderclient
def stub_novaclient(self):
if not hasattr(self, "novaclient"):
@ -264,6 +268,12 @@ class APITestCase(TestCase):
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')