From dc71da49e12ed3a26628a26fdf6f55b64533d53a Mon Sep 17 00:00:00 2001 From: Ken'ichi Ohmichi Date: Fri, 7 Feb 2014 20:02:23 +0900 Subject: [PATCH] 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 --- ceilometer/api/controllers/v2.py | 37 +------------------ .../tests/api/v2/test_alarm_scenarios.py | 2 +- .../tests/api/v2/test_wsme_custom_type.py | 24 ------------ requirements.txt | 2 +- 4 files changed, 4 insertions(+), 61 deletions(-) diff --git a/ceilometer/api/controllers/v2.py b/ceilometer/api/controllers/v2.py index 803d9428c..3573d5ab3 100644 --- a/ceilometer/api/controllers/v2.py +++ b/ceilometer/api/controllers/v2.py @@ -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) diff --git a/ceilometer/tests/api/v2/test_alarm_scenarios.py b/ceilometer/tests/api/v2/test_alarm_scenarios.py index eec679fa8..3dfd3b8d3 100644 --- a/ceilometer/tests/api/v2/test_alarm_scenarios.py +++ b/ceilometer/tests/api/v2/test_alarm_scenarios.py @@ -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') diff --git a/ceilometer/tests/api/v2/test_wsme_custom_type.py b/ceilometer/tests/api/v2/test_wsme_custom_type.py index ff8af812b..93979d042 100644 --- a/ceilometer/tests/api/v2/test_wsme_custom_type.py +++ b/ceilometer/tests/api/v2/test_wsme_custom_type.py @@ -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") diff --git a/requirements.txt b/requirements.txt index 7d9596850..6dd6bce75 100644 --- a/requirements.txt +++ b/requirements.txt @@ -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