From 426a9e9b67dfe66a0c5e41d862c2083051c90802 Mon Sep 17 00:00:00 2001 From: "Kai Qiang Wu(Kennan)" Date: Fri, 15 May 2015 17:22:11 +0800 Subject: [PATCH] Fix the ipaddress validate issue Right now the jenkins jobs all failed with ipv4address validate. We make ipaddress check logic use netaddr If wsme have better IPv4AddressType changes or magnum api types usage was not right, we can change latter. Right now, we should make magnum work. For netaddr it has been include in global-requirements For wsme IPv4AddressType, check ttps://github.com/stackforge/wsme/blob/master/wsme/types.py Closes-Bug: #1455405 Co-Authored-By: Davanum Srinivas Change-Id: I67e6c9e0377103bf5b0cd4d120c146ef2366f440 --- magnum/api/controllers/v1/baymodel.py | 2 +- magnum/api/controllers/v1/types.py | 16 ++++++++++++++++ requirements.txt | 1 + 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/magnum/api/controllers/v1/baymodel.py b/magnum/api/controllers/v1/baymodel.py index df35cb72df..73fbf9339c 100644 --- a/magnum/api/controllers/v1/baymodel.py +++ b/magnum/api/controllers/v1/baymodel.py @@ -72,7 +72,7 @@ class BayModel(base.APIBase): master_flavor_id = wtypes.StringType(min_length=1, max_length=255) """The flavor of the master node for this bay model""" - dns_nameserver = wtypes.IPv4AddressType() + dns_nameserver = types.IPv4AddressType() """The DNS nameserver address""" keypair_id = wsme.wsattr(wtypes.StringType(min_length=1, max_length=255), diff --git a/magnum/api/controllers/v1/types.py b/magnum/api/controllers/v1/types.py index ace920b505..c5540d507d 100644 --- a/magnum/api/controllers/v1/types.py +++ b/magnum/api/controllers/v1/types.py @@ -15,7 +15,9 @@ # License for the specific language governing permissions and limitations # under the License. +import netaddr from oslo_utils import strutils +import six import wsme from wsme import types as wtypes @@ -203,3 +205,17 @@ class JsonPatchType(wtypes.Base): if patch.value: ret['value'] = patch.value return ret + + +class IPv4AddressType(wtypes.UserType): + """A simple IPv4 type.""" + + basetype = six.string_types + name = "ipv4address" + + @staticmethod + def validate(value): + try: + netaddr.IPAddress(value, version=4, flags=netaddr.INET_PTON) + except netaddr.AddrFormatError as e: + raise ValueError(six.text_type(e)) diff --git a/requirements.txt b/requirements.txt index f68a91649c..8ccc0d3401 100644 --- a/requirements.txt +++ b/requirements.txt @@ -27,3 +27,4 @@ taskflow>=0.7.1 WSME>=0.6 docker-py>=1.1.0 # Apache-2.0 jsonpatch>=1.1 +netaddr>=0.7.12