Expose docker networking as state

This commit is contained in:
Nicolas Delaby 2015-11-24 13:54:59 +01:00
parent 94135d91c3
commit e98d18ba41
3 changed files with 168 additions and 3 deletions

View file

@ -175,8 +175,8 @@ Functions
<salt.modules.dockerng.inspect_network>`
- :py:func:`dockerng.connect_container_to_network
<salt.modules.dockerng.connect_container_to_network>`
- :py:func:`dockerng.disconnect_container_to_network
<salt.modules.dockerng.disconnect_container_to_network>`
- :py:func:`dockerng.disconnect_container_from_network
<salt.modules.dockerng.disconnect_container_from_network>`
@ -1594,7 +1594,7 @@ def _validate_input(kwargs,
else:
# just a name assume it is a network
log.info(
'Assuming network_mode {0!r} is a network.'.format(
'Assuming network_mode \'{0}\' is a network.'.format(
kwargs['network_mode'])
)
except SaltInvocationError:

View file

@ -1962,6 +1962,123 @@ def absent(name, force=False):
return ret
def network_present(name, driver=None, containers=None):
'''
Ensure that a network is present.
name
Name of the netwotk
driver
Type of driver for that network.
containers:
List of container names that should be part of this network
Usage Examples:
.. code-block:: yaml
network_foo:
dockerng.network_present
.. code-block:: yaml
network_bar:
dockerng.network_present
- name: bar
- containers:
- cont1
- cont2
'''
ret = {'name': name,
'changes': {},
'result': False,
'comment': ''}
if containers is None:
containers = []
networks = __salt__['dockerng.networks'](names=[name])
if networks:
network = networks[0] # we expect network's name to be unique
if all(c in network['Containers'] for c in containers):
ret['result'] = True
ret['comment'] = 'Network \'{0}\' already exists.'.format(name)
return ret
result = True
for container in containers:
if container not in network['Containers']:
try:
ret['changes']['connected'] = __salt__['dockerng.connect_container_to_network'](
container, name)
except Exception as exc:
ret['comment'] = ('Failed to connect container \'{0}\' to network \'{1}\' {2}'.format(
container, name, exc))
result = False
ret['result'] = result
else:
try:
ret['changes']['created'] = __salt__['dockerng.create_network'](
name, driver=driver)
except Exception as exc:
ret['comment'] = ('Failed to create network \'{0}\': {1}'
.format(name, exc))
else:
result = True
for container in containers:
try:
ret['changes']['connected'] = __salt__['dockerng.connect_container_to_network'](
container, name)
except Exception as exc:
ret['comment'] = ('Failed to connect container \'{0}\' to network \'{1}\' {2}'.format(
container, name, exc))
result = False
ret['result'] = result
return ret
def network_absent(name, driver=None):
'''
Ensure that a network is absent.
name
Name of the netwotk
Usage Examples:
.. code-block:: yaml
network_foo:
dockerng.network_absent
'''
ret = {'name': name,
'changes': {},
'result': False,
'comment': ''}
networks = __salt__['dockerng.networks'](names=[name])
if not networks:
ret['result'] = True
ret['comment'] = 'Network \'{0}\' already absent'.format(name)
return ret
for container in networks[0]['Containers']:
try:
ret['changes']['disconnected'] = __salt__['dockerng.disconnect_container_from_network'](container, name)
except Exception as exc:
ret['comment'] = ('Failed to disconnect container \'{0}\' to network \'{1}\' {2}'.format(
container, name, exc))
try:
ret['changes']['removed'] = __salt__['dockerng.remove_network'](name)
ret['result'] = True
except Exception as exc:
ret['comment'] = ('Failed to remove network \'{0}\': {1}'
.format(name, exc))
return ret
def mod_watch(name, sfun=None, **kwargs):
if sfun == 'running':
watch_kwargs = copy.deepcopy(kwargs)

View file

@ -453,6 +453,54 @@ class DockerngTestCase(TestCase):
labels=['LABEL1', 'LABEL2'],
client_timeout=60)
def test_network_present(self):
'''
Test dockerng.network_present
'''
dockerng_create_network = Mock(return_value='created')
dockerng_connect_container_to_network = Mock(return_value='connected')
__salt__ = {'dockerng.create_network': dockerng_create_network,
'dockerng.connect_container_to_network': dockerng_connect_container_to_network,
'dockerng.networks': Mock(return_value=[]),
}
with patch.dict(dockerng_state.__dict__,
{'__salt__': __salt__}):
ret = dockerng_state.network_present(
'network_foo',
containers=['container'],
)
dockerng_create_network.assert_called_with('network_foo', driver=None)
dockerng_connect_container_to_network.assert_called_with('container',
'network_foo')
self.assertEqual(ret, {'name': 'network_foo',
'comment': '',
'changes': {'connected': 'connected',
'created': 'created'},
'result': True})
def test_network_absent(self):
'''
Test dockerng.network_absent
'''
dockerng_remove_network = Mock(return_value='removed')
dockerng_disconnect_container_from_network = Mock(return_value='disconnected')
__salt__ = {'dockerng.remove_network': dockerng_remove_network,
'dockerng.disconnect_container_from_network': dockerng_disconnect_container_from_network,
'dockerng.networks': Mock(return_value=[{'Containers': {'container': {}}}]),
}
with patch.dict(dockerng_state.__dict__,
{'__salt__': __salt__}):
ret = dockerng_state.network_absent(
'network_foo',
)
dockerng_disconnect_container_from_network.assert_called_with('container',
'network_foo')
dockerng_remove_network.assert_called_with('network_foo')
self.assertEqual(ret, {'name': 'network_foo',
'comment': '',
'changes': {'disconnected': 'disconnected',
'removed': 'removed'},
'result': True})
if __name__ == '__main__':
from integration import run_tests