Merge "Refactor API version negotiation code"

This commit is contained in:
Zuul 2020-09-12 15:24:42 +00:00 committed by Gerrit Code Review
commit d43dc1ee36
1 changed files with 10 additions and 15 deletions

View File

@ -12,9 +12,6 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
from distutils.version import StrictVersion
from oslo_config import cfg from oslo_config import cfg
from oslo_log import log from oslo_log import log
from oslo_serialization import jsonutils from oslo_serialization import jsonutils
@ -35,6 +32,9 @@ LOG = log.getLogger(__name__)
MIN_IRONIC_VERSION = (1, 31) MIN_IRONIC_VERSION = (1, 31)
AGENT_VERSION_IRONIC_VERSION = (1, 36) AGENT_VERSION_IRONIC_VERSION = (1, 36)
AGENT_TOKEN_IRONIC_VERSION = (1, 62) AGENT_TOKEN_IRONIC_VERSION = (1, 62)
# NOTE(dtantsur): change this constant every time you add support for more
# versions to ensure that we send the highest version we know about.
MAX_KNOWN_VERSION = AGENT_TOKEN_IRONIC_VERSION
class APIClient(object): class APIClient(object):
@ -80,17 +80,11 @@ class APIClient(object):
cert=cert, cert=cert,
**kwargs) **kwargs)
def _get_ironic_api_version_header(self): def _get_ironic_api_version_header(self, version=None):
# TODO(TheJulia): It would be great to improve version handling if version is None:
# logic, but we need to ship a newer version if we can. ironic_version = self._get_ironic_api_version()
ironic_version = "%d.%d" % self._get_ironic_api_version() version = min(ironic_version, AGENT_TOKEN_IRONIC_VERSION)
agent_token_version = "%d.%d" % AGENT_TOKEN_IRONIC_VERSION return {'X-OpenStack-Ironic-API-Version': '%d.%d' % version}
if (StrictVersion(ironic_version)
>= StrictVersion(agent_token_version)):
version = agent_token_version
else:
version = ironic_version
return {'X-OpenStack-Ironic-API-Version': version}
def _get_ironic_api_version(self): def _get_ironic_api_version(self):
if not self._ironic_api_version: if not self._ironic_api_version:
@ -121,7 +115,8 @@ class APIClient(object):
if api_ver >= AGENT_VERSION_IRONIC_VERSION: if api_ver >= AGENT_VERSION_IRONIC_VERSION:
data['agent_version'] = version.version_info.release_string() data['agent_version'] = version.version_info.release_string()
headers = self._get_ironic_api_version_header() api_ver = min(MAX_KNOWN_VERSION, api_ver)
headers = self._get_ironic_api_version_header(api_ver)
try: try:
response = self._request('POST', path, data=data, headers=headers) response = self._request('POST', path, data=data, headers=headers)