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
This commit is contained in:
Lan Qi song 2015-05-15 17:32:39 +08:00
parent d60de97edc
commit 002473c0ea
4 changed files with 11 additions and 10 deletions

View File

@ -2,3 +2,4 @@ six>=1.9.0
WebOb>=1.2.3 WebOb>=1.2.3
simplegeneric simplegeneric
pytz pytz
netaddr>=0.7.12

View File

@ -1,5 +1,5 @@
six>=1.9.0 six>=1.9.0
WebOb>=1.2.3 WebOb>=1.2.3
simplegeneric simplegeneric
ipaddr
pytz pytz
netaddr>=0.7.12

View File

@ -339,20 +339,24 @@ Value: 'v3'. Value should be one of: v., v.",
v = types.IPv4AddressType() v = types.IPv4AddressType()
self.assertEqual(v.validate('127.0.0.1'), '127.0.0.1') 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('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, '')
self.assertRaises(ValueError, v.validate, 'foo') self.assertRaises(ValueError, v.validate, 'foo')
self.assertRaises(ValueError, v.validate, self.assertRaises(ValueError, v.validate,
'2001:0db8:bd05:01d2:288a:1fc0:0001:10ee') '2001:0db8:bd05:01d2:288a:1fc0:0001:10ee')
self.assertRaises(ValueError, v.validate, '1.2.3')
def test_validate_ipv6_address_type(self): def test_validate_ipv6_address_type(self):
v = types.IPv6AddressType() v = types.IPv6AddressType()
self.assertEqual(v.validate('0:0:0:0:0:0:0:1'), self.assertEqual(v.validate('0:0:0:0:0:0:0:1'),
'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'), self.assertEqual(v.validate('2001:0db8:bd05:01d2:288a:1fc0:0001:10ee'),
'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, '')
self.assertRaises(ValueError, v.validate, 'foo') self.assertRaises(ValueError, v.validate, 'foo')
self.assertRaises(ValueError, v.validate, '192.168.0.1') 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): def test_validate_uuid_type(self):
v = types.UuidType() v = types.UuidType()

View File

@ -3,17 +3,13 @@ import datetime
import decimal import decimal
import inspect import inspect
import logging import logging
import netaddr
import re import re
import six import six
import sys import sys
import uuid import uuid
import weakref import weakref
try:
import ipaddress
except ImportError:
import ipaddr as ipaddress
from wsme import exc from wsme import exc
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@ -234,8 +230,8 @@ class IPv4AddressType(UserType):
@staticmethod @staticmethod
def validate(value): def validate(value):
try: try:
ipaddress.IPv4Address(value) netaddr.IPAddress(value, version=4, flags=netaddr.INET_PTON)
except ipaddress.AddressValueError: except netaddr.AddrFormatError:
error = 'Value should be IPv4 format' error = 'Value should be IPv4 format'
raise ValueError(error) raise ValueError(error)
else: else:
@ -254,8 +250,8 @@ class IPv6AddressType(UserType):
@staticmethod @staticmethod
def validate(value): def validate(value):
try: try:
ipaddress.IPv6Address(value) netaddr.IPAddress(value, version=6, flags=netaddr.INET_PTON)
except ipaddress.AddressValueError: except netaddr.AddrFormatError:
error = 'Value should be IPv6 format' error = 'Value should be IPv6 format'
raise ValueError(error) raise ValueError(error)
else: else: