From cd698f2664edd91cda819bef6a29ca23276d8db8 Mon Sep 17 00:00:00 2001 From: "ChangBo Guo(gcb)" Date: Wed, 8 Jun 2016 15:19:20 +0800 Subject: [PATCH] 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 --- manila/share/drivers/service_instance.py | 7 ++++--- manila/tests/test_utils.py | 2 +- manila/utils.py | 5 +++-- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/manila/share/drivers/service_instance.py b/manila/share/drivers/service_instance.py index 98bdc931..f17ad6e2 100644 --- a/manila/share/drivers/service_instance.py +++ b/manila/share/drivers/service_instance.py @@ -25,6 +25,7 @@ import netaddr from oslo_config import cfg from oslo_log import log from oslo_utils import importutils +from oslo_utils import netutils import six from manila.common import constants as const @@ -257,13 +258,13 @@ class ServiceInstanceManager(object): self.admin_context, 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']] else: data['private_address'] = self._get_addresses_by_network_name( 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']] else: data['public_address'] = self._get_addresses_by_network_name( @@ -285,7 +286,7 @@ class ServiceInstanceManager(object): for key in ('private_address', 'public_address'): data[key + '_v4'] = None for address in data[key]: - if netaddr.valid_ipv4(address): + if netutils.is_valid_ipv4(address): data[key + '_v4'] = address break share_server['ip'] = data['private_address_v4'] diff --git a/manila/tests/test_utils.py b/manila/tests/test_utils.py index 1c885314..237001c7 100644 --- a/manila/tests/test_utils.py +++ b/manila/tests/test_utils.py @@ -661,7 +661,7 @@ class IsValidIPVersion(test.TestCase): for vers in (6, '6'): 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): for vers in (4, '4'): self.assertFalse(utils.is_valid_ip_address(addr, vers)) diff --git a/manila/utils.py b/manila/utils.py index cab18a9c..5a77c01f 100644 --- a/manila/utils.py +++ b/manila/utils.py @@ -38,6 +38,7 @@ from oslo_concurrency import processutils from oslo_config import cfg from oslo_log import log from oslo_utils import importutils +from oslo_utils import netutils from oslo_utils import timeutils import paramiko import retrying @@ -501,9 +502,9 @@ def cidr_to_netmask(cidr): def is_valid_ip_address(ip_address, ip_version): if int(ip_version) == 4: - return netaddr.valid_ipv4(ip_address) + return netutils.is_valid_ipv4(ip_address) elif int(ip_version) == 6: - return netaddr.valid_ipv6(ip_address) + return netutils.is_valid_ipv6(ip_address) else: raise exception.ManilaException( _("Provided improper IP version '%s'.") % ip_version)