summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatt Riedemann <mriedem.os@gmail.com>2017-05-31 11:54:48 -0400
committerMatt Riedemann <mriedem.os@gmail.com>2017-05-31 11:57:05 -0400
commite90503cba2db8aea03164f0cbaabd1c49bcc4a46 (patch)
treecb90f6a00722e15de29c02b4a7a6d4b4cffd1cbe
parentb9a968f96dc0ac2a819d14149c57e6e4fad557fa (diff)
downloadpython-cinderclient-e90503cba2db8aea03164f0cbaabd1c49bcc4a46.tar.gz
Handle AttributeError in _get_server_version_range
_get_server_version_range only supports a v3 client object because ServiceManager.server_api_version() is only implemented for the v3 client code. If you're trying to call this with a v1 or v2 client object, it will raise an AttributeError which is unhelpful for the end python API client. This change handles the AttributeError and returns a more useful error with an explanation of how the client code is wrong. We could alternatively set versions = None and let it return two empty APIVersion objects, but that seems confusing. This change opts to be explicit about the failure. Change-Id: I2266999df6332b8c5fb0ec808db69bcc847effb0 Closes-Bug: #1694739
-rw-r--r--cinderclient/api_versions.py9
-rw-r--r--cinderclient/tests/unit/test_api_versions.py9
2 files changed, 17 insertions, 1 deletions
diff --git a/cinderclient/api_versions.py b/cinderclient/api_versions.py
index d1bb1e1..5ba78a2 100644
--- a/cinderclient/api_versions.py
+++ b/cinderclient/api_versions.py
@@ -235,7 +235,14 @@ def get_api_version(version_string):
def _get_server_version_range(client):
- versions = client.services.server_api_version()
+ try:
+ versions = client.services.server_api_version()
+ except AttributeError:
+ # Wrong client was used, translate to something helpful.
+ raise exceptions.UnsupportedVersion(
+ _('Invalid client version %s to get server version range. Only '
+ 'the v3 client is supported for this operation.') %
+ client.version)
if not versions:
return APIVersion(), APIVersion()
diff --git a/cinderclient/tests/unit/test_api_versions.py b/cinderclient/tests/unit/test_api_versions.py
index ef8f311..5f14c3e 100644
--- a/cinderclient/tests/unit/test_api_versions.py
+++ b/cinderclient/tests/unit/test_api_versions.py
@@ -15,8 +15,10 @@
import ddt
import mock
+import six
from cinderclient import api_versions
+from cinderclient import client as base_client
from cinderclient import exceptions
from cinderclient.v3 import client
from cinderclient.tests.unit import utils
@@ -259,3 +261,10 @@ class DiscoverVersionTestCase(utils.TestCase):
highest_version = api_versions.get_highest_version(self.fake_client)
self.assertEqual("3.14", highest_version.get_string())
self.assertTrue(self.fake_client.services.server_api_version.called)
+
+ def test_get_highest_version_bad_client(self):
+ """Tests that we gracefully handle the wrong version of client."""
+ v2_client = base_client.Client('2.0')
+ ex = self.assertRaises(exceptions.UnsupportedVersion,
+ api_versions.get_highest_version, v2_client)
+ self.assertIn('Invalid client version 2.0 to get', six.text_type(ex))