fix handling of nics argument

The existing code was receiving a list of strings and erroneously
assuming it was being given a list of dictionaries, leading it to fail
with:

    AttributeError: 'str' object has no attribute 'get'

This commit corrects the list handling code to check the type of each
item and handle it appropriately.   Also, based on bcoca's comment
in #2253, thie code removes the special case for a string-only argument.

By transforming string arguments into dicts and then handling them like
any other dict argument, this also permits arguments of the form:

    nics: net-name=mynet

Or:

    nics: port-name=mynet

Previous versions of this code only supported `net-id` and `port-id` in
string specifications.
This commit is contained in:
Lars Kellogg-Stedman 2015-10-14 16:27:04 -04:00 committed by Matt Clay
parent db59af564e
commit 413a9d0c62

View file

@ -86,8 +86,8 @@ options:
- A list of networks to which the instance's interface should - A list of networks to which the instance's interface should
be attached. Networks may be referenced by net-id/net-name/port-id be attached. Networks may be referenced by net-id/net-name/port-id
or port-name. or port-name.
- 'Also this accepts a string containing a list of net-id/port-id. - 'Also this accepts a string containing a list of (net/port)-(id/name)
Eg: nics: "net-id=uuid-1,net-id=uuid-2"' Eg: nics: "net-id=uuid-1,port-name=myport"'
required: false required: false
default: None default: None
auto_ip: auto_ip:
@ -288,35 +288,37 @@ def _exit_hostvars(module, cloud, server, changed=True):
changed=changed, server=server, id=server.id, openstack=hostvars) changed=changed, server=server, id=server.id, openstack=hostvars)
def _parse_nics(nics):
for net in nics:
if type(net) == str:
for nic in net.split(','):
yield dict((nic.split('='),))
else:
yield net
def _network_args(module, cloud): def _network_args(module, cloud):
args = [] args = []
nics = module.params['nics'] nics = module.params['nics']
if type(nics) == str :
for kv_str in nics.split(","): for net in _parse_nics(nics):
nic = {} if net.get('net-id'):
k, v = kv_str.split("=") args.append(net)
nic[k] = v elif net.get('net-name'):
args.append(nic) by_name = cloud.get_network(net['net-name'])
else: if not by_name:
for net in module.params['nics']: module.fail_json(
if net.get('net-id'): msg='Could not find network by net-name: %s' %
args.append(net) net['net-name'])
elif net.get('net-name'): args.append({'net-id': by_name['id']})
by_name = cloud.get_network(net['net-name']) elif net.get('port-id'):
if not by_name: args.append(net)
module.fail_json( elif net.get('port-name'):
msg='Could not find network by net-name: %s' % by_name = cloud.get_port(net['port-name'])
net['net-name']) if not by_name:
args.append({'net-id': by_name['id']}) module.fail_json(
elif net.get('port-id'): msg='Could not find port by port-name: %s' %
args.append(net) net['port-name'])
elif net.get('port-name'): args.append({'port-id': by_name['id']})
by_name = cloud.get_port(net['port-name'])
if not by_name:
module.fail_json(
msg='Could not find port by port-name: %s' %
net['port-name'])
args.append({'port-id': by_name['id']})
return args return args