enhance network_id_to_name to accept list as well
This commit is contained in:
parent
c0ac6d0702
commit
e0d37d760a
2 changed files with 18 additions and 4 deletions
|
@ -62,17 +62,28 @@ def network_name_to_id(host_vars, user, password, tenant, auth_url):
|
|||
raise errors.AnsibleFilterError('There is no network of name {0} accessible for tenant {1}'.format(host_vars, tenant))
|
||||
|
||||
def network_id_to_name(host_vars, user, password, tenant, auth_url):
|
||||
""" Accept one id of network or list of ids of networks and return the same
|
||||
structure, but ids replaced by name of the network(s). """
|
||||
auth = identity.Password(auth_url=auth_url, username=user,
|
||||
password=password, tenant_name=tenant)
|
||||
sess = session.Session(auth=auth)
|
||||
token = auth.get_token(sess)
|
||||
endpoint = auth.get_endpoint(sess, service_name='neutron', service_type='network')
|
||||
neutron = NeutronClient('2.0', endpoint_url=endpoint, token=token)
|
||||
networks = neutron.list_networks(id=host_vars, fields='name')["networks"]
|
||||
if networks:
|
||||
return networks[0]['name']
|
||||
result_as_list = isinstance(host_vars, list)
|
||||
if not result_as_list:
|
||||
host_vars = [host_vars]
|
||||
result = []
|
||||
for net in host_vars:
|
||||
networks = neutron.list_networks(id=net, fields='name')["networks"]
|
||||
if networks:
|
||||
result += [networks[0]['name']]
|
||||
else:
|
||||
raise errors.AnsibleFilterError('There is no network of id {0} accessible for tenant {1}'.format(host_vars, tenant))
|
||||
if result_as_list:
|
||||
return result
|
||||
else:
|
||||
raise errors.AnsibleFilterError('There is no network of id {0} accessible for tenant {1}'.format(host_vars, tenant))
|
||||
return result[0]
|
||||
|
||||
class FilterModule (object):
|
||||
def filters(self):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue