Skip to content

Commit

Permalink
utils: Add 'supports_version'
Browse files Browse the repository at this point in the history
Not all services use microversion (looking at you, Glance), meaning the
'supports_microversion' helper is of no use to them. Add a
'support_version' helper for these services. We will use this shortly in
the cloud layer.

Change-Id: I02fa4037b8119681379bb3c4b7bb667efff506ee
Signed-off-by: Stephen Finucane <stephenfin@redhat.com>
  • Loading branch information
stephenfin committed May 26, 2023
1 parent 0b137cf commit 3cb5bc9
Showing 1 changed file with 45 additions and 10 deletions.
55 changes: 45 additions & 10 deletions openstack/utils.py
Expand Up @@ -9,6 +9,9 @@
# 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 __future__ import annotations

This comment has been minimized.

Copy link
@haligan

haligan Jun 27, 2023

This import statement breaks compatibility with Python Version 3.6. This includes RHEL 8.4, which by default uses Python 3.6.8.

According to some stack articles, it sounds like future isn't available until 3.7. Therefore, the openstackSDK isn't useable by Python 3.6 at this point.

Below is from a system with fresh openstacksdk install:

# openstack project list
Traceback (most recent call last):
  File "/usr/local/bin/openstack", line 7, in <module>
    from openstackclient.shell import main
  File "/usr/local/lib/python3.6/site-packages/openstackclient/shell.py", line 23, in <module>
    from osc_lib import shell
  File "/usr/local/lib/python3.6/site-packages/osc_lib/shell.py", line 32, in <module>
    from osc_lib.cli import client_config as cloud_config
  File "/usr/local/lib/python3.6/site-packages/osc_lib/cli/client_config.py", line 18, in <module>
    from openstack.config import exceptions as sdk_exceptions
  File "/usr/local/lib/python3.6/site-packages/openstack/__init__.py", line 57, in <module>
    import openstack.config
  File "/usr/local/lib/python3.6/site-packages/openstack/config/__init__.py", line 17, in <module>
    from openstack.config.loader import OpenStackConfig  # noqa
  File "/usr/local/lib/python3.6/site-packages/openstack/config/loader.py", line 33, in <module>
    from openstack.config import cloud_region
  File "/usr/local/lib/python3.6/site-packages/openstack/config/cloud_region.py", line 49, in <module>
    from openstack import proxy
  File "/usr/local/lib/python3.6/site-packages/openstack/proxy.py", line 34, in <module>
    from openstack import resource
  File "/usr/local/lib/python3.6/site-packages/openstack/resource.py", line 50, in <module>
    from openstack import utils
  File "/usr/local/lib/python3.6/site-packages/openstack/utils.py", line 13
    from __future__ import annotations
    ^
SyntaxError: future feature annotations is not defined

# python3 --version
Python 3.6.8

# hostnamectl | grep -i "operating system"
  Operating System: Red Hat Enterprise Linux 8.4 (Ootpa)
  
 # grep future /usr/local/lib/python3.6/site-packages/openstack/utils.py
from __future__ import annotations
  

Below is from system with old install:

# openstack
(openstack) exit
# hostnamectl | grep -i "operating system"
  Operating System: Red Hat Enterprise Linux 8.4 (Ootpa)
# python3 --version
Python 3.6.8
#grep future /usr/local/lib/python3.6/site-packages/openstack/utils.py
#

Steps to reproduce.

Install python 3.6.8
Install pip3
Install with pip3 python-openstackclient
Attempt any openstack command

This comment has been minimized.

Copy link
@ricolin

ricolin Jun 28, 2023

Contributor

Hi @haligan I just open a patch so we can have our discussion there https://review.opendev.org/c/openstack/openstacksdk/+/887137


from collections.abc import Mapping
import hashlib
import queue
Expand All @@ -17,6 +20,7 @@
import time

import keystoneauth1
from keystoneauth1 import adapter as ks_adapter
from keystoneauth1 import discover

from openstack import _log
Expand Down Expand Up @@ -97,25 +101,56 @@ def __getitem__(self, key):
return keys


def supports_version(
adapter: ks_adapter.Adapter,
version: str,
raise_exception: bool = False,
) -> bool:
"""Determine if the given adapter supports the given version.
Checks the version asserted by the service and ensures this matches the
provided version. ``version`` can be a major version of a major-minor
version
:param adapter: :class:`~keystoneauth1.adapter.Adapter` instance.
:param version: String containing the desired version.
:param raise_exception: Raise exception when requested version
is not supported by the server.
:returns: ``True`` if the service supports the version, else ``False``.
:raises: :class:`~openstack.exceptions.SDKException` when
``raise_exception`` is ``True`` and requested version is not supported.
"""
required = discover.normalize_version_number(version)

if discover.version_match(required, adapter.get_api_major_version()):
return True

if raise_exception:
raise exceptions.SDKException(
f'Required version {version} is not supported by the server'
)

return False


def supports_microversion(adapter, microversion, raise_exception=False):
"""Determine if the given adapter supports the given microversion.
Checks the min and max microversion asserted by the service and checks to
make sure that ``min <= microversion <= max``. Current default microversion
is taken into consideration if set and verifies that ``microversion <=
default``.
Checks the min and max microversion asserted by the service and ensures
``min <= microversion <= max``. If set, the current default microversion is
taken into consideration to ensure ``microversion <= default``.
:param adapter: :class:`~keystoneauth1.adapter.Adapter` instance.
:param str microversion: String containing the desired microversion.
:param bool raise_exception: Raise exception when requested microversion
is not supported be the server side or is higher than the current
default microversion.
:returns: True if the service supports the microversion.
is not supported by the server or is higher than the current default
microversion.
:returns: True if the service supports the microversion, else False.
:rtype: bool
:raises: :class:`~openstack.exceptions.SDKException` when requested
microversion is not supported.
:raises: :class:`~openstack.exceptions.SDKException` when
``raise_exception`` is ``True`` and requested microversion is not
supported.
"""

endpoint_data = adapter.get_endpoint_data()
if (
endpoint_data.min_microversion
Expand Down

0 comments on commit 3cb5bc9

Please sign in to comment.