Merge pull request #1812 from rycus86/greedy_network_list

Fetch network details with network lists greedily
This commit is contained in:
Joffrey F 2017-12-13 16:33:06 -08:00 committed by GitHub
commit aad0c76002
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 7 deletions

View File

@ -1,4 +1,5 @@
from ..api import APIClient
from ..utils import version_gte
from .containers import Container
from .resource import Model, Collection
@ -153,7 +154,7 @@ class NetworkCollection(Collection):
resp = self.client.api.create_network(name, *args, **kwargs)
return self.get(resp['Id'])
def get(self, network_id):
def get(self, network_id, *args, **kwargs):
"""
Get a network by its ID.
@ -175,7 +176,9 @@ class NetworkCollection(Collection):
If the server returns an error.
"""
return self.prepare_model(self.client.api.inspect_network(network_id))
return self.prepare_model(
self.client.api.inspect_network(network_id, *args, **kwargs)
)
def list(self, *args, **kwargs):
"""
@ -184,6 +187,13 @@ class NetworkCollection(Collection):
Args:
names (:py:class:`list`): List of names to filter by.
ids (:py:class:`list`): List of ids to filter by.
filters (dict): Filters to be processed on the network list.
Available filters:
- ``driver=[<driver-name>]`` Matches a network's driver.
- ``label=[<key>]`` or ``label=[<key>=<value>]``.
- ``type=["custom"|"builtin"]`` Filters networks by type.
greedy (bool): Fetch more details for each network individually.
You might want this to get the containers attached to them.
Returns:
(list of :py:class:`Network`) The networks on the server.
@ -192,8 +202,13 @@ class NetworkCollection(Collection):
:py:class:`docker.errors.APIError`
If the server returns an error.
"""
greedy = kwargs.pop('greedy', False)
resp = self.client.api.networks(*args, **kwargs)
return [self.prepare_model(item) for item in resp]
networks = [self.prepare_model(item) for item in resp]
if greedy and version_gte(self.client.api._version, '1.28'):
for net in networks:
net.reload()
return networks
def prune(self, filters=None):
self.client.api.prune_networks(filters=filters)

View File

@ -3,7 +3,7 @@ from .. import helpers
from .base import BaseIntegrationTest, TEST_API_VERSION
class ImageCollectionTest(BaseIntegrationTest):
class NetworkCollectionTest(BaseIntegrationTest):
def test_create(self):
client = docker.from_env(version=TEST_API_VERSION)
@ -47,7 +47,7 @@ class ImageCollectionTest(BaseIntegrationTest):
assert network.id not in [n.id for n in client.networks.list()]
class ImageTest(BaseIntegrationTest):
class NetworkTest(BaseIntegrationTest):
def test_connect_disconnect(self):
client = docker.from_env(version=TEST_API_VERSION)
@ -59,6 +59,12 @@ class ImageTest(BaseIntegrationTest):
network.connect(container)
container.start()
assert client.networks.get(network.id).containers == [container]
network_containers = list(
c
for net in client.networks.list(ids=[network.id], greedy=True)
for c in net.containers
)
assert network_containers == [container]
network.disconnect(container)
assert network.containers == []
assert client.networks.get(network.id).containers == []

View File

@ -4,7 +4,7 @@ from .fake_api import FAKE_NETWORK_ID, FAKE_CONTAINER_ID
from .fake_api_client import make_fake_client
class ImageCollectionTest(unittest.TestCase):
class NetworkCollectionTest(unittest.TestCase):
def test_create(self):
client = make_fake_client()
@ -37,7 +37,7 @@ class ImageCollectionTest(unittest.TestCase):
assert client.api.networks.called_once_with(names=["foobar"])
class ImageTest(unittest.TestCase):
class NetworkTest(unittest.TestCase):
def test_connect(self):
client = make_fake_client()