Skip to content

Commit 0855110

Browse files
committed
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 <stephenfin@redhat.com>
1 parent 8735b86 commit 0855110

3 files changed

Lines changed: 56 additions & 10 deletions

File tree

openstackclient/compute/v2/server.py

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,11 @@
3232
from osc_lib.command import command
3333
from osc_lib import exceptions
3434
from osc_lib import utils
35-
from oslo_utils import strutils
3635

3736
from openstackclient.i18n import _
3837
from openstackclient.identity import common as identity_common
3938
from openstackclient.network import common as network_common
4039

41-
4240
LOG = logging.getLogger(__name__)
4341

4442
IMAGE_STRING_FOR_BFV = 'N/A (booted from volume)'
@@ -273,6 +271,30 @@ def _prep_server_detail(compute_client, image_client, server, refresh=True):
273271
return info
274272

275273

274+
def bool_from_str(value, strict=False):
275+
true_strings = ('1', 't', 'true', 'on', 'y', 'yes')
276+
false_strings = ('0', 'f', 'false', 'off', 'n', 'no')
277+
278+
if isinstance(value, bool):
279+
return value
280+
281+
lowered = value.strip().lower()
282+
if lowered in true_strings:
283+
return True
284+
elif lowered in false_strings or not strict:
285+
return False
286+
287+
msg = _(
288+
"Unrecognized value '%(value)s'; acceptable values are: %(valid)s"
289+
) % {
290+
'value': value,
291+
'valid': ', '.join(
292+
f"'{s}'" for s in sorted(true_strings + false_strings)
293+
),
294+
}
295+
raise ValueError(msg)
296+
297+
276298
def boolenv(*vars, default=False):
277299
"""Search for the first defined of possibly many bool-like env vars.
278300
@@ -287,7 +309,7 @@ def boolenv(*vars, default=False):
287309
for v in vars:
288310
value = os.environ.get(v, None)
289311
if value:
290-
return strutils.bool_from_string(value)
312+
return bool_from_str(value)
291313
return default
292314

293315

@@ -1713,8 +1735,9 @@ def _match_image(image_api, wanted_properties):
17131735

17141736
if 'delete_on_termination' in mapping:
17151737
try:
1716-
value = strutils.bool_from_string(
1717-
mapping['delete_on_termination'], strict=True
1738+
value = bool_from_str(
1739+
mapping['delete_on_termination'],
1740+
strict=True,
17181741
)
17191742
except ValueError:
17201743
msg = _(

openstackclient/compute/v2/server_event.py

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"""Compute v2 Server operation event implementations"""
1717

1818
import logging
19+
import uuid
1920

2021
from cliff import columns
2122
import iso8601
@@ -24,14 +25,37 @@
2425
from osc_lib.command import command
2526
from osc_lib import exceptions
2627
from osc_lib import utils
27-
from oslo_utils import uuidutils
2828

2929
from openstackclient.i18n import _
3030

31-
3231
LOG = logging.getLogger(__name__)
3332

3433

34+
# TODO(stephenfin): Move this to osc_lib since it's useful elsewhere (e.g.
35+
# glance)
36+
def is_uuid_like(value) -> bool:
37+
"""Returns validation of a value as a UUID.
38+
39+
:param val: Value to verify
40+
:type val: string
41+
:returns: bool
42+
43+
.. versionchanged:: 1.1.1
44+
Support non-lowercase UUIDs.
45+
"""
46+
try:
47+
formatted_value = (
48+
value.replace('urn:', '')
49+
.replace('uuid:', '')
50+
.strip('{}')
51+
.replace('-', '')
52+
.lower()
53+
)
54+
return str(uuid.UUID(value)).replace('-', '') == formatted_value
55+
except (TypeError, ValueError, AttributeError):
56+
return False
57+
58+
3559
class ServerActionEventColumn(columns.FormattableColumn):
3660
"""Custom formatter for server action events.
3761
@@ -202,7 +226,7 @@ def take_action(self, parsed_args):
202226
# If we fail to find the resource, it is possible the server is
203227
# deleted. Try once more using the <server> arg directly if it is a
204228
# UUID.
205-
if uuidutils.is_uuid_like(parsed_args.server):
229+
if is_uuid_like(parsed_args.server):
206230
server_id = parsed_args.server
207231
else:
208232
raise
@@ -275,7 +299,7 @@ def take_action(self, parsed_args):
275299
# If we fail to find the resource, it is possible the server is
276300
# deleted. Try once more using the <server> arg directly if it is a
277301
# UUID.
278-
if uuidutils.is_uuid_like(parsed_args.server):
302+
if is_uuid_like(parsed_args.server):
279303
server_id = parsed_args.server
280304
else:
281305
raise

requirements.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ iso8601>=0.1.11 # MIT
1010
openstacksdk>=1.4.0 # Apache-2.0
1111
osc-lib>=2.3.0 # Apache-2.0
1212
oslo.i18n>=3.15.3 # Apache-2.0
13-
oslo.utils>=3.33.0 # Apache-2.0
1413
python-keystoneclient>=3.22.0 # Apache-2.0
1514
python-novaclient>=18.1.0 # Apache-2.0
1615
python-cinderclient>=3.3.0 # Apache-2.0

0 commit comments

Comments
 (0)