add allow_reserved in IPOption

This commit is contained in:
Emmanuel Garette 2013-09-19 21:51:55 +02:00
parent 30c376e3ea
commit 28c416dd84
2 changed files with 14 additions and 4 deletions

View file

@ -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])

View file

@ -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"))