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
This commit is contained in:
Hongbin Lu 2018-03-05 03:46:28 +00:00
parent c95b0d4c02
commit 4bc01dfb7f
2 changed files with 10 additions and 11 deletions

View File

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

View File

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