Skip to content

Commit

Permalink
nm: Fix profile name changing
Browse files Browse the repository at this point in the history
Previously, nmstate can not change profile name on existing connection.
This patch will update the profile name when `interface.profile-name`
been desired explicitly.

Integration test case included.

Resolves: https://issues.redhat.com/browse/RHEL-59239

Signed-off-by: Gris Ge <[email protected]>
  • Loading branch information
cathay4t committed Sep 26, 2024
1 parent aeb5cf6 commit a44f555
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
7 changes: 6 additions & 1 deletion rust/src/lib/nm/settings/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,12 @@ pub(crate) fn gen_nm_conn_setting(
stable_uuid: bool,
) -> Result<(), NmstateError> {
let mut nm_conn_set = if let Some(cur_nm_conn_set) = &nm_conn.connection {
cur_nm_conn_set.clone()
let mut new_nm_conn_set = cur_nm_conn_set.clone();
// Change existing connection's profile_name if desired explicitly
if let Some(n) = iface.base_iface().profile_name.as_deref() {
new_nm_conn_set.id = Some(n.to_string());
}
new_nm_conn_set
} else {
let mut new_nm_conn_set = NmSettingConnection::default();
let conn_name =
Expand Down
18 changes: 16 additions & 2 deletions rust/src/lib/nm/show.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ pub(crate) fn nm_conn_to_base_iface(
query_nmstate_wait_ip(nm_conn.ipv4.as_ref(), nm_conn.ipv6.as_ref());
base_iface.description = get_description(nm_conn);
base_iface.identifier = Some(get_identifier(nm_conn));
base_iface.profile_name = get_connection_name(nm_conn);
base_iface.profile_name = get_connection_name(nm_conn, nm_saved_conn);
if base_iface.profile_name.as_ref() == Some(&base_iface.name) {
base_iface.profile_name = None;
}
Expand Down Expand Up @@ -572,7 +572,21 @@ fn get_identifier(nm_conn: &NmConnection) -> InterfaceIdentifier {
InterfaceIdentifier::Name
}

fn get_connection_name(nm_conn: &NmConnection) -> Option<String> {
// The applied connection will not update `connection.id` when reapply due to
// bug: https://issues.redhat.com/browse/RHEL-59548
// Hence we prefer saved NmConnection over active when they are pointing to the
// same UUID.
fn get_connection_name(
nm_conn: &NmConnection,
saved_nm_conn: Option<&NmConnection>,
) -> Option<String> {
if let Some(saved_nm_conn) = saved_nm_conn.as_ref() {
if saved_nm_conn.uuid() == nm_conn.uuid() {
if let Some(nm_set) = saved_nm_conn.connection.as_ref() {
return nm_set.id.clone();
}
}
}
if let Some(nm_set) = nm_conn.connection.as_ref() {
return nm_set.id.clone();
}
Expand Down
22 changes: 22 additions & 0 deletions tests/integration/nm/profile_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -654,3 +654,25 @@ def test_delete_mac_based_profile_using_profile_name(mac_based_profile_eth1):
)[0]
!= 0
)


# https://issues.redhat.com/browse/RHEL-59239
@pytest.mark.tier1
def test_change_profile_name(eth1_up):
desired_state = {
Interface.KEY: [
{
Interface.NAME: "eth1",
Interface.PROFILE_NAME: "eth1-new",
Interface.TYPE: InterfaceType.ETHERNET,
Interface.STATE: InterfaceState.UP,
}
]
}
libnmstate.apply(desired_state)
assert (
cmdlib.exec_cmd("nmcli -g connection.id c show eth1-new".split())[
1
].strip()
== "eth1-new"
)

0 comments on commit a44f555

Please sign in to comment.