Add a 'netaddress' value type for --key.

It is quite common to expect either a numeric IP address or domain name.
We include all ASCII characters needed to express IPv4, CIDR networks,
IPv6, or DNS names.

Change-Id: I2ab370d2bdad0d568b34c4ed55d60a5b6cdd9b19
This commit is contained in:
Clint Byrum 2013-05-28 11:10:46 -07:00
parent 1ef136031c
commit e76e73a61d
3 changed files with 25 additions and 1 deletions

View File

@ -181,7 +181,8 @@ def parse_opts(argv):
' (may be used with --type and --key-default)')
parser.add_argument('--type', default='default',
help='exit with error if the specified --key does not'
' match type. Valid types are <int|default|raw>')
' match type. Valid types are'
' <int|default|netaddress|raw>')
parser.add_argument('--key-default',
help='This option only affects running with --key.'
' Print this if key is not found. This value is'

View File

@ -43,3 +43,25 @@ class ValueTypeTestCase(testtools.TestCase):
def test_raw_empty(self):
self.assertEqual('',
value_types.ensure_type('', 'raw'))
def test_net_address_ipv4(self):
self.assertEqual('192.0.2.1', value_types.ensure_type('192.0.2.1',
'netaddress'))
def test_net_address_cidr(self):
self.assertEqual('192.0.2.0/24',
value_types.ensure_type('192.0.2.0/24', 'netaddress'))
def test_ent_address_ipv6(self):
self.assertEqual('::', value_types.ensure_type('::', 'netaddress'))
self.assertEqual('2001:db8::2:1', value_types.ensure_type(
'2001:db8::2:1', 'netaddress'))
def test_net_address_dns(self):
self.assertEqual('host.0domain-name.test',
value_types.ensure_type('host.0domain-name.test',
'netaddress'))
def test_net_address_bad(self):
self.assertRaises(config_exception.ConfigException,
value_types.ensure_type, "192.0.2.1;DROP TABLE foo")

View File

@ -20,6 +20,7 @@ from config_exception import ConfigException
TYPES = {
"int": "^[0-9]+$",
"default": "^[A-Za-z0-9]*$",
"netaddress": "^[A-Za-z0-9/.:-]+$",
"raw": ""
}