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
nmcli: module refactor #1113
nmcli: module refactor #1113
Conversation
…modifying connections and detecting changes. * Keep DNS list arguments as lists internally. * Remove duplicated code where practical.
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.
I'm not familiar with the module's code nor did I look through all the code-changes. I'm mainly interested in:
- Will this change make something stop working in certain situations that worked before? (If something specific is not installed?)
- Is there any (intended) behavior change which breaks existing use-cases?
plugins/modules/net_tools/nmcli.py
Outdated
if self.conn_name == con_item: | ||
return True | ||
def connection_exists(self): | ||
cmd = [self.nmcli_bin, 'con', 'show', self.conn_name] |
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 is recommend to use command nmcli con show
instead of nmcli con show <conn_name>
for detecting if connection exists. Here're some reasons:
- The performance of
nmcli con show <conn_name>
may slower than the former one, because all details will be dumped to the standard output. - Using
nmcli con show
could avoid most potential exceptions in parsing and displaying active connection data. - Not only connection name, but also any other aliases (uuid, path, path), can be used to denote a connection in
nmcli con show <conn_id>
. But it's better only to match the connection name for determining the existence.
The result of connection list fetched from nmcli con show
can also be cached in Nmcli object to avoid duplicated queries. If so, the cached data should be cleaned up in method remove_connection()
and create_connection()
as well.
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.
Exactly the type of feedback I was looking for! I'll look into fixing the connection_exists
implementation tonight.
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.
Fixed in ffeae0b, but no caching.
plugins/modules/net_tools/nmcli.py
Outdated
'ipv6.dns', | ||
'ipv6.dns-search'): | ||
return list | ||
return None |
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.
Suppose default type is str
, isn't it?
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.
Fixed in 618307e.
plugins/modules/net_tools/nmcli.py
Outdated
for setting, value in options.items(): | ||
setting_type = self.settings_type(setting) | ||
type_cast = None | ||
if setting_type == bool: |
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.
The operator is
is preferred for checking type objects compared to the operator ==
.
For example, if setting_type is bool:
.
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.
While it was an improvement over issubclass(setting_type, bool)
, I agree the identity operator is what should be used instead here; fixed in 766433e.
elif key in ['ipv4.dns', 'ipv4.dns-search', 'ipv6.dns', 'ipv6.dns-search']: | ||
values = raw_value.split(',') | ||
conn_info[key] = values | ||
elif key_type == list: |
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.
The operator is
is preferred for checking type objects compared to the operator ==
.
For example, elif key_type is list:
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.
Fixed in 766433e.
@jbronn thanks for your good ideas and elaborate work. I add several comments about the implementation details for your references. I didn't test the code yet, but it looks good to work fine. :-) |
Co-authored-by: Felix Fontein <felix@fontein.de>
Co-authored-by: Felix Fontein <felix@fontein.de>
I don't think so. What I've done, requirements-wise with fixing #1112, is to eliminate the dependency on the Python bindings and not NetworkManager itself. The updated description in the
As far as I know, all existing use-cases should be preserved. Other than the new "feature" of #1112, I tried to ensure I didn't introduce any other new features to muddle review on an otherwise large diff -- for example, I could've easily extended Here are some differences from the previous implementation:
Please advise as to what test cases I should add if the current ones are insufficient. |
@jbronn thanks for your reply! Sounds good to me. About the tests: I wish there would be integration tests, but that's probably a lot harder in CI than unit tests (if possible at all). I can't say much about the unit tests, since I neither know nmcli, the module nor the unit tests enough :) |
* Improve variable naming, use `convert_func` instead of `type_cast`.
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.
+1 that looks good to me. :)
Changelog fragment looks good, too :) @alcamie101 @nerzhul any comments? If nobody complains, I'll merge in a couple of days (assuming @jbronn you don't want to change anything anymore). |
I don't plan on adding anything more, thanks for the review y'all! In the meantime, I'll look into implementing integration tests for the other tickets I've opened. |
* * Refactor `nmcli` module to use consistent parameters when creating/modifying connections and detecting changes. * Keep DNS list arguments as lists internally. * Remove duplicated code where practical. * DBus and GObject dependencies are not necessary. * Update changelog fragment. Co-authored-by: Felix Fontein <felix@fontein.de> * Update changelog fragment. Co-authored-by: Felix Fontein <felix@fontein.de> * Use identity operator instead of equality for type comparison. * Don't start changelog notes with a capital letter. * * Have `settings_type` return `str` by default instead of `None`. * Improve variable naming, use `convert_func` instead of `type_cast`. * Revert new feature of allowing ethernet types as slaves. * Bring back `list_connection_info` to list all connections with `nmcli con show`. Co-authored-by: Felix Fontein <felix@fontein.de> (cherry picked from commit 7722800)
@jbronn thanks a lot for doing this! |
* * Refactor `nmcli` module to use consistent parameters when creating/modifying connections and detecting changes. * Keep DNS list arguments as lists internally. * Remove duplicated code where practical. * DBus and GObject dependencies are not necessary. * Update changelog fragment. Co-authored-by: Felix Fontein <felix@fontein.de> * Update changelog fragment. Co-authored-by: Felix Fontein <felix@fontein.de> * Use identity operator instead of equality for type comparison. * Don't start changelog notes with a capital letter. * * Have `settings_type` return `str` by default instead of `None`. * Improve variable naming, use `convert_func` instead of `type_cast`. * Revert new feature of allowing ethernet types as slaves. * Bring back `list_connection_info` to list all connections with `nmcli con show`. Co-authored-by: Felix Fontein <felix@fontein.de> (cherry picked from commit 7722800) Co-authored-by: Justin Bronn <jbronn@gmail.com>
Can we look for other nmcli issues, via searching |
Refactor
nmcli
module for consistency and to remove duplicate and/or unused code:connection_update
: replaces all{create,modify}_connection_*
methodsconnection_options
: returns consistent nmcli options for all connection types and when detecting changes.ipv4.addresses
instead ofip4
oripv4.address
.connection.autoconnect,
connection.interface-nameinstead of
autoconnect,
ifname`.bridge-port.hairpin-mode
instead ofbridge-port.hairpin
.ip-tunnel.parent
instead ofdev
.nmcli
commands issued).Bugfixes:
autoconnect
parameter.ifname
after connection creation.New features:
ADDITIONAL NOTES