summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorscottda <scott.dangelo@ibm.com>2017-01-13 11:10:40 -0700
committerscottda <scott.dangelo@ibm.com>2017-01-26 13:18:21 -0700
commit4f22510ccf0628554de1b566e007a7af750d99cf (patch)
tree6396cb02554840dcaef550501970e43e2b793eb6
parent82181f0bf5c6abe57aedfd54d9a28e2a41ccd37a (diff)
downloadpython-cinderclient-4f22510ccf0628554de1b566e007a7af750d99cf.tar.gz
static method to get_server_version
This is a static method that takes a url for the cinder endpoint and returns the server's min and max api version as APIVersion objects. Change-Id: I33fa9d0883ad7377c480c9e230412dfa487ccbc9
-rw-r--r--cinderclient/client.py25
-rw-r--r--cinderclient/tests/unit/test_client.py28
-rw-r--r--cinderclient/tests/unit/v3/fakes.py40
3 files changed, 93 insertions, 0 deletions
diff --git a/cinderclient/client.py b/cinderclient/client.py
index a4554a2..b5bf49c 100644
--- a/cinderclient/client.py
+++ b/cinderclient/client.py
@@ -40,6 +40,7 @@ from cinderclient import api_versions
from cinderclient import exceptions
import cinderclient.extension
from cinderclient._i18n import _
+from cinderclient._i18n import _LW
from oslo_utils import encodeutils
from oslo_utils import importutils
from oslo_utils import strutils
@@ -72,6 +73,30 @@ for svc in ('volume', 'volumev2', 'volumev3'):
discover.add_catalog_discover_hack(svc, re.compile('/v[12]/\w+/?$'), '/')
+def get_server_version(url):
+ """Queries the server via the naked endpoint and gets version info.
+
+ :param url: url of the cinder endpoint
+ :returns: APIVersion object for min and max version supported by
+ the server
+ """
+
+ logger = logging.getLogger(__name__)
+ try:
+ scheme, netloc, path, query, frag = urlparse.urlsplit(url)
+ response = requests.get(scheme + '://' + netloc)
+ data = json.loads(response.text)
+ versions = data['versions']
+ for version in versions:
+ if '3.' in version['version']:
+ return (api_versions.APIVersion(version['min_version']),
+ api_versions.APIVersion(version['version']))
+ except exceptions.ClientException as e:
+ logger.warning(_LW("Error in server version query:%s\n"
+ "Returning APIVersion 2.0") % six.text_type(e.message))
+ return api_versions.APIVersion("2.0"), api_versions.APIVersion("2.0")
+
+
def get_volume_api_from_url(url):
scheme, netloc, path, query, frag = urlparse.urlsplit(url)
components = path.split("/")
diff --git a/cinderclient/tests/unit/test_client.py b/cinderclient/tests/unit/test_client.py
index e13a772..a381601 100644
--- a/cinderclient/tests/unit/test_client.py
+++ b/cinderclient/tests/unit/test_client.py
@@ -24,8 +24,11 @@ import six
import cinderclient.client
import cinderclient.v1.client
import cinderclient.v2.client
+from cinderclient import api_versions
from cinderclient import exceptions
+from cinderclient import utils
from cinderclient.tests.unit import utils
+from cinderclient.tests.unit.v3 import fakes
class ClientTest(utils.TestCase):
@@ -313,3 +316,28 @@ class ClientTestSensitiveInfo(utils.TestCase):
output = self.logger.output.split('\n')
self.assertIn('***', output[1], output)
self.assertNotIn(auth_password, output[1], output)
+
+
+class GetAPIVersionTestCase(utils.TestCase):
+
+ @mock.patch('cinderclient.client.requests.get')
+ def test_get_server_version(self, mock_request):
+
+ mock_response = utils.TestResponse({
+ "status_code": 200,
+ "text": json.dumps(fakes.fake_request_get())
+ })
+
+ mock_request.return_value = mock_response
+
+ url = "http://192.168.122.127:8776/v3/e5526285ebd741b1819393f772f11fc3"
+
+ min_version, max_version = cinderclient.client.get_server_version(url)
+ self.assertEqual(min_version, api_versions.APIVersion('3.0'))
+ self.assertEqual(max_version, api_versions.APIVersion('3.16'))
+
+ url = "https://192.168.122.127:8776/v3/e55285ebd741b1819393f772f11fc3"
+
+ min_version, max_version = cinderclient.client.get_server_version(url)
+ self.assertEqual(min_version, api_versions.APIVersion('3.0'))
+ self.assertEqual(max_version, api_versions.APIVersion('3.16'))
diff --git a/cinderclient/tests/unit/v3/fakes.py b/cinderclient/tests/unit/v3/fakes.py
index 060b697..95cc421 100644
--- a/cinderclient/tests/unit/v3/fakes.py
+++ b/cinderclient/tests/unit/v3/fakes.py
@@ -460,3 +460,43 @@ class FakeHTTPClient(fake_v2.FakeHTTPClient):
'guaranteed_until': "2013-11-12T21:00:00.000000",
}
return 200, {}, {'message': message}
+
+
+def fake_request_get():
+ versions = {'versions': [{'id': 'v1.0',
+ 'links': [{'href': 'http://docs.openstack.org/',
+ 'rel': 'describedby',
+ 'type': 'text/html'},
+ {'href': 'http://192.168.122.197/v1/',
+ 'rel': 'self'}],
+ 'media-types': [{'base': 'application/json',
+ 'type': 'application/'}],
+ 'min_version': '',
+ 'status': 'DEPRECATED',
+ 'updated': '2016-05-02T20:25:19Z',
+ 'version': ''},
+ {'id': 'v2.0',
+ 'links': [{'href': 'http://docs.openstack.org/',
+ 'rel': 'describedby',
+ 'type': 'text/html'},
+ {'href': 'http://192.168.122.197/v2/',
+ 'rel': 'self'}],
+ 'media-types': [{'base': 'application/json',
+ 'type': 'application/'}],
+ 'min_version': '',
+ 'status': 'SUPPORTED',
+ 'updated': '2014-06-28T12:20:21Z',
+ 'version': ''},
+ {'id': 'v3.0',
+ 'links': [{'href': 'http://docs.openstack.org/',
+ 'rel': 'describedby',
+ 'type': 'text/html'},
+ {'href': 'http://192.168.122.197/v3/',
+ 'rel': 'self'}],
+ 'media-types': [{'base': 'application/json',
+ 'type': 'application/'}],
+ 'min_version': '3.0',
+ 'status': 'CURRENT',
+ 'updated': '2016-02-08T12:20:21Z',
+ 'version': '3.16'}]}
+ return versions