Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "network exists" command #632

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
Loading