From 08551106e6cfb32d938d83a74d3ee9f27ad05ee0 Mon Sep 17 00:00:00 2001 From: Stephen Finucane Date: Sun, 4 Jun 2023 13:34:44 +0100 Subject: [PATCH] Remove use of oslo.utils While a relatively small library, this is one import that we really don't need. Remove it. Change-Id: I726f3c3548cbd896588ef0613222e36c529f5bcc Signed-off-by: Stephen Finucane --- openstackclient/compute/v2/server.py | 33 ++++++++++++++++++---- openstackclient/compute/v2/server_event.py | 32 ++++++++++++++++++--- requirements.txt | 1 - 3 files changed, 56 insertions(+), 10 deletions(-) diff --git a/openstackclient/compute/v2/server.py b/openstackclient/compute/v2/server.py index 61fffe2d3..2f20d022b 100644 --- a/openstackclient/compute/v2/server.py +++ b/openstackclient/compute/v2/server.py @@ -32,13 +32,11 @@ from osc_lib.cli import parseractions from osc_lib.command import command from osc_lib import exceptions from osc_lib import utils -from oslo_utils import strutils from openstackclient.i18n import _ from openstackclient.identity import common as identity_common from openstackclient.network import common as network_common - LOG = logging.getLogger(__name__) IMAGE_STRING_FOR_BFV = 'N/A (booted from volume)' @@ -273,6 +271,30 @@ def _prep_server_detail(compute_client, image_client, server, refresh=True): return info +def bool_from_str(value, strict=False): + true_strings = ('1', 't', 'true', 'on', 'y', 'yes') + false_strings = ('0', 'f', 'false', 'off', 'n', 'no') + + if isinstance(value, bool): + return value + + lowered = value.strip().lower() + if lowered in true_strings: + return True + elif lowered in false_strings or not strict: + return False + + msg = _( + "Unrecognized value '%(value)s'; acceptable values are: %(valid)s" + ) % { + 'value': value, + 'valid': ', '.join( + f"'{s}'" for s in sorted(true_strings + false_strings) + ), + } + raise ValueError(msg) + + def boolenv(*vars, default=False): """Search for the first defined of possibly many bool-like env vars. @@ -287,7 +309,7 @@ def boolenv(*vars, default=False): for v in vars: value = os.environ.get(v, None) if value: - return strutils.bool_from_string(value) + return bool_from_str(value) return default @@ -1713,8 +1735,9 @@ class CreateServer(command.ShowOne): if 'delete_on_termination' in mapping: try: - value = strutils.bool_from_string( - mapping['delete_on_termination'], strict=True + value = bool_from_str( + mapping['delete_on_termination'], + strict=True, ) except ValueError: msg = _( diff --git a/openstackclient/compute/v2/server_event.py b/openstackclient/compute/v2/server_event.py index 7c161cee5..683179524 100644 --- a/openstackclient/compute/v2/server_event.py +++ b/openstackclient/compute/v2/server_event.py @@ -16,6 +16,7 @@ """Compute v2 Server operation event implementations""" import logging +import uuid from cliff import columns import iso8601 @@ -24,14 +25,37 @@ from openstack import utils as sdk_utils from osc_lib.command import command from osc_lib import exceptions from osc_lib import utils -from oslo_utils import uuidutils from openstackclient.i18n import _ - LOG = logging.getLogger(__name__) +# TODO(stephenfin): Move this to osc_lib since it's useful elsewhere (e.g. +# glance) +def is_uuid_like(value) -> bool: + """Returns validation of a value as a UUID. + + :param val: Value to verify + :type val: string + :returns: bool + + .. versionchanged:: 1.1.1 + Support non-lowercase UUIDs. + """ + try: + formatted_value = ( + value.replace('urn:', '') + .replace('uuid:', '') + .strip('{}') + .replace('-', '') + .lower() + ) + return str(uuid.UUID(value)).replace('-', '') == formatted_value + except (TypeError, ValueError, AttributeError): + return False + + class ServerActionEventColumn(columns.FormattableColumn): """Custom formatter for server action events. @@ -202,7 +226,7 @@ class ListServerEvent(command.Lister): # If we fail to find the resource, it is possible the server is # deleted. Try once more using the arg directly if it is a # UUID. - if uuidutils.is_uuid_like(parsed_args.server): + if is_uuid_like(parsed_args.server): server_id = parsed_args.server else: raise @@ -275,7 +299,7 @@ class ShowServerEvent(command.ShowOne): # If we fail to find the resource, it is possible the server is # deleted. Try once more using the arg directly if it is a # UUID. - if uuidutils.is_uuid_like(parsed_args.server): + if is_uuid_like(parsed_args.server): server_id = parsed_args.server else: raise diff --git a/requirements.txt b/requirements.txt index bc9505628..a6b67dcc3 100644 --- a/requirements.txt +++ b/requirements.txt @@ -10,7 +10,6 @@ iso8601>=0.1.11 # MIT openstacksdk>=1.4.0 # Apache-2.0 osc-lib>=2.3.0 # Apache-2.0 oslo.i18n>=3.15.3 # Apache-2.0 -oslo.utils>=3.33.0 # Apache-2.0 python-keystoneclient>=3.22.0 # Apache-2.0 python-novaclient>=18.1.0 # Apache-2.0 python-cinderclient>=3.3.0 # Apache-2.0