Skip to content

Commit a665dfb

Browse files
committed
Add support for labels and enable_ipv6 in create_network
Tests + docs Signed-off-by: Joffrey F <[email protected]>
1 parent 24bfb99 commit a665dfb

File tree

3 files changed

+53
-8
lines changed

3 files changed

+53
-8
lines changed

docker/api/network.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ def networks(self, names=None, ids=None):
2222

2323
@minimum_version('1.21')
2424
def create_network(self, name, driver=None, options=None, ipam=None,
25-
check_duplicate=None, internal=False):
25+
check_duplicate=None, internal=False, labels=None,
26+
enable_ipv6=False):
2627
if options is not None and not isinstance(options, dict):
2728
raise TypeError('options must be a dictionary')
2829

@@ -34,6 +35,22 @@ def create_network(self, name, driver=None, options=None, ipam=None,
3435
'CheckDuplicate': check_duplicate
3536
}
3637

38+
if labels is not None:
39+
if version_lt(self._version, '1.23'):
40+
raise InvalidVersion(
41+
'network labels were introduced in API 1.23'
42+
)
43+
if not isinstance(labels, dict):
44+
raise TypeError('labels must be a dictionary')
45+
data["Labels"] = labels
46+
47+
if enable_ipv6:
48+
if version_lt(self._version, '1.23'):
49+
raise InvalidVersion(
50+
'enable_ipv6 was introduced in API 1.23'
51+
)
52+
data['EnableIPv6'] = True
53+
3754
if internal:
3855
if version_lt(self._version, '1.22'):
3956
raise InvalidVersion('Internal networks are not '

docs/api.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -283,22 +283,25 @@ The utility can be used as follows:
283283
```python
284284
>>> import docker.utils
285285
>>> my_envs = docker.utils.parse_env_file('/path/to/file')
286-
>>> docker.utils.create_container_config('1.18', '_mongodb', 'foobar', environment=my_envs)
286+
>>> client.create_container('myimage', 'command', environment=my_envs)
287287
```
288288

289-
You can now use this with 'environment' for `create_container`.
290-
291-
292289
## create_network
293290

294-
Create a network, similar to the `docker network create` command.
291+
Create a network, similar to the `docker network create` command. See the
292+
[networks documentation](networks.md) for details.
295293

296294
**Params**:
297295

298296
* name (str): Name of the network
299297
* driver (str): Name of the driver used to create the network
300-
301298
* options (dict): Driver options as a key-value dictionary
299+
* ipam (dict): Optional custom IP scheme for the network
300+
* check_duplicate (bool): Request daemon to check for networks with same name.
301+
Default: `True`.
302+
* internal (bool): Restrict external access to the network. Default `False`.
303+
* labels (dict): Map of labels to set on the network. Default `None`.
304+
* enable_ipv6 (bool): Enable IPv6 on the network. Default `False`.
302305

303306
**Returns** (dict): The created network reference object
304307

tests/integration/network_test.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,8 @@ def test_create_check_duplicate(self):
300300
net_name, net_id = self.create_network()
301301
with self.assertRaises(docker.errors.APIError):
302302
self.client.create_network(net_name, check_duplicate=True)
303-
self.client.create_network(net_name, check_duplicate=False)
303+
net_id = self.client.create_network(net_name, check_duplicate=False)
304+
self.tmp_networks.append(net_id['Id'])
304305

305306
@requires_api_version('1.22')
306307
def test_connect_with_links(self):
@@ -387,3 +388,27 @@ def test_create_internal_networks(self):
387388
_, net_id = self.create_network(internal=True)
388389
net = self.client.inspect_network(net_id)
389390
assert net['Internal'] is True
391+
392+
@requires_api_version('1.23')
393+
def test_create_network_with_labels(self):
394+
_, net_id = self.create_network(labels={
395+
'com.docker.py.test': 'label'
396+
})
397+
398+
net = self.client.inspect_network(net_id)
399+
assert 'Labels' in net
400+
assert len(net['Labels']) == 1
401+
assert net['Labels'] == {
402+
'com.docker.py.test': 'label'
403+
}
404+
405+
@requires_api_version('1.23')
406+
def test_create_network_with_labels_wrong_type(self):
407+
with pytest.raises(TypeError):
408+
self.create_network(labels=['com.docker.py.test=label', ])
409+
410+
@requires_api_version('1.23')
411+
def test_create_network_ipv6_enabled(self):
412+
_, net_id = self.create_network(enable_ipv6=True)
413+
net = self.client.inspect_network(net_id)
414+
assert net['EnableIPv6'] is True

0 commit comments

Comments
 (0)