Allow boot server with multiple nics

Trying to boot a server while specifying multiple
--nic parameters leads to an error: Invalid nic
argument.
This patch fixes it.

Change-Id: I662fd366df8e79db1966d45a9e090087dbace4b0
Closes-Bug: #1706597
This commit is contained in:
huangtianhua 2017-08-09 14:39:51 +08:00
parent aec93636f6
commit 0a3cf89c67
3 changed files with 36 additions and 9 deletions

View File

@ -695,6 +695,29 @@ class ShellTest(utils.TestCase):
},
)
def test_boot_with_multiple_nics(self):
cmd = ('boot --image %s --flavor 1 '
'--nic net-id=net_a,v4-fixed-ip=10.0.0.1 '
'--nic net-id=net_b some-server' %
FAKE_UUID_1)
self.run_command(cmd)
self.assert_called_anytime(
'POST', '/servers',
{
'server': {
'flavorRef': '1',
'name': 'some-server',
'imageRef': FAKE_UUID_1,
'min_count': 1,
'max_count': 1,
'networks': [
{'uuid': 'net_a', 'fixed_ip': '10.0.0.1'},
{'uuid': 'net_b'}
],
},
},
)
def test_boot_nics_with_tag(self):
cmd = ('boot --image %s --flavor 1 '
'--nic net-id=a=c,v4-fixed-ip=10.0.0.1,tag=foo some-server' %

View File

@ -268,12 +268,11 @@ def _parse_nics(cs, args):
supports_auto_alloc = cs.api_version >= api_versions.APIVersion('2.37')
supports_nic_tags = _supports_nic_tags(cs)
nic_info = {"net-id": "", "v4-fixed-ip": "", "v6-fixed-ip": "",
"port-id": "", "net-name": ""}
nic_keys = {'net-id', 'v4-fixed-ip', 'v6-fixed-ip', 'port-id', 'net-name'}
if supports_auto_alloc and supports_nic_tags:
# API version >= 2.42
nic_info.update({"tag": ""})
nic_keys.add('tag')
err_msg = (_("Invalid nic argument '%s'. Nic arguments must be of "
"the form --nic <auto,none,net-id=net-uuid,"
"net-name=network-name,v4-fixed-ip=ip-addr,"
@ -292,7 +291,7 @@ def _parse_nics(cs, args):
"be used with any other --nic value."))
elif not supports_auto_alloc and supports_nic_tags:
# 2.36 >= API version >= 2.32
nic_info.update({"tag": ""})
nic_keys.add('tag')
err_msg = (_("Invalid nic argument '%s'. Nic arguments must be of "
"the form --nic <net-id=net-uuid,"
"net-name=network-name,v4-fixed-ip=ip-addr,"
@ -310,6 +309,7 @@ def _parse_nics(cs, args):
auto_or_none = False
nics = []
for nic_str in args.nics:
nic_info = {}
nic_info_set = False
for kv_str in nic_str.split(","):
if auto_or_none:
@ -341,13 +341,13 @@ def _parse_nics(cs, args):
except ValueError:
raise exceptions.CommandError(err_msg % nic_str)
if k in nic_info:
if k in nic_keys:
# if user has given a net-name resolve it to network ID
if k == 'net-name':
k = 'net-id'
v = _find_network_id(cs, v)
# if some argument was given multiple times
if nic_info[k]:
if k in nic_info:
raise exceptions.CommandError(err_msg % nic_str)
nic_info[k] = v
nic_info_set = True
@ -357,15 +357,15 @@ def _parse_nics(cs, args):
if auto_or_none:
continue
if nic_info['v4-fixed-ip'] and not netutils.is_valid_ipv4(
if 'v4-fixed-ip' in nic_info and not netutils.is_valid_ipv4(
nic_info['v4-fixed-ip']):
raise exceptions.CommandError(_("Invalid ipv4 address."))
if nic_info['v6-fixed-ip'] and not netutils.is_valid_ipv6(
if 'v6-fixed-ip' in nic_info and not netutils.is_valid_ipv6(
nic_info['v6-fixed-ip']):
raise exceptions.CommandError(_("Invalid ipv6 address."))
if bool(nic_info['net-id']) == bool(nic_info['port-id']):
if bool(nic_info.get('net-id')) == bool(nic_info.get('port-id')):
raise exceptions.CommandError(err_msg % nic_str)
nics.append(nic_info)

View File

@ -0,0 +1,4 @@
---
fixes:
- Fix an ability to boot server with multiple nics which was broken with
microversion 2.42 (fix tag attribute disappearing).