Add VersionPredicate type of field

Any new property which wants to use a list of versions can
use VersionPredicate to say which ranges are good.

NOTE: The new type of field is also proposed for o.vo
here: https://review.openstack.org/204113

Partially-Implements: blueprint request-spec-object

Change-Id: I8b371ec46f8844fce380988e8fdd52146197503b
This commit is contained in:
Sylvain Bauza 2015-07-20 11:26:35 +02:00
parent be8550cf65
commit b9247f52d1
2 changed files with 31 additions and 0 deletions

View File

@ -13,6 +13,7 @@
# under the License.
from collections import OrderedDict
from distutils import versionpredicate
import netaddr
from oslo_utils import strutils
@ -362,6 +363,19 @@ class MonitorMetricType(Enum):
valid_values=MonitorMetricType.ALL)
# NOTE(sbauza): Remove this on next release of oslo.versionedobjects
class VersionPredicate(fields.String):
@staticmethod
def coerce(obj, attr, value):
try:
versionpredicate.VersionPredicate('check (%s)' % value)
except ValueError:
raise ValueError(_('Version %(val)s is not a valid predicate in '
'field %(attr)s') %
{'val': value, 'attr': attr})
return value
# NOTE(danms): Remove this on next release of oslo.versionedobjects
class FlexibleBoolean(fields.Boolean):
@staticmethod
@ -629,6 +643,11 @@ class MonitorMetricTypeField(BaseEnumField):
AUTO_TYPE = MonitorMetricType()
# FIXME(sbauza): Remove this after oslo.versionedobjects gets it
class VersionPredicateField(AutoTypedField):
AUTO_TYPE = VersionPredicate()
# FIXME(danms): Remove this after oslo.versionedobjects gets it
# This is a flexible interpretation of boolean
# values using common user friendly semantics for

View File

@ -529,6 +529,18 @@ class TestMonitorMetricType(TestField):
self.assertRaises(ValueError, self.field.stringify, 'cpufrequency')
class TestVersionPredicate(TestString):
def setUp(self):
super(TestVersionPredicate, self).setUp()
self.field = fields.VersionPredicateField()
self.coerce_good_values = [('>=1.0', '>=1.0'),
('==1.1', '==1.1'),
('<1.1.0', '<1.1.0')]
self.coerce_bad_values = ['1', 'foo', '>1', 1.0, '1.0', '=1.0']
self.to_primitive_values = self.coerce_good_values[0:1]
self.from_primitive_values = self.coerce_good_values[0:1]
class TestInteger(TestField):
def setUp(self):
super(TestInteger, self).setUp()