Skip to content

Commit

Permalink
Use oslo_utils.is_uuid_like for id_or_uuid
Browse files Browse the repository at this point in the history
Code from the stone age was using isinstance(..., str) to check the
potential UUID-ness of an input param.  With a "fix" in
oslo.versionedobjects [1] this started causing failures in VSCSI delete
paths that used find_maps.  Here we switch over to using the more modern
is_uuid_like from oslo.utils.

[1] https://review.openstack.org/#/q/Ic6b6308fb1960ec40407e6efde30137b64543e72

Closes-Bug: #1766692
Change-Id: If0ffa8157890b862c67fc720a55f0e899b23d587
  • Loading branch information
Eric Fried committed Apr 25, 2018
1 parent ac6a823 commit d55b4c8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
22 changes: 13 additions & 9 deletions pypowervm/tests/utils/test_uuid.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
# under the License.


import six

from pypowervm.utils import uuid as uuid_utils

import unittest
Expand All @@ -38,12 +40,14 @@ def test_uuid_conversion(self):

def test_id_or_uuid(self):
self.assertEqual((False, 123), uuid_utils.id_or_uuid(123))
self.assertEqual((False, 123), uuid_utils.id_or_uuid('123'))
self.assertEqual(
(True, '12345678-abcd-ABCD-0000-0a1B2c3D4e5F'),
uuid_utils.id_or_uuid('12345678-abcd-ABCD-0000-0a1B2c3D4e5F'))
self.assertRaises(ValueError, uuid_utils.id_or_uuid,
'12345678abcdABCD00000a1B2c3D4e5F')
# This one has too many digits
self.assertRaises(ValueError, uuid_utils.id_or_uuid,
'12345678-abcd-ABCD-0000-0a1B2c3D4e5F0')
# Test all stringish permutations
converters = [lambda x: x, six.text_type]
for conv in converters:
self.assertEqual((False, 123), uuid_utils.id_or_uuid(conv('123')))
uuid = conv('12345678-abcd-ABCD-0000-0a1B2c3D4e5F')
self.assertEqual((True, uuid), uuid_utils.id_or_uuid(uuid))
uuid = conv('12345678abcdABCD00000a1B2c3D4e5F')
self.assertEqual((True, uuid), uuid_utils.id_or_uuid(uuid))
# This one has too many digits
self.assertRaises(ValueError, uuid_utils.id_or_uuid,
conv('12345678-abcd-ABCD-0000-0a1B2c3D4e5F0'))
6 changes: 2 additions & 4 deletions pypowervm/utils/uuid.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@

"""Utilities around Universally-Unique Identifiers (UUIDs)."""

import re

from pypowervm import const
from oslo_utils import uuidutils


def convert_uuid_to_pvm(uuid):
Expand Down Expand Up @@ -47,7 +45,7 @@ def id_or_uuid(an_id):
:return: The input ID, either converted to int, or in its original string
form if a UUID.
"""
if isinstance(an_id, str) and re.match(const.UUID_REGEX_WORD, an_id):
if uuidutils.is_uuid_like(an_id):
is_uuid = True
ret_id = an_id
else:
Expand Down

0 comments on commit d55b4c8

Please sign in to comment.