Skip to content

Commit

Permalink
Some missing API additions and enhancements (#623)
Browse files Browse the repository at this point in the history
Co-authored-by: Andrei Neagu <neagu@itis.swiss>
  • Loading branch information
GitHK and Andrei Neagu committed Nov 15, 2021
1 parent 8d6c89a commit 0b52c7b
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 5 deletions.
3 changes: 3 additions & 0 deletions CHANGES/623.feat
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Add support for filters when listing networks.
Add support for filters when listing volumes.
Add get option for fetching volumes by name or id.
25 changes: 22 additions & 3 deletions aiodocker/networks.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,32 @@
import json
from typing import Any, Dict, List
from typing import Any, Dict, List, Mapping

from .utils import clean_filters


class DockerNetworks:
def __init__(self, docker):
self.docker = docker

async def list(self) -> List[Dict[str, Any]]:
data = await self.docker._query_json("networks")
async def list(self, *, filters: Mapping = None) -> List[Dict[str, Any]]:
"""
Return a list of networks
Args:
filters: a dict with a list of filters
Available filters:
dangling=<boolean>
driver=<driver-name>
id=<network-id>
label=<key> or label=<key>=<value> of a network label.
name=<network-name>
scope=["swarm"|"global"|"local"]
type=["custom"|"builtin"]
"""
params = {} if filters is None else {"filters": clean_filters(filters)}

data = await self.docker._query_json("networks", params=params)
return data

async def create(self, config: Dict[str, Any]) -> "DockerNetwork":
Expand Down
24 changes: 22 additions & 2 deletions aiodocker/volumes.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,34 @@
import json

from .utils import clean_filters


class DockerVolumes:
def __init__(self, docker):
self.docker = docker

async def list(self):
data = await self.docker._query_json("volumes")
async def list(self, *, filters=None):
"""
Return a list of volumes
Args:
filters: a dict with a list of filters
Available filters:
dangling=<boolean>
driver=<volume-driver-name>
label=<key> or label=<key>:<value>
name=<volume-name>
"""
params = {} if filters is None else {"filters": clean_filters(filters)}

data = await self.docker._query_json("volumes", params=params)
return data

async def get(self, id):
data = await self.docker._query_json("volumes/{id}".format(id=id), method="GET")
return DockerVolume(self.docker, data["Name"])

async def create(self, config):
config = json.dumps(config, sort_keys=True).encode("utf-8")
data = await self.docker._query_json(
Expand Down
9 changes: 9 additions & 0 deletions tests/test_networks.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ async def test_list_networks(docker):
assert networks["none"]["Driver"] == "null"


@pytest.mark.asyncio
async def test_list_networks_with_filter(docker):
await docker.networks.create(
{"Name": "test-net-filter", "Labels": {"some": "label"}}
)
networks = await docker.networks.list(filters={"label": "some=label"})
assert len(networks) == 1


@pytest.mark.asyncio
async def test_networks(docker):
network = await docker.networks.create({"Name": "test-net"})
Expand Down
14 changes: 14 additions & 0 deletions tests/test_volumes.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
import pytest


@pytest.mark.asyncio
async def test_create_search_get_delete(docker):
name = "aiodocker-test-volume-two"
await docker.volumes.create(
{"Name": name, "Labels": {"some": "label"}, "Driver": "local"}
)
volumes_response = await docker.volumes.list(filters={"label": "some=label"})
volumes = volumes_response["Volumes"]
assert len(volumes) == 1
volume_data = volumes[0]
volume = await docker.volumes.get(volume_data["Name"])
await volume.delete()


@pytest.mark.asyncio
async def test_create_show_delete_volume(docker):
name = "aiodocker-test-volume"
Expand Down

0 comments on commit 0b52c7b

Please sign in to comment.