Replace BoundedInt with WSME's IntegerType
WSME has contained IntegerType class since version 0.6. The class has been tested in WSME, and it is almost the same as Ceilometer's BoundedInt class. For the maintainability, this patch replaces BoundedInt class with WSME's IntegerType class. Change-Id: I8444d54c0bc53aec26dad57645f3f6e3f9867f01
This commit is contained in:
parent
a9f8c743b7
commit
dc71da49e1
@ -82,39 +82,6 @@ class EntityNotFound(ClientSideError):
|
||||
status_code=404)
|
||||
|
||||
|
||||
class BoundedInt(wtypes.UserType):
|
||||
basetype = int
|
||||
|
||||
def __init__(self, min=None, max=None):
|
||||
self.min = min
|
||||
self.max = max
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
if self.min is not None and self.max is not None:
|
||||
return 'int between %d and %d' % (self.min, self.max)
|
||||
elif self.min is not None:
|
||||
return 'int greater than %d' % self.min
|
||||
else:
|
||||
return 'int lower than %d' % self.max
|
||||
|
||||
@staticmethod
|
||||
def frombasetype(value):
|
||||
return int(value) if value is not None else None
|
||||
|
||||
def validate(self, value):
|
||||
if self.min is not None and value < self.min:
|
||||
error = _('Value %(value)s is invalid (should be greater or equal '
|
||||
'to %(min)s)') % dict(value=value, min=self.min)
|
||||
raise ClientSideError(error)
|
||||
|
||||
if self.max is not None and value > self.max:
|
||||
error = _('Value %(value)s is invalid (should be lower or equal '
|
||||
'to %(max)s)') % dict(value=value, max=self.max)
|
||||
raise ClientSideError(error)
|
||||
return value
|
||||
|
||||
|
||||
class AdvEnum(wtypes.wsproperty):
|
||||
"""Handle default and mandatory for wtypes.Enum
|
||||
"""
|
||||
@ -1094,7 +1061,7 @@ class AlarmThresholdRule(_Base):
|
||||
Ownership settings are automatically included based on the Alarm owner.
|
||||
"""
|
||||
|
||||
period = wsme.wsattr(BoundedInt(min=1), default=60)
|
||||
period = wsme.wsattr(wtypes.IntegerType(minimum=1), default=60)
|
||||
"The time range in seconds over which query"
|
||||
|
||||
comparison_operator = AdvEnum('comparison_operator', str,
|
||||
@ -1109,7 +1076,7 @@ class AlarmThresholdRule(_Base):
|
||||
'count', default='avg')
|
||||
"The statistic to compare to the threshold"
|
||||
|
||||
evaluation_periods = wsme.wsattr(BoundedInt(min=1), default=1)
|
||||
evaluation_periods = wsme.wsattr(wtypes.IntegerType(minimum=1), default=1)
|
||||
"The number of historical periods to evaluate the threshold"
|
||||
|
||||
exclude_outliers = wsme.wsattr(bool, default=False)
|
||||
|
@ -500,7 +500,7 @@ class TestAlarms(FunctionalTest,
|
||||
json['threshold_rule']['query'].append({
|
||||
'field': 'project_id', 'op': 'eq',
|
||||
'value': self.auth_headers['X-Project-Id']})
|
||||
# to check to BoundedInt type conversion
|
||||
# to check to IntegerType type conversion
|
||||
json['threshold_rule']['evaluation_periods'] = 3
|
||||
json['threshold_rule']['period'] = 180
|
||||
self._verify_alarm(json, alarms[0], 'added_alarm')
|
||||
|
@ -29,30 +29,6 @@ class TestWsmeCustomType(test.BaseTestCase):
|
||||
super(TestWsmeCustomType, self).setUp()
|
||||
pecan.response = mock.MagicMock()
|
||||
|
||||
def test_bounded_int_convertion(self):
|
||||
bi = v2.BoundedInt(1, 5)
|
||||
self.assertEqual(bi.frombasetype("2"), 2)
|
||||
|
||||
def test_bounded_int_invalid_convertion(self):
|
||||
bi = v2.BoundedInt(1, 5)
|
||||
self.assertRaises(TypeError, bi.frombasetype, wsme)
|
||||
|
||||
def test_bounded_int_maxmin(self):
|
||||
bi = v2.BoundedInt(1, 5)
|
||||
self.assertRaises(wsme.exc.ClientSideError, bi.validate, -1)
|
||||
self.assertRaises(wsme.exc.ClientSideError, bi.validate, 7)
|
||||
self.assertEqual(bi.validate(2), 2)
|
||||
|
||||
def test_bounded_int_max(self):
|
||||
bi = v2.BoundedInt(max=5)
|
||||
self.assertEqual(bi.validate(-1), -1)
|
||||
self.assertRaises(wsme.exc.ClientSideError, bi.validate, 7)
|
||||
|
||||
def test_bounded_int_min(self):
|
||||
bi = v2.BoundedInt(min=5)
|
||||
self.assertEqual(bi.validate(7), 7)
|
||||
self.assertRaises(wsme.exc.ClientSideError, bi.validate, -1)
|
||||
|
||||
def test_advenum_default(self):
|
||||
class dummybase(wsme.types.Base):
|
||||
ae = v2.AdvEnum("name", str, "one", "other", default="other")
|
||||
|
@ -27,4 +27,4 @@ SQLAlchemy>=0.7.8,<=0.8.99
|
||||
sqlalchemy-migrate>=0.8.2
|
||||
stevedore>=0.14
|
||||
WebOb>=1.2.3
|
||||
WSME>=0.5b6
|
||||
WSME>=0.6
|
||||
|
Loading…
x
Reference in New Issue
Block a user