From 4bc01dfb7fc07bc9f20e3f62d2b2f72a15e79081 Mon Sep 17 00:00:00 2001 From: Hongbin Lu Date: Mon, 5 Mar 2018 03:46:28 +0000 Subject: [PATCH] Avoid empty values in 'nets' options The python-zunclient sends the 'nets' parameter to server with empty string. For example: * {'network': xxx, 'port': '', 'v4-fixed-ip': '', 'v6-fixed-ip': ''} * {'network': '', 'port': xxx, 'v4-fixed-ip': '', 'v6-fixed-ip': ''} This patch changes it to: * {'network': xxx} * {'port': xxx} The new form doesn't contain a key with empty string value. This allows a better validation on the server side in the future. Change-Id: Iebb3ddb53e5e9d31175e6e7eeb170a66a8630a17 --- zunclient/common/utils.py | 14 +++++++------- zunclient/tests/unit/common/test_utils.py | 7 +++---- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/zunclient/common/utils.py b/zunclient/common/utils.py index 242fafc0..92d7e454 100644 --- a/zunclient/common/utils.py +++ b/zunclient/common/utils.py @@ -237,8 +237,8 @@ def parse_nets(ns): "with only one of network, or port specified.") nets = [] for net_str in ns: - net_info = {"network": "", "v4-fixed-ip": "", "v6-fixed-ip": "", - "port": ""} + keys = ["network", "port", "v4-fixed-ip", "v6-fixed-ip"] + net_info = {} for kv_str in net_str.split(","): try: k, v = kv_str.split("=", 1) @@ -246,22 +246,22 @@ def parse_nets(ns): v = v.strip() except ValueError: raise apiexec.CommandError(err_msg % net_str) - if k in net_info: - if net_info[k]: + if k in keys: + if net_info.get(k): raise apiexec.CommandError(err_msg % net_str) net_info[k] = v else: raise apiexec.CommandError(err_msg % net_str) - if net_info['v4-fixed-ip'] and not netutils.is_valid_ipv4( + if net_info.get('v4-fixed-ip') and not netutils.is_valid_ipv4( net_info['v4-fixed-ip']): raise apiexec.CommandError("Invalid ipv4 address.") - if net_info['v6-fixed-ip'] and not netutils.is_valid_ipv6( + if net_info.get('v6-fixed-ip') and not netutils.is_valid_ipv6( net_info['v6-fixed-ip']): raise apiexec.CommandError("Invalid ipv6 address.") - if bool(net_info['network']) == bool(net_info['port']): + if bool(net_info.get('network')) == bool(net_info.get('port')): raise apiexec.CommandError(err_msg % net_str) nets.append(net_info) diff --git a/zunclient/tests/unit/common/test_utils.py b/zunclient/tests/unit/common/test_utils.py index b7824762..b98110a7 100644 --- a/zunclient/tests/unit/common/test_utils.py +++ b/zunclient/tests/unit/common/test_utils.py @@ -220,14 +220,13 @@ class ParseNetsTest(test_utils.BaseTestCase): def test_nets_with_network(self): nets = [' network = 1234567 , v4-fixed-ip = 172.17.0.3 '] result = utils.parse_nets(nets) - self.assertEqual([{'network': '1234567', 'v4-fixed-ip': '172.17.0.3', - 'port': '', 'v6-fixed-ip': ''}], result) + self.assertEqual([{'network': '1234567', 'v4-fixed-ip': '172.17.0.3'}], + result) def test_nets_with_port(self): nets = ['port=1234567, v6-fixed-ip=2001:db8::2'] result = utils.parse_nets(nets) - self.assertEqual([{'network': '', 'v4-fixed-ip': '', - 'port': '1234567', 'v6-fixed-ip': '2001:db8::2'}], + self.assertEqual([{'port': '1234567', 'v6-fixed-ip': '2001:db8::2'}], result) def test_nets_with_only_ip(self):