Use method validate_integer from oslo.utils
We added method validate_integer in oslo.utils 3.33.0 [1], so we can simply the method validate_integer in Nova. Depends-On: I741875eba329b762789a7c7c910a6c46beeff3fa [1] https://review.openstack.org/#/c/508401/ Change-Id: Id90c7f66f75de0264ea70233ee020fab2ddc9012
This commit is contained in:
parent
d19e1252d5
commit
12fce9970a
|
@ -839,25 +839,7 @@ class StringLengthTestCase(test.NoDBTestCase):
|
|||
|
||||
|
||||
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")
|
||||
|
|
|
@ -851,29 +851,19 @@ def check_string_length(value, name=None, min_length=0, max_length=None):
|
|||
|
||||
|
||||
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}))
|
||||
"""Make sure that value is a valid integer, potentially within range.
|
||||
|
||||
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
|
||||
:param value: value of the integer
|
||||
:param name: name of the integer
|
||||
:param min_value: min_value of the integer
|
||||
:param max_value: max_value of the integer
|
||||
:returns: integer
|
||||
:raise: InvalidInput If value is not a valid integer
|
||||
"""
|
||||
try:
|
||||
return strutils.validate_integer(value, name, min_value, max_value)
|
||||
except ValueError as e:
|
||||
raise exception.InvalidInput(reason=six.text_type(e))
|
||||
|
||||
|
||||
def _serialize_profile_info():
|
||||
|
|
Loading…
Reference in New Issue