mirror of https://github.com/docker/docker-py.git
Add support for force disconnect
Signed-off-by: Joffrey F <joffrey@docker.com>
This commit is contained in:
parent
a665dfb375
commit
6552076856
|
|
@ -93,8 +93,15 @@ class NetworkApiMixin(object):
|
||||||
|
|
||||||
@check_resource
|
@check_resource
|
||||||
@minimum_version('1.21')
|
@minimum_version('1.21')
|
||||||
def disconnect_container_from_network(self, container, net_id):
|
def disconnect_container_from_network(self, container, net_id,
|
||||||
data = {"container": container}
|
force=False):
|
||||||
|
data = {"Container": container}
|
||||||
|
if force:
|
||||||
|
if version_lt(self._version, '1.22'):
|
||||||
|
raise InvalidVersion(
|
||||||
|
'Forced disconnect was introduced in API 1.22'
|
||||||
|
)
|
||||||
|
data['Force'] = force
|
||||||
url = self._url("/networks/{0}/disconnect", net_id)
|
url = self._url("/networks/{0}/disconnect", net_id)
|
||||||
res = self._post_json(url, data=data)
|
res = self._post_json(url, data=data)
|
||||||
self._raise_for_status(res)
|
self._raise_for_status(res)
|
||||||
|
|
|
||||||
|
|
@ -355,6 +355,8 @@ Inspect changes on a container's filesystem.
|
||||||
|
|
||||||
* container (str): container-id/name to be disconnected from a network
|
* container (str): container-id/name to be disconnected from a network
|
||||||
* net_id (str): network id
|
* net_id (str): network id
|
||||||
|
* force (bool): Force the container to disconnect from a network.
|
||||||
|
Default: `False`
|
||||||
|
|
||||||
## events
|
## events
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -115,7 +115,8 @@ class TestNetworks(helpers.BaseTestCase):
|
||||||
network_data = self.client.inspect_network(net_id)
|
network_data = self.client.inspect_network(net_id)
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
list(network_data['Containers'].keys()),
|
list(network_data['Containers'].keys()),
|
||||||
[container['Id']])
|
[container['Id']]
|
||||||
|
)
|
||||||
|
|
||||||
with pytest.raises(docker.errors.APIError):
|
with pytest.raises(docker.errors.APIError):
|
||||||
self.client.connect_container_to_network(container, net_id)
|
self.client.connect_container_to_network(container, net_id)
|
||||||
|
|
@ -127,6 +128,33 @@ class TestNetworks(helpers.BaseTestCase):
|
||||||
with pytest.raises(docker.errors.APIError):
|
with pytest.raises(docker.errors.APIError):
|
||||||
self.client.disconnect_container_from_network(container, net_id)
|
self.client.disconnect_container_from_network(container, net_id)
|
||||||
|
|
||||||
|
@requires_api_version('1.22')
|
||||||
|
def test_connect_and_force_disconnect_container(self):
|
||||||
|
net_name, net_id = self.create_network()
|
||||||
|
|
||||||
|
container = self.client.create_container('busybox', 'top')
|
||||||
|
self.tmp_containers.append(container)
|
||||||
|
self.client.start(container)
|
||||||
|
|
||||||
|
network_data = self.client.inspect_network(net_id)
|
||||||
|
self.assertFalse(network_data.get('Containers'))
|
||||||
|
|
||||||
|
self.client.connect_container_to_network(container, net_id)
|
||||||
|
network_data = self.client.inspect_network(net_id)
|
||||||
|
self.assertEqual(
|
||||||
|
list(network_data['Containers'].keys()),
|
||||||
|
[container['Id']]
|
||||||
|
)
|
||||||
|
|
||||||
|
self.client.disconnect_container_from_network(container, net_id, True)
|
||||||
|
network_data = self.client.inspect_network(net_id)
|
||||||
|
self.assertFalse(network_data.get('Containers'))
|
||||||
|
|
||||||
|
with pytest.raises(docker.errors.APIError):
|
||||||
|
self.client.disconnect_container_from_network(
|
||||||
|
container, net_id, force=True
|
||||||
|
)
|
||||||
|
|
||||||
@requires_api_version('1.22')
|
@requires_api_version('1.22')
|
||||||
def test_connect_with_aliases(self):
|
def test_connect_with_aliases(self):
|
||||||
net_name, net_id = self.create_network()
|
net_name, net_id = self.create_network()
|
||||||
|
|
|
||||||
|
|
@ -184,4 +184,4 @@ class NetworkTest(DockerClientTest):
|
||||||
|
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
json.loads(post.call_args[1]['data']),
|
json.loads(post.call_args[1]['data']),
|
||||||
{'container': container_id})
|
{'Container': container_id})
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue