mirror of https://github.com/docker/docker-py.git
Add support for labels and enable_ipv6 in create_network
Tests + docs Signed-off-by: Joffrey F <joffrey@docker.com>
This commit is contained in:
parent
24bfb99e05
commit
a665dfb375
|
@ -22,7 +22,8 @@ class NetworkApiMixin(object):
|
|||
|
||||
@minimum_version('1.21')
|
||||
def create_network(self, name, driver=None, options=None, ipam=None,
|
||||
check_duplicate=None, internal=False):
|
||||
check_duplicate=None, internal=False, labels=None,
|
||||
enable_ipv6=False):
|
||||
if options is not None and not isinstance(options, dict):
|
||||
raise TypeError('options must be a dictionary')
|
||||
|
||||
|
@ -34,6 +35,22 @@ class NetworkApiMixin(object):
|
|||
'CheckDuplicate': check_duplicate
|
||||
}
|
||||
|
||||
if labels is not None:
|
||||
if version_lt(self._version, '1.23'):
|
||||
raise InvalidVersion(
|
||||
'network labels were introduced in API 1.23'
|
||||
)
|
||||
if not isinstance(labels, dict):
|
||||
raise TypeError('labels must be a dictionary')
|
||||
data["Labels"] = labels
|
||||
|
||||
if enable_ipv6:
|
||||
if version_lt(self._version, '1.23'):
|
||||
raise InvalidVersion(
|
||||
'enable_ipv6 was introduced in API 1.23'
|
||||
)
|
||||
data['EnableIPv6'] = True
|
||||
|
||||
if internal:
|
||||
if version_lt(self._version, '1.22'):
|
||||
raise InvalidVersion('Internal networks are not '
|
||||
|
|
15
docs/api.md
15
docs/api.md
|
@ -283,22 +283,25 @@ The utility can be used as follows:
|
|||
```python
|
||||
>>> import docker.utils
|
||||
>>> my_envs = docker.utils.parse_env_file('/path/to/file')
|
||||
>>> docker.utils.create_container_config('1.18', '_mongodb', 'foobar', environment=my_envs)
|
||||
>>> client.create_container('myimage', 'command', environment=my_envs)
|
||||
```
|
||||
|
||||
You can now use this with 'environment' for `create_container`.
|
||||
|
||||
|
||||
## create_network
|
||||
|
||||
Create a network, similar to the `docker network create` command.
|
||||
Create a network, similar to the `docker network create` command. See the
|
||||
[networks documentation](networks.md) for details.
|
||||
|
||||
**Params**:
|
||||
|
||||
* name (str): Name of the network
|
||||
* driver (str): Name of the driver used to create the network
|
||||
|
||||
* options (dict): Driver options as a key-value dictionary
|
||||
* ipam (dict): Optional custom IP scheme for the network
|
||||
* check_duplicate (bool): Request daemon to check for networks with same name.
|
||||
Default: `True`.
|
||||
* internal (bool): Restrict external access to the network. Default `False`.
|
||||
* labels (dict): Map of labels to set on the network. Default `None`.
|
||||
* enable_ipv6 (bool): Enable IPv6 on the network. Default `False`.
|
||||
|
||||
**Returns** (dict): The created network reference object
|
||||
|
||||
|
|
|
@ -300,7 +300,8 @@ class TestNetworks(helpers.BaseTestCase):
|
|||
net_name, net_id = self.create_network()
|
||||
with self.assertRaises(docker.errors.APIError):
|
||||
self.client.create_network(net_name, check_duplicate=True)
|
||||
self.client.create_network(net_name, check_duplicate=False)
|
||||
net_id = self.client.create_network(net_name, check_duplicate=False)
|
||||
self.tmp_networks.append(net_id['Id'])
|
||||
|
||||
@requires_api_version('1.22')
|
||||
def test_connect_with_links(self):
|
||||
|
@ -387,3 +388,27 @@ class TestNetworks(helpers.BaseTestCase):
|
|||
_, net_id = self.create_network(internal=True)
|
||||
net = self.client.inspect_network(net_id)
|
||||
assert net['Internal'] is True
|
||||
|
||||
@requires_api_version('1.23')
|
||||
def test_create_network_with_labels(self):
|
||||
_, net_id = self.create_network(labels={
|
||||
'com.docker.py.test': 'label'
|
||||
})
|
||||
|
||||
net = self.client.inspect_network(net_id)
|
||||
assert 'Labels' in net
|
||||
assert len(net['Labels']) == 1
|
||||
assert net['Labels'] == {
|
||||
'com.docker.py.test': 'label'
|
||||
}
|
||||
|
||||
@requires_api_version('1.23')
|
||||
def test_create_network_with_labels_wrong_type(self):
|
||||
with pytest.raises(TypeError):
|
||||
self.create_network(labels=['com.docker.py.test=label', ])
|
||||
|
||||
@requires_api_version('1.23')
|
||||
def test_create_network_ipv6_enabled(self):
|
||||
_, net_id = self.create_network(enable_ipv6=True)
|
||||
net = self.client.inspect_network(net_id)
|
||||
assert net['EnableIPv6'] is True
|
||||
|
|
Loading…
Reference in New Issue