Repository: docker/docker-py

Issue:
create_ipam_config() got an unexpected keyword argument 'subnet'
https://docker-py.readthedocs.io/en/latest/networks/ this tells me I can use subnet argument, but when I launch my program it throws `TypeError: create_ipam_config() got an unexpected keyword argument 'subnet'`

docker-py version is 1.8.1



Discussion:

Resolving patch:
diff --git a/docker/api/network.py b/docker/api/network.py
--- a/docker/api/network.py
+++ b/docker/api/network.py
@@ -1,7 +1,7 @@
 import json
 
 from ..errors import InvalidVersion
-from ..utils import check_resource, minimum_version, normalize_links
+from ..utils import check_resource, minimum_version
 from ..utils import version_lt
 
 
@@ -63,26 +63,12 @@ def connect_container_to_network(self, container, net_id,
                                      aliases=None, links=None):
         data = {
             "Container": container,
-            "EndpointConfig": {
-                "Aliases": aliases,
-                "Links": normalize_links(links) if links else None,
-            },
+            "EndpointConfig": self.create_endpoint_config(
+                aliases=aliases, links=links, ipv4_address=ipv4_address,
+                ipv6_address=ipv6_address
+            ),
         }
 
-        # 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)
diff --git a/docker/utils/utils.py b/docker/utils/utils.py
--- a/docker/utils/utils.py
+++ b/docker/utils/utils.py
@@ -813,19 +813,30 @@ def create_networking_config(endpoints_config=None):
     return networking_config
 
 
-def create_endpoint_config(version, aliases=None, links=None):
+def create_endpoint_config(version, aliases=None, links=None,
+                           ipv4_address=None, ipv6_address=None):
+    if version_lt(version, '1.22'):
+        raise errors.InvalidVersion(
+            'Endpoint config is not supported for API version < 1.22'
+        )
     endpoint_config = {}
 
     if aliases:
-        if version_lt(version, '1.22'):
-            raise host_config_version_error('endpoint_config.aliases', '1.22')
         endpoint_config["Aliases"] = aliases
 
     if links:
-        if version_lt(version, '1.22'):
-            raise host_config_version_error('endpoint_config.links', '1.22')
         endpoint_config["Links"] = normalize_links(links)
 
+    ipam_config = {}
+    if ipv4_address:
+        ipam_config['IPv4Address'] = ipv4_address
+
+    if ipv6_address:
+        ipam_config['IPv6Address'] = ipv6_address
+
+    if ipam_config:
+        endpoint_config['IPAMConfig'] = ipam_config
+
     return endpoint_config
 
 


Test patch:
diff --git a/tests/integration/network_test.py b/tests/integration/network_test.py
--- a/tests/integration/network_test.py
+++ b/tests/integration/network_test.py
@@ -185,7 +185,66 @@ def test_create_with_aliases(self):
         container_data = self.client.inspect_container(container)
         self.assertEqual(
             container_data['NetworkSettings']['Networks'][net_name]['Aliases'],
-            ['foo', 'bar'])
+            ['foo', 'bar']
+        )
+
+    @requires_api_version('1.22')
+    def test_create_with_ipv4_address(self):
+        net_name, net_id = self.create_network(
+            ipam=create_ipam_config(
+                driver='default',
+                pool_configs=[create_ipam_pool(subnet="132.124.0.0/16")],
+            ),
+        )
+        container = self.client.create_container(
+            image='busybox', command='top',
+            host_config=self.client.create_host_config(network_mode=net_name),
+            networking_config=self.client.create_networking_config({
+                net_name: self.client.create_endpoint_config(
+                    ipv4_address='132.124.0.23'
+                )
+            })
+        )
+        self.tmp_containers.append(container)
+        self.client.start(container)
+
+        container_data = self.client.inspect_container(container)
+        self.assertEqual(
+            container_data[
+                'NetworkSettings']['Networks'][net_name]['IPAMConfig'][
+                'IPv4Address'
+            ],
+            '132.124.0.23'
+        )
+
+    @requires_api_version('1.22')
+    def test_create_with_ipv6_address(self):
+        net_name, net_id = self.create_network(
+            ipam=create_ipam_config(
+                driver='default',
+                pool_configs=[create_ipam_pool(subnet="2001:389::1/64")],
+            ),
+        )
+        container = self.client.create_container(
+            image='busybox', command='top',
+            host_config=self.client.create_host_config(network_mode=net_name),
+            networking_config=self.client.create_networking_config({
+                net_name: self.client.create_endpoint_config(
+                    ipv6_address='2001:389::f00d'
+                )
+            })
+        )
+        self.tmp_containers.append(container)
+        self.client.start(container)
+
+        container_data = self.client.inspect_container(container)
+        self.assertEqual(
+            container_data[
+                'NetworkSettings']['Networks'][net_name]['IPAMConfig'][
+                'IPv6Address'
+            ],
+            '2001:389::f00d'
+        )
 
     @requires_api_version('1.22')
     def test_create_with_links(self):