Browse files

Merge "Avoid cinder calls, when cinder is unavailable"

  • Loading branch information...
2 parents 9cdb026 + 24367e6 commit 4b0f66f00422d7af71161ed77b16113dec1f5fde Jenkins committed with openstack-gerrit Dec 17, 2012
View
24 openstack_dashboard/api/cinder.py
@@ -31,7 +31,7 @@
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 @@
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
6 openstack_dashboard/dashboards/admin/info/tabs.py
@@ -22,6 +22,7 @@
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
14 openstack_dashboard/dashboards/project/images_and_snapshots/views.py
@@ -32,6 +32,7 @@
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 @@ 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
View
56 openstack_dashboard/test/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 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
10 openstack_dashboard/test/helpers.py
@@ -34,6 +34,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
import httplib2
import mox
@@ -244,26 +245,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.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()
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.cinder.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')

0 comments on commit 4b0f66f

Please sign in to comment.