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
simplegeneric
pytz
netaddr>=0.7.12

View File

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

View File

@ -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()

View File

@ -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: