diff --git a/docker/api/network.py b/docker/api/network.py index 071a12a6..79778085 100644 --- a/docker/api/network.py +++ b/docker/api/network.py @@ -61,6 +61,8 @@ class NetworkApiMixin(object): attachable (bool): If enabled, and the network is in the global scope, non-service containers on worker nodes will be able to connect to the network. + scope (str): Specify the network's scope (``local``, ``global`` or + ``swarm``) ingress (bool): If set, create an ingress network which provides the routing-mesh in swarm mode. @@ -140,6 +142,13 @@ class NetworkApiMixin(object): data['Ingress'] = ingress + if scope is not None: + if version_lt(self._version, '1.30'): + raise InvalidVersion( + 'scope is not supported in API version < 1.30' + ) + data['Scope'] = scope + url = self._url("/networks/create") res = self._post_json(url, data=data) return self._result(res, json=True) @@ -181,7 +190,7 @@ class NetworkApiMixin(object): @minimum_version('1.21') @check_resource('net_id') - def inspect_network(self, net_id, verbose=None): + def inspect_network(self, net_id, verbose=None, scope=None): """ Get detailed information about a network. @@ -189,12 +198,18 @@ class NetworkApiMixin(object): net_id (str): ID of network verbose (bool): Show the service details across the cluster in swarm mode. + scope (str): Filter the network by scope (``swarm``, ``global`` + or ``local``). """ params = {} if verbose is not None: if version_lt(self._version, '1.28'): raise InvalidVersion('verbose was introduced in API 1.28') params['verbose'] = verbose + if scope is not None: + if version_lt(self._version, '1.31'): + raise InvalidVersion('scope was introduced in API 1.31') + params['scope'] = scope url = self._url("/networks/{0}", net_id) res = self._get(url, params=params) diff --git a/docker/models/networks.py b/docker/models/networks.py index afb0ebe8..158af99b 100644 --- a/docker/models/networks.py +++ b/docker/models/networks.py @@ -102,15 +102,19 @@ class NetworkCollection(Collection): 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. - Created with :py:class:`~docker.types.IPAMConfig`. + ipam (IPAMConfig): Optional custom IP scheme for the network. check_duplicate (bool): Request daemon to check for networks with - same name. Default: ``True``. + same name. Default: ``None``. 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``. + attachable (bool): If enabled, and the network is in the global + scope, non-service containers on worker nodes will be able to + connect to the network. + scope (str): Specify the network's scope (``local``, ``global`` or + ``swarm``) ingress (bool): If set, create an ingress network which provides the routing-mesh in swarm mode. @@ -155,6 +159,10 @@ class NetworkCollection(Collection): Args: network_id (str): The ID of the network. + verbose (bool): Retrieve the service details across the cluster in + swarm mode. + scope (str): Filter the network by scope (``swarm``, ``global`` + or ``local``). Returns: (:py:class:`Network`) The network. diff --git a/tests/integration/api_network_test.py b/tests/integration/api_network_test.py index 1cc632fa..f4fefde5 100644 --- a/tests/integration/api_network_test.py +++ b/tests/integration/api_network_test.py @@ -465,3 +465,22 @@ class TestNetworks(BaseAPIIntegrationTest): net_name, _ = self.create_network() result = self.client.prune_networks() assert net_name in result['NetworksDeleted'] + + @requires_api_version('1.31') + def test_create_inspect_network_with_scope(self): + assert self.init_swarm() + net_name_loc, net_id_loc = self.create_network(scope='local') + + assert self.client.inspect_network(net_name_loc) + assert self.client.inspect_network(net_name_loc, scope='local') + with pytest.raises(docker.errors.NotFound): + self.client.inspect_network(net_name_loc, scope='global') + + net_name_swarm, net_id_swarm = self.create_network( + driver='overlay', scope='swarm' + ) + + assert self.client.inspect_network(net_name_swarm) + assert self.client.inspect_network(net_name_swarm, scope='swarm') + with pytest.raises(docker.errors.NotFound): + self.client.inspect_network(net_name_swarm, scope='local')