Fix dockerng.network_* name matching

Updating the states so that they loop through the list of networks
returned from `dockerng.networks` to check that one of the networks
returned fully matches the name of the network that needs to be ensured
to be present or absent.

Another option would be to add an argument to the `dockerng.networks`
execution module to enable strict matching.  This would probably be
preferable to repeating the logic in the states, but I didn't want to
meddle too much as the execution module is currently just wrapping the
docker-py library.

Fixes #41982
This commit is contained in:
Andrew Bulford 2017-06-28 17:28:22 +01:00
parent 82b1eb28ab
commit 515c612808

View file

@ -2242,8 +2242,17 @@ def network_present(name, driver=None, containers=None):
# map containers to container's Ids.
containers = [__salt__['dockerng.inspect_container'](c)['Id'] for c in containers]
networks = __salt__['dockerng.networks'](names=[name])
# networks will contain all Docker networks which partially match 'name'.
# We need to loop through to find the matching network, if there is one.
network = None
if networks:
network = networks[0] # we expect network's name to be unique
for network_iter in networks:
if network_iter['Name'] == name:
network = network_iter
break
if network is not None:
if all(c in network['Containers'] for c in containers):
ret['result'] = True
ret['comment'] = 'Network \'{0}\' already exists.'.format(name)
@ -2302,7 +2311,17 @@ def network_absent(name, driver=None):
'comment': ''}
networks = __salt__['dockerng.networks'](names=[name])
if not networks:
# networks will contain all Docker networks which partially match 'name'.
# We need to loop through to find the matching network, if there is one.
network = None
if networks:
for network_iter in networks:
if network_iter['Name'] == name:
network = network_iter
break
if network is None:
ret['result'] = True
ret['comment'] = 'Network \'{0}\' already absent'.format(name)
return ret