From 28c416dd84bba8eb1e5164af9f00de43a5aa5a6e Mon Sep 17 00:00:00 2001 From: Emmanuel Garette Date: Thu, 19 Sep 2013 21:51:55 +0200 Subject: [PATCH] add allow_reserved in IPOption --- test/test_config_ip.py | 9 +++++++++ tiramisu/option.py | 9 +++++---- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/test/test_config_ip.py b/test/test_config_ip.py index e889c92..2bdba53 100644 --- a/test/test_config_ip.py +++ b/test/test_config_ip.py @@ -29,6 +29,15 @@ def test_ip_default(): c.a == '88.88.88.88' +def test_ip_reserved(): + a = IPOption('a', '') + b = IPOption('b', '', allow_reserved=True) + od = OptionDescription('od', '', [a, b]) + c = Config(od) + raises(ValueError, "c.a = '226.94.1.1'") + c.b = '226.94.1.1' + + def test_network(): a = NetworkOption('a', '') od = OptionDescription('od', '', [a]) diff --git a/tiramisu/option.py b/tiramisu/option.py index 1948f39..0c7e732 100644 --- a/tiramisu/option.py +++ b/tiramisu/option.py @@ -702,14 +702,15 @@ class SymLinkOption(BaseOption): class IPOption(Option): "represents the choice of an ip" - __slots__ = ('_only_private',) + __slots__ = ('_only_private', '_allow_reserved') _opt_type = 'ip' def __init__(self, name, doc, default=None, default_multi=None, requires=None, multi=False, callback=None, callback_params=None, validator=None, validator_params=None, - properties=None, only_private=False): + properties=None, only_private=False, allow_reserved=False): self._only_private = only_private + self._allow_reserved = allow_reserved super(IPOption, self).__init__(name, doc, default=default, default_multi=default_multi, callback=callback, @@ -722,8 +723,8 @@ class IPOption(Option): def _validate(self, value): ip = IP('{0}/32'.format(value)) - if ip.iptype() == 'RESERVED': - raise ValueError(_("IP shall not be in reserved class")) + if not self._allow_reserved and ip.iptype() == 'RESERVED': + raise ValueError(_("IP mustn't not be in reserved class")) if self._only_private and not ip.iptype() == 'PRIVATE': raise ValueError(_("IP must be in private class"))