Skip to content

Commit a0f6758

Browse files
committed
Add support for scope filter in inspect_network
Fix missing scope implementation in create_network Signed-off-by: Joffrey F <[email protected]>
1 parent 6e1f933 commit a0f6758

File tree

3 files changed

+46
-4
lines changed

3 files changed

+46
-4
lines changed

docker/api/network.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ def create_network(self, name, driver=None, options=None, ipam=None,
6161
attachable (bool): If enabled, and the network is in the global
6262
scope, non-service containers on worker nodes will be able to
6363
connect to the network.
64+
scope (str): Specify the network's scope (``local``, ``global`` or
65+
``swarm``)
6466
ingress (bool): If set, create an ingress network which provides
6567
the routing-mesh in swarm mode.
6668
@@ -140,6 +142,13 @@ def create_network(self, name, driver=None, options=None, ipam=None,
140142

141143
data['Ingress'] = ingress
142144

145+
if scope is not None:
146+
if version_lt(self._version, '1.30'):
147+
raise InvalidVersion(
148+
'scope is not supported in API version < 1.30'
149+
)
150+
data['Scope'] = scope
151+
143152
url = self._url("/networks/create")
144153
res = self._post_json(url, data=data)
145154
return self._result(res, json=True)
@@ -181,20 +190,26 @@ def remove_network(self, net_id):
181190

182191
@minimum_version('1.21')
183192
@check_resource('net_id')
184-
def inspect_network(self, net_id, verbose=None):
193+
def inspect_network(self, net_id, verbose=None, scope=None):
185194
"""
186195
Get detailed information about a network.
187196
188197
Args:
189198
net_id (str): ID of network
190199
verbose (bool): Show the service details across the cluster in
191200
swarm mode.
201+
scope (str): Filter the network by scope (``swarm``, ``global``
202+
or ``local``).
192203
"""
193204
params = {}
194205
if verbose is not None:
195206
if version_lt(self._version, '1.28'):
196207
raise InvalidVersion('verbose was introduced in API 1.28')
197208
params['verbose'] = verbose
209+
if scope is not None:
210+
if version_lt(self._version, '1.31'):
211+
raise InvalidVersion('scope was introduced in API 1.31')
212+
params['scope'] = scope
198213

199214
url = self._url("/networks/{0}", net_id)
200215
res = self._get(url, params=params)

docker/models/networks.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,15 +102,19 @@ def create(self, name, *args, **kwargs):
102102
name (str): Name of the network
103103
driver (str): Name of the driver used to create the network
104104
options (dict): Driver options as a key-value dictionary
105-
ipam (dict): Optional custom IP scheme for the network.
106-
Created with :py:class:`~docker.types.IPAMConfig`.
105+
ipam (IPAMConfig): Optional custom IP scheme for the network.
107106
check_duplicate (bool): Request daemon to check for networks with
108-
same name. Default: ``True``.
107+
same name. Default: ``None``.
109108
internal (bool): Restrict external access to the network. Default
110109
``False``.
111110
labels (dict): Map of labels to set on the network. Default
112111
``None``.
113112
enable_ipv6 (bool): Enable IPv6 on the network. Default ``False``.
113+
attachable (bool): If enabled, and the network is in the global
114+
scope, non-service containers on worker nodes will be able to
115+
connect to the network.
116+
scope (str): Specify the network's scope (``local``, ``global`` or
117+
``swarm``)
114118
ingress (bool): If set, create an ingress network which provides
115119
the routing-mesh in swarm mode.
116120
@@ -155,6 +159,10 @@ def get(self, network_id):
155159
156160
Args:
157161
network_id (str): The ID of the network.
162+
verbose (bool): Retrieve the service details across the cluster in
163+
swarm mode.
164+
scope (str): Filter the network by scope (``swarm``, ``global``
165+
or ``local``).
158166
159167
Returns:
160168
(:py:class:`Network`) The network.

tests/integration/api_network_test.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,3 +465,22 @@ def test_prune_networks(self):
465465
net_name, _ = self.create_network()
466466
result = self.client.prune_networks()
467467
assert net_name in result['NetworksDeleted']
468+
469+
@requires_api_version('1.31')
470+
def test_create_inspect_network_with_scope(self):
471+
assert self.init_swarm()
472+
net_name_loc, net_id_loc = self.create_network(scope='local')
473+
474+
assert self.client.inspect_network(net_name_loc)
475+
assert self.client.inspect_network(net_name_loc, scope='local')
476+
with pytest.raises(docker.errors.NotFound):
477+
self.client.inspect_network(net_name_loc, scope='global')
478+
479+
net_name_swarm, net_id_swarm = self.create_network(
480+
driver='overlay', scope='swarm'
481+
)
482+
483+
assert self.client.inspect_network(net_name_swarm)
484+
assert self.client.inspect_network(net_name_swarm, scope='swarm')
485+
with pytest.raises(docker.errors.NotFound):
486+
self.client.inspect_network(net_name_swarm, scope='local')

0 commit comments

Comments
 (0)