Unverified Kaydet (Commit) aad0c760 authored tarafından Joffrey F's avatar Joffrey F Kaydeden (comit) GitHub

Merge pull request #1812 from rycus86/greedy_network_list

Fetch network details with network lists greedily
from ..api import APIClient from ..api import APIClient
from ..utils import version_gte
from .containers import Container from .containers import Container
from .resource import Model, Collection from .resource import Model, Collection
...@@ -153,7 +154,7 @@ class NetworkCollection(Collection): ...@@ -153,7 +154,7 @@ class NetworkCollection(Collection):
resp = self.client.api.create_network(name, *args, **kwargs) resp = self.client.api.create_network(name, *args, **kwargs)
return self.get(resp['Id']) return self.get(resp['Id'])
def get(self, network_id): def get(self, network_id, *args, **kwargs):
""" """
Get a network by its ID. Get a network by its ID.
...@@ -175,7 +176,9 @@ class NetworkCollection(Collection): ...@@ -175,7 +176,9 @@ class NetworkCollection(Collection):
If the server returns an error. 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): def list(self, *args, **kwargs):
""" """
...@@ -184,6 +187,13 @@ class NetworkCollection(Collection): ...@@ -184,6 +187,13 @@ class NetworkCollection(Collection):
Args: Args:
names (:py:class:`list`): List of names to filter by. names (:py:class:`list`): List of names to filter by.
ids (:py:class:`list`): List of ids 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: Returns:
(list of :py:class:`Network`) The networks on the server. (list of :py:class:`Network`) The networks on the server.
...@@ -192,8 +202,13 @@ class NetworkCollection(Collection): ...@@ -192,8 +202,13 @@ class NetworkCollection(Collection):
:py:class:`docker.errors.APIError` :py:class:`docker.errors.APIError`
If the server returns an error. If the server returns an error.
""" """
greedy = kwargs.pop('greedy', False)
resp = self.client.api.networks(*args, **kwargs) 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): def prune(self, filters=None):
self.client.api.prune_networks(filters=filters) self.client.api.prune_networks(filters=filters)
......
...@@ -3,7 +3,7 @@ from .. import helpers ...@@ -3,7 +3,7 @@ from .. import helpers
from .base import BaseIntegrationTest, TEST_API_VERSION from .base import BaseIntegrationTest, TEST_API_VERSION
class ImageCollectionTest(BaseIntegrationTest): class NetworkCollectionTest(BaseIntegrationTest):
def test_create(self): def test_create(self):
client = docker.from_env(version=TEST_API_VERSION) client = docker.from_env(version=TEST_API_VERSION)
...@@ -47,7 +47,7 @@ class ImageCollectionTest(BaseIntegrationTest): ...@@ -47,7 +47,7 @@ class ImageCollectionTest(BaseIntegrationTest):
assert network.id not in [n.id for n in client.networks.list()] assert network.id not in [n.id for n in client.networks.list()]
class ImageTest(BaseIntegrationTest): class NetworkTest(BaseIntegrationTest):
def test_connect_disconnect(self): def test_connect_disconnect(self):
client = docker.from_env(version=TEST_API_VERSION) client = docker.from_env(version=TEST_API_VERSION)
...@@ -59,6 +59,12 @@ class ImageTest(BaseIntegrationTest): ...@@ -59,6 +59,12 @@ class ImageTest(BaseIntegrationTest):
network.connect(container) network.connect(container)
container.start() container.start()
assert client.networks.get(network.id).containers == [container] 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) network.disconnect(container)
assert network.containers == [] assert network.containers == []
assert client.networks.get(network.id).containers == [] assert client.networks.get(network.id).containers == []
...@@ -4,7 +4,7 @@ from .fake_api import FAKE_NETWORK_ID, FAKE_CONTAINER_ID ...@@ -4,7 +4,7 @@ from .fake_api import FAKE_NETWORK_ID, FAKE_CONTAINER_ID
from .fake_api_client import make_fake_client from .fake_api_client import make_fake_client
class ImageCollectionTest(unittest.TestCase): class NetworkCollectionTest(unittest.TestCase):
def test_create(self): def test_create(self):
client = make_fake_client() client = make_fake_client()
...@@ -37,7 +37,7 @@ class ImageCollectionTest(unittest.TestCase): ...@@ -37,7 +37,7 @@ class ImageCollectionTest(unittest.TestCase):
assert client.api.networks.called_once_with(names=["foobar"]) assert client.api.networks.called_once_with(names=["foobar"])
class ImageTest(unittest.TestCase): class NetworkTest(unittest.TestCase):
def test_connect(self): def test_connect(self):
client = make_fake_client() client = make_fake_client()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment