Skip to content

Commit

Permalink
Add "network exists" command (#632)
Browse files Browse the repository at this point in the history
Podman has support for a "network exists" command which checks if a
network exists. This adds support for that command, although it does so
by way of network inspect for compatibility with Docker, which does not
have a standalone `network exists` command.

Closes #618

---------

Co-authored-by: Gabriel de Marmiesse <[email protected]>
  • Loading branch information
eclark0426 and gabrieldemarmiesse authored Sep 5, 2024
1 parent f58af9e commit 4a518a5
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 0 deletions.
24 changes: 24 additions & 0 deletions python_on_whales/components/network/cli_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
NetworkInspectResult,
NetworkIPAM,
)
from python_on_whales.exceptions import NoSuchNetwork
from python_on_whales.utils import format_mapping_for_cli, run, to_list


Expand Down Expand Up @@ -104,6 +105,13 @@ def config_only(self) -> bool:
def __repr__(self):
return f"python_on_whales.Network(id='{self.id[:12]}', name={self.name})"

def exists(self) -> bool:
"""Returns `True` if the network exists and `False` if it doesn't exist.
If it doesn't exist, that most likely means it was removed.
"""
return NetworkCLI(self.client_config).exists(self.id)

def remove(self) -> None:
"""Removes this Docker network.
Expand Down Expand Up @@ -209,6 +217,22 @@ def disconnect(
full_cmd += [network, container]
run(full_cmd)

def exists(
self,
network: ValidNetwork,
) -> bool:
"""Check if a network exists
Parameters:
network: The name of the network.
"""
try:
self.inspect(str(network))
except NoSuchNetwork:
return False
else:
return True

@overload
def inspect(self, x: str) -> Network: ...

Expand Down
4 changes: 4 additions & 0 deletions python_on_whales/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ class NoSuchImage(DockerException):
pass


class NoSuchNetwork(DockerException):
pass


class NoSuchPod(DockerException):
pass

Expand Down
8 changes: 8 additions & 0 deletions python_on_whales/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
DockerException,
NoSuchContainer,
NoSuchImage,
NoSuchNetwork,
NoSuchPod,
NoSuchService,
NoSuchVolume,
Expand Down Expand Up @@ -218,6 +219,13 @@ def run(
completed_process.stdout,
completed_process.stderr,
)
if "network" in decoded_stderr and "not found" in decoded_stderr:
raise NoSuchNetwork(
args,
completed_process.returncode,
completed_process.stdout,
completed_process.stderr,
)

raise DockerException(
args,
Expand Down
14 changes: 14 additions & 0 deletions tests/python_on_whales/components/test_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,17 @@ def test_swarm_service_create(docker_client: DockerClient):
"busybox", ["sleep", "infinity"], network=my_net.name
):
assert len(my_net.containers) == 2 # 1 container + 1 endpoint


@pytest.mark.parametrize("ctr_client", ["docker", "podman"], indirect=True)
def test_network_exists(ctr_client: DockerClient):
network_name = random_name()
assert not ctr_client.network.exists(network_name)
with ctr_client.network.create(network_name) as network:
assert ctr_client.network.exists(network_name)
assert ctr_client.network.exists(network)
assert network.exists()

# Outside with clause, network should be removed and no longer exist
assert not ctr_client.network.exists(network)
assert not network.exists()

0 comments on commit 4a518a5

Please sign in to comment.