Use is_valid_ipv4 and is_valid_ipv6 from oslo.utils
In module netaddr valid_ipv4/valid_ipv6 raise exception AddrFormatError if address is specified as empty string. Methods is_valid_ipv4/is_valid_ipv6 catch the exception and return boolean value. So we'd better use them from oslo.utils. This commit also adds empty string case in unit test. Change-Id: If58889bb322686c252b4fe89a4ddf9469121028c
This commit is contained in:
parent
48ba5fc117
commit
cd698f2664
@ -25,6 +25,7 @@ import netaddr
|
|||||||
from oslo_config import cfg
|
from oslo_config import cfg
|
||||||
from oslo_log import log
|
from oslo_log import log
|
||||||
from oslo_utils import importutils
|
from oslo_utils import importutils
|
||||||
|
from oslo_utils import netutils
|
||||||
import six
|
import six
|
||||||
|
|
||||||
from manila.common import constants as const
|
from manila.common import constants as const
|
||||||
@ -257,13 +258,13 @@ class ServiceInstanceManager(object):
|
|||||||
self.admin_context,
|
self.admin_context,
|
||||||
self.get_config_option('service_instance_name_or_id'))
|
self.get_config_option('service_instance_name_or_id'))
|
||||||
|
|
||||||
if netaddr.valid_ipv4(data['service_net_name_or_ip']):
|
if netutils.is_valid_ipv4(data['service_net_name_or_ip']):
|
||||||
data['private_address'] = [data['service_net_name_or_ip']]
|
data['private_address'] = [data['service_net_name_or_ip']]
|
||||||
else:
|
else:
|
||||||
data['private_address'] = self._get_addresses_by_network_name(
|
data['private_address'] = self._get_addresses_by_network_name(
|
||||||
data['service_net_name_or_ip'], data['instance'])
|
data['service_net_name_or_ip'], data['instance'])
|
||||||
|
|
||||||
if netaddr.valid_ipv4(data['tenant_net_name_or_ip']):
|
if netutils.is_valid_ipv4(data['tenant_net_name_or_ip']):
|
||||||
data['public_address'] = [data['tenant_net_name_or_ip']]
|
data['public_address'] = [data['tenant_net_name_or_ip']]
|
||||||
else:
|
else:
|
||||||
data['public_address'] = self._get_addresses_by_network_name(
|
data['public_address'] = self._get_addresses_by_network_name(
|
||||||
@ -285,7 +286,7 @@ class ServiceInstanceManager(object):
|
|||||||
for key in ('private_address', 'public_address'):
|
for key in ('private_address', 'public_address'):
|
||||||
data[key + '_v4'] = None
|
data[key + '_v4'] = None
|
||||||
for address in data[key]:
|
for address in data[key]:
|
||||||
if netaddr.valid_ipv4(address):
|
if netutils.is_valid_ipv4(address):
|
||||||
data[key + '_v4'] = address
|
data[key + '_v4'] = address
|
||||||
break
|
break
|
||||||
share_server['ip'] = data['private_address_v4']
|
share_server['ip'] = data['private_address_v4']
|
||||||
|
@ -661,7 +661,7 @@ class IsValidIPVersion(test.TestCase):
|
|||||||
for vers in (6, '6'):
|
for vers in (6, '6'):
|
||||||
self.assertFalse(utils.is_valid_ip_address(addr, vers))
|
self.assertFalse(utils.is_valid_ip_address(addr, vers))
|
||||||
|
|
||||||
@ddt.data(None, 'fake', '255.255.255.256', '2001:cdba::3257:9652')
|
@ddt.data(None, 'fake', '255.255.255.256', '2001:cdba::3257:9652', '')
|
||||||
def test_provided_invalid_v4_address(self, addr):
|
def test_provided_invalid_v4_address(self, addr):
|
||||||
for vers in (4, '4'):
|
for vers in (4, '4'):
|
||||||
self.assertFalse(utils.is_valid_ip_address(addr, vers))
|
self.assertFalse(utils.is_valid_ip_address(addr, vers))
|
||||||
|
@ -38,6 +38,7 @@ from oslo_concurrency import processutils
|
|||||||
from oslo_config import cfg
|
from oslo_config import cfg
|
||||||
from oslo_log import log
|
from oslo_log import log
|
||||||
from oslo_utils import importutils
|
from oslo_utils import importutils
|
||||||
|
from oslo_utils import netutils
|
||||||
from oslo_utils import timeutils
|
from oslo_utils import timeutils
|
||||||
import paramiko
|
import paramiko
|
||||||
import retrying
|
import retrying
|
||||||
@ -501,9 +502,9 @@ def cidr_to_netmask(cidr):
|
|||||||
|
|
||||||
def is_valid_ip_address(ip_address, ip_version):
|
def is_valid_ip_address(ip_address, ip_version):
|
||||||
if int(ip_version) == 4:
|
if int(ip_version) == 4:
|
||||||
return netaddr.valid_ipv4(ip_address)
|
return netutils.is_valid_ipv4(ip_address)
|
||||||
elif int(ip_version) == 6:
|
elif int(ip_version) == 6:
|
||||||
return netaddr.valid_ipv6(ip_address)
|
return netutils.is_valid_ipv6(ip_address)
|
||||||
else:
|
else:
|
||||||
raise exception.ManilaException(
|
raise exception.ManilaException(
|
||||||
_("Provided improper IP version '%s'.") % ip_version)
|
_("Provided improper IP version '%s'.") % ip_version)
|
||||||
|
Loading…
Reference in New Issue
Block a user