Skip to content

Commit

Permalink
Merge pull request #40753 from arno01/dockerng-enhanced
Browse files Browse the repository at this point in the history
dockerng: avoid network duplication and add driver_opts
  • Loading branch information
Mike Place committed Apr 21, 2017
2 parents 12345de + 4c29c5f commit 8652ad6
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 7 deletions.
14 changes: 12 additions & 2 deletions salt/modules/dockermod.py
Expand Up @@ -3939,7 +3939,9 @@ def networks(names=None, ids=None):
return response


def create_network(name, driver=None):
def create_network(name,
driver=None,
driver_opts=None):
'''
Create a new network
Expand All @@ -3949,13 +3951,21 @@ def create_network(name, driver=None):
driver
Driver of the network
driver_opts
Options for the network driver.
CLI Example:
.. code-block:: bash
salt myminion docker.create_network web_network driver=bridge
'''
response = _client_wrapper('create_network', name, driver=driver)
response = _client_wrapper('create_network',
name,
driver=driver,
options=driver_opts,
check_duplicate=True)

_clear_context()
# Only non-error return case is a True return, so just return the response
return response
Expand Down
23 changes: 21 additions & 2 deletions salt/states/docker_network.py
Expand Up @@ -33,6 +33,9 @@
from __future__ import absolute_import
import logging

# Import salt libs
import salt.utils

# Enable proper logging
log = logging.getLogger(__name__) # pylint: disable=invalid-name

Expand All @@ -49,7 +52,10 @@ def __virtual__():
return (False, __salt__.missing_fun_string('docker.version'))


def present(name, driver=None, containers=None):
def present(name,
driver=None,
driver_opts=None,
containers=None):
'''
Ensure that a network is present.
Expand All @@ -59,6 +65,9 @@ def present(name, driver=None, containers=None):
driver
Type of driver for that network.
driver_opts
Options for the network driver.
containers:
List of container names that should be part of this network
Expand All @@ -75,6 +84,8 @@ def present(name, driver=None, containers=None):
network_bar:
docker_network.present
- name: bar
- driver_opts:
- com.docker.network.driver.mtu: "1450"
- containers:
- cont1
- cont2
Expand All @@ -84,6 +95,10 @@ def present(name, driver=None, containers=None):
'changes': {},
'result': False,
'comment': ''}

if salt.utils.is_dictlist(driver_opts):
driver_opts = salt.utils.repack_dictlist(driver_opts)

if containers is None:
containers = []
# map containers to container's Ids.
Expand All @@ -110,7 +125,11 @@ def present(name, driver=None, containers=None):
else:
try:
ret['changes']['created'] = __salt__['docker.create_network'](
name, driver=driver)
name,
driver=driver,
driver_opts=driver_opts,
check_duplicate=True)

except Exception as exc:
ret['comment'] = ('Failed to create network \'{0}\': {1}'
.format(name, exc))
Expand Down
9 changes: 7 additions & 2 deletions tests/unit/modules/test_dockermod.py
Expand Up @@ -182,8 +182,13 @@ def test_create_network(self, *args):
with patch.dict(docker_mod.__dict__,
{'__salt__': __salt__}):
with patch.object(docker_mod, '_get_client', get_client_mock):
docker_mod.create_network('foo', driver='bridge')
client.create_network.assert_called_once_with('foo', driver='bridge')
docker_mod.create_network('foo',
driver='bridge',
driver_opts={})
client.create_network.assert_called_once_with('foo',
driver='bridge',
options={},
check_duplicate=True)

@skipIf(docker_version < (1, 5, 0),
'docker module must be installed to run this test or is too old. >=1.5.0')
Expand Down
5 changes: 4 additions & 1 deletion tests/unit/states/test_docker_network.py
Expand Up @@ -54,7 +54,10 @@ def test_present(self):
'network_foo',
containers=['container'],
)
docker_create_network.assert_called_with('network_foo', driver=None)
docker_create_network.assert_called_with('network_foo',
driver=None,
driver_opts=None,
check_duplicate=True)
docker_connect_container_to_network.assert_called_with('abcd',
'network_foo')
self.assertEqual(ret, {'name': 'network_foo',
Expand Down

0 comments on commit 8652ad6

Please sign in to comment.