From 5098d45cca0e858f8763a083308733bbc470b120 Mon Sep 17 00:00:00 2001 From: Grzegorz Grasza Date: Wed, 23 Nov 2022 15:58:22 +0100 Subject: [PATCH] Allow passing of version header Add keyword option to get_version_data() to allow passing of the version header so that we can get the microversions. Specifically, this is so that we can re-use this function in barbican, which recently implemented microversions, but doesn't return them by default, for backward compatibility with old clients. Change-Id: I909750381a559f9dc61650c9f98c88d4481012b7 --- keystoneauth1/discover.py | 7 ++++++- keystoneauth1/tests/unit/test_discovery.py | 6 ++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/keystoneauth1/discover.py b/keystoneauth1/discover.py index f586e95a..7c36f4ee 100644 --- a/keystoneauth1/discover.py +++ b/keystoneauth1/discover.py @@ -58,7 +58,7 @@ def _int_or_latest(val): return LATEST if val == 'latest' or val == LATEST else int(val) -def get_version_data(session, url, authenticated=None): +def get_version_data(session, url, authenticated=None, version_header=None): """Retrieve raw version data from a url. The return is a list of dicts of the form:: @@ -93,10 +93,15 @@ def get_version_data(session, url, authenticated=None): :param string url: Endpoint or discovery URL from which to retrieve data. :param bool authenticated: Include a token in the discovery call. (optional) Defaults to None. + :param string version_header: provide the OpenStack-API-Version header + for services which don't return version information without it, for + backward compatibility. :return: A list of dicts containing version information. :rtype: list(dict) """ headers = {'Accept': 'application/json'} + if version_header: + headers['OpenStack-API-Version'] = version_header try: resp = session.get(url, headers=headers, authenticated=authenticated) diff --git a/keystoneauth1/tests/unit/test_discovery.py b/keystoneauth1/tests/unit/test_discovery.py index b0dca60f..8e8184fa 100644 --- a/keystoneauth1/tests/unit/test_discovery.py +++ b/keystoneauth1/tests/unit/test_discovery.py @@ -1321,7 +1321,8 @@ class EndpointDataTests(utils.TestCase): def test_run_discovery_auth(self): url = 'https://example.com' - headers = {'Accept': 'application/json'} + headers = {'Accept': 'application/json', + 'OpenStack-API-Version': 'version header test'} session = mock.Mock() session.get.side_effect = [ @@ -1332,7 +1333,8 @@ class EndpointDataTests(utils.TestCase): ] try: - discover.get_version_data(session, url) + discover.get_version_data( + session, url, version_header='version header test') except exceptions.BadRequest: pass # Only one call with 'url'