mirror of https://github.com/docker/docker-py.git
Add ip4&ip6 (#935) support, network/id/connect
Signed-off-by: Matt Daue <mattdaue@gmail.com> - Implement check to validate API ver is >= 1.22 for new feature - Includes patch @elchris82: Changed network in data dict to IPAMConfig as needed from the API. See https://github.com/docker/docker/issues/20732 - Update unit test for container attach to net - Update integration tests - Add integration test for IP setting Signed-off-by: Matt Daue <mattdaue@gmail.com>
This commit is contained in:
parent
062c76d8b2
commit
d4a5bc4a86
|
|
@ -1,6 +1,8 @@
|
|||
import json
|
||||
|
||||
from ..errors import InvalidVersion
|
||||
from ..utils import check_resource, minimum_version, normalize_links
|
||||
from ..utils import version_lt
|
||||
|
||||
|
||||
class NetworkApiMixin(object):
|
||||
|
|
@ -48,6 +50,7 @@ class NetworkApiMixin(object):
|
|||
@check_resource
|
||||
@minimum_version('1.21')
|
||||
def connect_container_to_network(self, container, net_id,
|
||||
ipv4_address=None, ipv6_address=None,
|
||||
aliases=None, links=None):
|
||||
data = {
|
||||
"Container": container,
|
||||
|
|
@ -56,6 +59,21 @@ class NetworkApiMixin(object):
|
|||
"Links": normalize_links(links) if links else None,
|
||||
},
|
||||
}
|
||||
|
||||
# IPv4 or IPv6 or neither:
|
||||
if ipv4_address or ipv6_address:
|
||||
if version_lt(self._version, '1.22'):
|
||||
raise InvalidVersion('IP address assignment is not '
|
||||
'supported in API version < 1.22')
|
||||
|
||||
data['EndpointConfig']['IPAMConfig'] = dict()
|
||||
if ipv4_address:
|
||||
data['EndpointConfig']['IPAMConfig']['IPv4Address'] = \
|
||||
ipv4_address
|
||||
if ipv6_address:
|
||||
data['EndpointConfig']['IPAMConfig']['IPv6Address'] = \
|
||||
ipv6_address
|
||||
|
||||
url = self._url("/networks/{0}/connect", net_id)
|
||||
res = self._post_json(url, data=data)
|
||||
self._raise_for_status(res)
|
||||
|
|
|
|||
|
|
@ -235,3 +235,51 @@ class TestNetworks(helpers.BaseTestCase):
|
|||
)
|
||||
|
||||
self.execute(container, ['nslookup', 'bar'])
|
||||
|
||||
@requires_api_version('1.22')
|
||||
def test_connect_with_ipv4_address(self):
|
||||
net_name, net_id = self.create_network()
|
||||
|
||||
container = self.create_and_start(
|
||||
host_config=self.client.create_host_config(network_mode=net_name))
|
||||
|
||||
self.client.disconnect_container_from_network(container, net_name)
|
||||
self.client.connect_container_to_network(
|
||||
container, net_name,
|
||||
ipv4_address='192.168.0.1')
|
||||
|
||||
container_data = self.client.inspect_container(container)
|
||||
self.assertEqual(
|
||||
container_data['NetworkSettings']['Networks'][net_name]
|
||||
['IPAMConfig']['IPv4Address'],
|
||||
'192.168.0.1')
|
||||
|
||||
self.create_and_start(
|
||||
name='docker-py-test-upstream',
|
||||
host_config=self.client.create_host_config(network_mode=net_name))
|
||||
|
||||
self.execute(container, ['nslookup', 'bar'])
|
||||
|
||||
@requires_api_version('1.22')
|
||||
def test_connect_with_ipv6_address(self):
|
||||
net_name, net_id = self.create_network()
|
||||
|
||||
container = self.create_and_start(
|
||||
host_config=self.client.create_host_config(network_mode=net_name))
|
||||
|
||||
self.client.disconnect_container_from_network(container, net_name)
|
||||
self.client.connect_container_to_network(
|
||||
container, net_name,
|
||||
ipv6_address='2001:389::1')
|
||||
|
||||
container_data = self.client.inspect_container(container)
|
||||
self.assertEqual(
|
||||
container_data['NetworkSettings']['Networks'][net_name]
|
||||
['IPAMConfig']['IPv6Address'],
|
||||
'2001:389::1')
|
||||
|
||||
self.create_and_start(
|
||||
name='docker-py-test-upstream',
|
||||
host_config=self.client.create_host_config(network_mode=net_name))
|
||||
|
||||
self.execute(container, ['nslookup', 'bar'])
|
||||
|
|
|
|||
Loading…
Reference in New Issue