Add method is_valid_cidr to netutils
This method exists in Nova[1], Magnum[2] and neutron_client[3], and can be leveraged by other projects as well. [1]https://github.com/openstack/nova/blob/13.0.0.0b2/nova/utils.py#L657 [2]https://github.com/openstack/magnum/blob/1.1.0/magnum/common/utils.py#L256 [3]https://github.com/openstack/python-neutronclient/blob/4.0.0/neutronclient/common/utils.py#L187 Change-Id: Ia9c8c0123e6d1e9f8bd2b662871b4d01f14bd06d
This commit is contained in:
parent
2bb9015f11
commit
f082a5b859
@ -111,6 +111,32 @@ def is_valid_ipv6(address):
|
||||
return False
|
||||
|
||||
|
||||
def is_valid_cidr(address):
|
||||
"""Verify that address represents a valid CIDR address.
|
||||
|
||||
:param address: Value to verify
|
||||
:type address: string
|
||||
:returns: bool
|
||||
|
||||
.. versionadded:: 3.8
|
||||
"""
|
||||
try:
|
||||
# Validate the correct CIDR Address
|
||||
netaddr.IPNetwork(address)
|
||||
except netaddr.AddrFormatError:
|
||||
return False
|
||||
|
||||
# Prior validation partially verify /xx part
|
||||
# Verify it here
|
||||
ip_segment = address.split('/')
|
||||
|
||||
if (len(ip_segment) <= 1 or
|
||||
ip_segment[1] == ''):
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
|
||||
def get_ipv6_addr_by_EUI64(prefix, mac):
|
||||
"""Calculate IPv6 address using EUI-64 specification.
|
||||
|
||||
|
@ -180,6 +180,17 @@ class NetworkUtilsTest(test_base.BaseTestCase):
|
||||
|
||||
self.assertFalse(netutils.is_valid_ip(''))
|
||||
|
||||
def test_is_valid_cidr(self):
|
||||
self.assertTrue(netutils.is_valid_cidr('10.0.0.0/24'))
|
||||
self.assertTrue(netutils.is_valid_cidr('10.0.0.1/32'))
|
||||
self.assertTrue(netutils.is_valid_cidr('0.0.0.0/0'))
|
||||
self.assertTrue(netutils.is_valid_cidr('2600::/64'))
|
||||
self.assertTrue(netutils.is_valid_cidr(
|
||||
'0000:0000:0000:0000:0000:0000:0000:0001/32'))
|
||||
|
||||
self.assertFalse(netutils.is_valid_cidr('10.0.0.1'))
|
||||
self.assertFalse(netutils.is_valid_cidr('10.0.0.1/33'))
|
||||
|
||||
def test_valid_port(self):
|
||||
valid_inputs = [1, '1', 2, '3', '5', 8, 13, 21,
|
||||
'80', '3246', '65535']
|
||||
|
Loading…
x
Reference in New Issue
Block a user