-
Notifications
You must be signed in to change notification settings - Fork 102
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
Add "network exists" command #632
Conversation
04bf1e5
to
fb43548
Compare
@@ -209,6 +210,26 @@ def disconnect( | |||
full_cmd += [network, container] | |||
run(full_cmd) | |||
|
|||
def exists( | |||
self, | |||
network: str, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should support passing a Network
object too, i.e. this should be ValidNetwork
as with other methods such as disconnect()
above.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated, along with making sure the test covers that use case.
fb43548
to
8d27a80
Compare
@overload | ||
def inspect(self, x: str) -> Network: ... | ||
def inspect(self, x: ValidNetwork) -> Network: ... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
inspect()
doesn't make a lot of sense to take a Network
object, since it's used for getting a Network
object. Can you instead make exists()
do self.inspect(str(network))
? (or check how his is handled for other object types such as Container
, Pod
, ...)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems like things are slightly inconsistent on whether or not inspect()
takes ValidSomeObject
or just str
. This change was based on ContainerCLI.inspect()
, which takes ValidContainer
as an argument. PodCLI.inspect()
similarly takes ValidPod
. However, VolumeCLI.inspect()
and ServiceCLI.inspect()
only take a str
.
Given the inconsistency, sounds like str
would be the preferred approach, yes?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh dear 🥲 I don't mind - the main thing I noticed is you didn't quite update the type annotations correctly in adding the support. I'd leave it as just str
here to keep the scope of the changeset contained.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved back to str
and used self.id
in Network.exists()
.
You'll have to excuse any errors in my type changes, I'm not all that familiar with the codebase. Been using it a lot the last several months, but only recently dived into the actual code. For my own understanding, what was incorrect with how I'd updated the type annotations?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For my own understanding, what was incorrect with how I'd updated the type annotations?
In this commit you had
@overload
def inspect(self, x: ValidNetwork) -> Network: ...
@overload
def inspect(self, x: List[str]) -> List[Network]: ...
def inspect(self, x: Union[str, List[str]]) -> Union[Network, List[Network]]:
if isinstance(x, str) or isinstance(x, Network):
return Network(self.client_config, x)
else:
return [Network(self.client_config, reference) for reference in x]
- The
@overload
methods say that the two possible invocations are a singleValidNetwork
(str | Network
) or a list ofstr
. This means there's not support for a list ofNetwork
, i.e. the second overload should've hadList[ValidNetwork]
(althoughList
is a bad choice of type, see Avoid usingList
andDict
for function argument types #584). - The actual method signature (without
@overload
) wasn't updated to supportNetwork
at all, so is inconsistent with the overloads.
If we wanted to support Network
objects here I would make this:
@overload
def inspect(self, x: ValidNetwork) -> Network: ...
@overload
def inspect(self, x: Iterable[ValidNetwork]) -> List[Network]: ...
def inspect(self, x: Union[ValidNetwork, Iterable[ValidNetwork]]) -> Union[Network, List[Network]]:
if isinstance(x, Iterable) and not isinstance(x, str):
return [Network(self.client_config, str(net)) for net in x]
else:
return Network(self.client_config, str(x))
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To be clear, I still think the exists()
method should support Network
types:
def exists(self, network: ValidNetwork) -> bool:
try:
self.inspect(str(network))
except NoSuchNetwork:
return False
else:
return True
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got it. Overloads are one of things I haven't touched much with type annotations so it doesn't entirely surprise me that that would be where I went awry. Thanks for the clarification!
Thanks for doing this enhancement :) Currently all PRs are blocked on the docs CI job being broken, which I believe I have fixed in #628 but this needs confirming. [EDIT: now fixed, merge in latest |
Podman has added 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 cross-compatibility with Docker.
8d27a80
to
50efde0
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks again :)
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