Use method validate_integer from oslo.utils
We added method validate_integer in oslo.utils 3.33.0 [1], so we can simply use that validate_integer method in Masakari. [1] https://review.openstack.org/#/c/508401/ Change-Id: Id773d107cf06ade64adbb5af5c764f4adeb914d7
This commit is contained in:
parent
314b3aded4
commit
a4fdab23aa
@ -237,25 +237,7 @@ class SpawnTestCase(SpawnNTestCase):
|
||||
|
||||
|
||||
class ValidateIntegerTestCase(test.NoDBTestCase):
|
||||
def test_valid_inputs(self):
|
||||
self.assertEqual(
|
||||
utils.validate_integer(42, "answer"), 42)
|
||||
self.assertEqual(
|
||||
utils.validate_integer("42", "answer"), 42)
|
||||
self.assertEqual(
|
||||
utils.validate_integer(
|
||||
"7", "lucky", min_value=7, max_value=8), 7)
|
||||
self.assertEqual(
|
||||
utils.validate_integer(
|
||||
7, "lucky", min_value=6, max_value=7), 7)
|
||||
self.assertEqual(
|
||||
utils.validate_integer(
|
||||
300, "Spartaaa!!!", min_value=300), 300)
|
||||
self.assertEqual(
|
||||
utils.validate_integer(
|
||||
"300", "Spartaaa!!!", max_value=300), 300)
|
||||
|
||||
def test_invalid_inputs(self):
|
||||
def test_exception_converted(self):
|
||||
self.assertRaises(exception.InvalidInput,
|
||||
utils.validate_integer,
|
||||
"im-not-an-int", "not-an-int")
|
||||
|
@ -28,6 +28,7 @@ from oslo_concurrency import lockutils
|
||||
from oslo_context import context as common_context
|
||||
from oslo_log import log as logging
|
||||
from oslo_utils import importutils
|
||||
from oslo_utils import strutils
|
||||
from oslo_utils import timeutils
|
||||
import six
|
||||
|
||||
@ -244,27 +245,9 @@ def tempdir(**kwargs):
|
||||
def validate_integer(value, name, min_value=None, max_value=None):
|
||||
"""Make sure that value is a valid integer, potentially within range."""
|
||||
try:
|
||||
value = int(str(value))
|
||||
except (ValueError, UnicodeEncodeError):
|
||||
msg = _('%(value_name)s must be an integer')
|
||||
raise exception.InvalidInput(reason=(
|
||||
msg % {'value_name': name}))
|
||||
|
||||
if min_value is not None:
|
||||
if value < min_value:
|
||||
msg = _('%(value_name)s must be >= %(min_value)d')
|
||||
raise exception.InvalidInput(
|
||||
reason=(msg % {'value_name': name,
|
||||
'min_value': min_value}))
|
||||
if max_value is not None:
|
||||
if value > max_value:
|
||||
msg = _('%(value_name)s must be <= %(max_value)d')
|
||||
raise exception.InvalidInput(
|
||||
reason=(
|
||||
msg % {'value_name': name,
|
||||
'max_value': max_value})
|
||||
)
|
||||
return value
|
||||
return strutils.validate_integer(value, name, min_value, max_value)
|
||||
except ValueError as e:
|
||||
raise exception.InvalidInput(reason=e)
|
||||
|
||||
|
||||
def synchronized(name, semaphores=None, blocking=False):
|
||||
|
Loading…
x
Reference in New Issue
Block a user