From 002473c0eaff5bde539e6f6980c387d3817f7063 Mon Sep 17 00:00:00 2001 From: Lan Qi song Date: Fri, 15 May 2015 17:32:39 +0800 Subject: [PATCH] Move ipaddr to netaddr Ipaddr module was never introduced by openstack and never used by other openstack components. We should move to more reliable module netaddr to validate ip format. Change-Id: I38d0c6f4ea2147ce071ab62a7c9d546436aec185 Closes-Bug: #1455404 --- requirements-py3.txt | 1 + requirements.txt | 2 +- wsme/tests/test_types.py | 4 ++++ wsme/types.py | 14 +++++--------- 4 files changed, 11 insertions(+), 10 deletions(-) diff --git a/requirements-py3.txt b/requirements-py3.txt index 84d9dc6..d15bd16 100644 --- a/requirements-py3.txt +++ b/requirements-py3.txt @@ -2,3 +2,4 @@ six>=1.9.0 WebOb>=1.2.3 simplegeneric pytz +netaddr>=0.7.12 diff --git a/requirements.txt b/requirements.txt index 8662b14..d15bd16 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,5 @@ six>=1.9.0 WebOb>=1.2.3 simplegeneric -ipaddr pytz +netaddr>=0.7.12 diff --git a/wsme/tests/test_types.py b/wsme/tests/test_types.py index 3aa98ca..c11ba34 100644 --- a/wsme/tests/test_types.py +++ b/wsme/tests/test_types.py @@ -339,20 +339,24 @@ Value: 'v3'. Value should be one of: v., v.", v = types.IPv4AddressType() self.assertEqual(v.validate('127.0.0.1'), '127.0.0.1') self.assertEqual(v.validate('192.168.0.1'), '192.168.0.1') + self.assertEqual(v.validate(u'8.8.1.1'), u'8.8.1.1') self.assertRaises(ValueError, v.validate, '') self.assertRaises(ValueError, v.validate, 'foo') self.assertRaises(ValueError, v.validate, '2001:0db8:bd05:01d2:288a:1fc0:0001:10ee') + self.assertRaises(ValueError, v.validate, '1.2.3') def test_validate_ipv6_address_type(self): v = types.IPv6AddressType() self.assertEqual(v.validate('0:0:0:0:0:0:0:1'), '0:0:0:0:0:0:0:1') + self.assertEqual(v.validate(u'0:0:0:0:0:0:0:1'), u'0:0:0:0:0:0:0:1') self.assertEqual(v.validate('2001:0db8:bd05:01d2:288a:1fc0:0001:10ee'), '2001:0db8:bd05:01d2:288a:1fc0:0001:10ee') self.assertRaises(ValueError, v.validate, '') self.assertRaises(ValueError, v.validate, 'foo') self.assertRaises(ValueError, v.validate, '192.168.0.1') + self.assertRaises(ValueError, v.validate, '0:0:0:0:0:0:1') def test_validate_uuid_type(self): v = types.UuidType() diff --git a/wsme/types.py b/wsme/types.py index ac16694..77404e5 100644 --- a/wsme/types.py +++ b/wsme/types.py @@ -3,17 +3,13 @@ import datetime import decimal import inspect import logging +import netaddr import re import six import sys import uuid import weakref -try: - import ipaddress -except ImportError: - import ipaddr as ipaddress - from wsme import exc log = logging.getLogger(__name__) @@ -234,8 +230,8 @@ class IPv4AddressType(UserType): @staticmethod def validate(value): try: - ipaddress.IPv4Address(value) - except ipaddress.AddressValueError: + netaddr.IPAddress(value, version=4, flags=netaddr.INET_PTON) + except netaddr.AddrFormatError: error = 'Value should be IPv4 format' raise ValueError(error) else: @@ -254,8 +250,8 @@ class IPv6AddressType(UserType): @staticmethod def validate(value): try: - ipaddress.IPv6Address(value) - except ipaddress.AddressValueError: + netaddr.IPAddress(value, version=6, flags=netaddr.INET_PTON) + except netaddr.AddrFormatError: error = 'Value should be IPv6 format' raise ValueError(error) else: