mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
Add expose networking to modules.dockerng
This commit is contained in:
parent
17638c734b
commit
17ff5c1ab5
2 changed files with 325 additions and 3 deletions
|
@ -167,6 +167,18 @@ Functions
|
|||
- :py:func:`dockerng.rmi <salt.modules.dockerng.rmi>`
|
||||
- :py:func:`dockerng.save <salt.modules.dockerng.save>`
|
||||
- :py:func:`dockerng.tag <salt.modules.dockerng.tag>`
|
||||
- Network Management
|
||||
- :py:func:`dockerng.networks <salt.modules.dockerng.networks>`
|
||||
- :py:func:`dockerng.create_network <salt.modules.dockerng.create_network>`
|
||||
- :py:func:`dockerng.remove_network <salt.modules.dockerng.remove_network>`
|
||||
- :py:func:`dockerng.inspect_network
|
||||
<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>`
|
||||
|
||||
|
||||
|
||||
.. _docker-execution-driver:
|
||||
|
||||
|
@ -550,6 +562,30 @@ class _api_version(object):
|
|||
return _mimic_signature(func, wrapper)
|
||||
|
||||
|
||||
class _client_version(object):
|
||||
'''
|
||||
Enforce a specific Docker client version
|
||||
'''
|
||||
def __init__(self, version):
|
||||
self.version = distutils.version.StrictVersion(version)
|
||||
|
||||
def __call__(self, func):
|
||||
def wrapper(*args, **kwargs):
|
||||
'''
|
||||
Get the current client version and check it against the one passed
|
||||
'''
|
||||
_get_client()
|
||||
current_version = '.'.join(map(str, _get_docker_py_versioninfo()))
|
||||
if distutils.version.StrictVersion(current_version) < self.version:
|
||||
raise CommandExecutionError(
|
||||
'This function requires a Docker Client version of at least '
|
||||
'{0}. Version in use is {1}.'
|
||||
.format(self.version, current_version)
|
||||
)
|
||||
return func(*args, **salt.utils.clean_kwargs(**kwargs))
|
||||
return _mimic_signature(func, wrapper)
|
||||
|
||||
|
||||
def _docker_client(wrapped):
|
||||
'''
|
||||
Decorator to run a function that requires the use of a docker.Client()
|
||||
|
@ -1555,11 +1591,16 @@ def _validate_input(kwargs,
|
|||
# Ensure that the user didn't just pass 'container:', because
|
||||
# that would be invalid.
|
||||
return
|
||||
raise SaltInvocationError()
|
||||
else:
|
||||
# just a name assume it is a network
|
||||
log.info(
|
||||
'Assuming network_mode {0!r} is a network.'.format(
|
||||
kwargs['network_mode'])
|
||||
)
|
||||
except SaltInvocationError:
|
||||
raise SaltInvocationError(
|
||||
'network_mode must be one of \'bridge\', \'host\', or '
|
||||
'\'container:<id or name>\''
|
||||
'network_mode must be one of \'bridge\', \'host\', '
|
||||
'\'container:<id or name>\' or a name of a network.'
|
||||
)
|
||||
|
||||
def _valid_restart_policy(): # pylint: disable=unused-variable
|
||||
|
@ -4231,6 +4272,153 @@ def tag_(name, image, force=False):
|
|||
# Only non-error return case is a True return, so just return the response
|
||||
return response
|
||||
|
||||
# Network Management
|
||||
|
||||
|
||||
@_api_version(1.21)
|
||||
@_client_version('1.5.0')
|
||||
def networks(names=None, ids=None):
|
||||
'''
|
||||
List existing networks
|
||||
|
||||
names
|
||||
Filter by name
|
||||
|
||||
ids
|
||||
Filter by id
|
||||
|
||||
CLI Example:
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
salt myminion dockerng.networks names="['network-web']"
|
||||
salt myminion dockerng.networks ids="['1f9d2454d0872b68dd9e8744c6e7a4c66b86f10abaccc21e14f7f014f729b2bc']"
|
||||
'''
|
||||
response = _client_wrapper('networks',
|
||||
names=names,
|
||||
ids=ids,
|
||||
)
|
||||
_clear_context()
|
||||
# Only non-error return case is a True return, so just return the response
|
||||
return response
|
||||
|
||||
|
||||
@_api_version(1.21)
|
||||
def create_network(name, driver=None):
|
||||
'''
|
||||
Create a new network
|
||||
|
||||
network_id
|
||||
ID of network
|
||||
|
||||
driver
|
||||
Driver of the network
|
||||
|
||||
CLI Example:
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
salt myminion dockerng.create_network web_network driver=bridge
|
||||
'''
|
||||
response = _client_wrapper('create_network', name, driver=driver)
|
||||
_clear_context()
|
||||
# Only non-error return case is a True return, so just return the response
|
||||
return response
|
||||
|
||||
|
||||
@_api_version(1.21)
|
||||
@_client_version('1.5.0')
|
||||
def remove_network(network_id):
|
||||
'''
|
||||
Remove a network
|
||||
|
||||
network_id
|
||||
ID of network
|
||||
|
||||
CLI Example:
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
salt myminion dockerng.remove_network 1f9d2454d0872b68dd9e8744c6e7a4c66b86f10abaccc21e14f7f014f729b2bc
|
||||
'''
|
||||
response = _client_wrapper('remove_network', network_id)
|
||||
_clear_context()
|
||||
# Only non-error return case is a True return, so just return the response
|
||||
return response
|
||||
|
||||
|
||||
@_api_version(1.21)
|
||||
@_client_version('1.5.0')
|
||||
def inspect_network(network_id):
|
||||
'''
|
||||
Inspect Network
|
||||
|
||||
network_id
|
||||
ID of network
|
||||
|
||||
CLI Example:
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
salt myminion dockerng.inspect_network 1f9d2454d0872b68dd9e8744c6e7a4c66b86f10abaccc21e14f7f014f729b2bc
|
||||
'''
|
||||
response = _client_wrapper('inspect_network', network_id)
|
||||
_clear_context()
|
||||
# Only non-error return case is a True return, so just return the response
|
||||
return response
|
||||
|
||||
|
||||
@_api_version(1.21)
|
||||
@_client_version('1.5.0')
|
||||
def connect_container_to_network(container, network_id):
|
||||
'''
|
||||
Connect container to network.
|
||||
|
||||
container
|
||||
Container name or ID
|
||||
|
||||
network_id
|
||||
ID of network
|
||||
|
||||
CLI Example:
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
salt myminion dockerng.connect_container_from_network web-1 1f9d2454d0872b68dd9e8744c6e7a4c66b86f10abaccc21e14f7f014f729b2bc
|
||||
'''
|
||||
response = _client_wrapper('connect_container_to_network',
|
||||
container,
|
||||
network_id)
|
||||
_clear_context()
|
||||
# Only non-error return case is a True return, so just return the response
|
||||
return response
|
||||
|
||||
|
||||
@_api_version(1.21)
|
||||
@_client_version('1.5.0')
|
||||
def disconnect_container_from_network(container, network_id):
|
||||
'''
|
||||
Disconnect container from network.
|
||||
|
||||
container
|
||||
Container name or ID
|
||||
|
||||
network_id
|
||||
ID of network
|
||||
|
||||
CLI Example:
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
salt myminion dockerng.disconnect_container_from_network web-1 1f9d2454d0872b68dd9e8744c6e7a4c66b86f10abaccc21e14f7f014f729b2bc
|
||||
'''
|
||||
response = _client_wrapper('disconnect_container_from_network',
|
||||
container,
|
||||
network_id)
|
||||
_clear_context()
|
||||
# Only non-error return case is a True return, so just return the response
|
||||
return response
|
||||
|
||||
|
||||
# Functions to manage container state
|
||||
@_refresh_mine_cache
|
||||
|
|
|
@ -287,6 +287,140 @@ class DockerngTestCase(TestCase):
|
|||
name='ctn',
|
||||
)
|
||||
|
||||
@skipIf(_docker_py_version() < (1, 5, 0),
|
||||
'docker module must be installed to run this test or is too old. >=1.5.0')
|
||||
def test_list_networks(self, *args):
|
||||
'''
|
||||
test list networks.
|
||||
'''
|
||||
__salt__ = {
|
||||
'config.get': Mock(),
|
||||
'mine.send': Mock(),
|
||||
}
|
||||
host_config = {}
|
||||
client = Mock()
|
||||
client.api_version = '1.21'
|
||||
with patch.dict(dockerng_mod.__dict__,
|
||||
{'__salt__': __salt__}):
|
||||
with patch.dict(dockerng_mod.__context__,
|
||||
{'docker.client': client}):
|
||||
dockerng_mod.networks(
|
||||
names=['foo'],
|
||||
ids=['01234'],
|
||||
)
|
||||
client.networks.assert_called_once_with(
|
||||
names=['foo'],
|
||||
ids=['01234'],
|
||||
)
|
||||
|
||||
@skipIf(_docker_py_version() < (1, 5, 0),
|
||||
'docker module must be installed to run this test or is too old. >=1.5.0')
|
||||
def test_create_network(self, *args):
|
||||
'''
|
||||
test create network.
|
||||
'''
|
||||
__salt__ = {
|
||||
'config.get': Mock(),
|
||||
'mine.send': Mock(),
|
||||
}
|
||||
host_config = {}
|
||||
client = Mock()
|
||||
client.api_version = '1.21'
|
||||
with patch.dict(dockerng_mod.__dict__,
|
||||
{'__salt__': __salt__}):
|
||||
with patch.dict(dockerng_mod.__context__,
|
||||
{'docker.client': client}):
|
||||
dockerng_mod.create_network(
|
||||
'foo',
|
||||
driver='bridge',
|
||||
)
|
||||
client.create_network.assert_called_once_with(
|
||||
'foo',
|
||||
driver='bridge',
|
||||
)
|
||||
|
||||
@skipIf(_docker_py_version() < (1, 5, 0),
|
||||
'docker module must be installed to run this test or is too old. >=1.5.0')
|
||||
def test_remove_network(self, *args):
|
||||
'''
|
||||
test remove network.
|
||||
'''
|
||||
__salt__ = {
|
||||
'config.get': Mock(),
|
||||
'mine.send': Mock(),
|
||||
}
|
||||
host_config = {}
|
||||
client = Mock()
|
||||
client.api_version = '1.21'
|
||||
with patch.dict(dockerng_mod.__dict__,
|
||||
{'__salt__': __salt__}):
|
||||
with patch.dict(dockerng_mod.__context__,
|
||||
{'docker.client': client}):
|
||||
dockerng_mod.remove_network('foo')
|
||||
client.remove_network.assert_called_once_with('foo')
|
||||
|
||||
@skipIf(_docker_py_version() < (1, 5, 0),
|
||||
'docker module must be installed to run this test or is too old. >=1.5.0')
|
||||
def test_inspect_network(self, *args):
|
||||
'''
|
||||
test inspect network.
|
||||
'''
|
||||
__salt__ = {
|
||||
'config.get': Mock(),
|
||||
'mine.send': Mock(),
|
||||
}
|
||||
host_config = {}
|
||||
client = Mock()
|
||||
client.api_version = '1.21'
|
||||
with patch.dict(dockerng_mod.__dict__,
|
||||
{'__salt__': __salt__}):
|
||||
with patch.dict(dockerng_mod.__context__,
|
||||
{'docker.client': client}):
|
||||
dockerng_mod.inspect_network('foo')
|
||||
client.inspect_network.assert_called_once_with('foo')
|
||||
|
||||
@skipIf(_docker_py_version() < (1, 5, 0),
|
||||
'docker module must be installed to run this test or is too old. >=1.5.0')
|
||||
def test_connect_container_to_network(self, *args):
|
||||
'''
|
||||
test inspect network.
|
||||
'''
|
||||
__salt__ = {
|
||||
'config.get': Mock(),
|
||||
'mine.send': Mock(),
|
||||
}
|
||||
host_config = {}
|
||||
client = Mock()
|
||||
client.api_version = '1.21'
|
||||
with patch.dict(dockerng_mod.__dict__,
|
||||
{'__salt__': __salt__}):
|
||||
with patch.dict(dockerng_mod.__context__,
|
||||
{'docker.client': client}):
|
||||
dockerng_mod.connect_container_to_network('container', 'foo')
|
||||
client.connect_container_to_network.assert_called_once_with(
|
||||
'container', 'foo')
|
||||
|
||||
@skipIf(_docker_py_version() < (1, 5, 0),
|
||||
'docker module must be installed to run this test or is too old. >=1.5.0')
|
||||
def test_disconnect_container_from_network(self, *args):
|
||||
'''
|
||||
test inspect network.
|
||||
'''
|
||||
__salt__ = {
|
||||
'config.get': Mock(),
|
||||
'mine.send': Mock(),
|
||||
}
|
||||
host_config = {}
|
||||
client = Mock()
|
||||
client.api_version = '1.21'
|
||||
with patch.dict(dockerng_mod.__dict__,
|
||||
{'__salt__': __salt__}):
|
||||
with patch.dict(dockerng_mod.__context__,
|
||||
{'docker.client': client}):
|
||||
dockerng_mod.disconnect_container_from_network('container', 'foo')
|
||||
client.disconnect_container_from_network.assert_called_once_with(
|
||||
'container', 'foo')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
from integration import run_tests
|
||||
|
|
Loading…
Add table
Reference in a new issue