From: Wolfgang Bumiller <w.bumiller@proxmox.com>
To: Stefan Hanreich <s.hanreich@proxmox.com>
Cc: pbs-devel@lists.proxmox.com
Subject: Re: [pbs-devel] [PATCH proxmox v4 3/3] network-api: add rename_interfaces method
Date: Mon, 4 Aug 2025 14:55:48 +0200 [thread overview]
Message-ID: <4bs5sbsu3jnjtxtz3jiryeepw6od4nd2dd6anzdcc4n66lthbv@eqqjtpokkw2m> (raw)
In-Reply-To: <20250731140855.573717-5-s.hanreich@proxmox.com>
On Thu, Jul 31, 2025 at 04:08:47PM +0200, Stefan Hanreich wrote:
> Used for batch renaming interfaces in the /e/n/i configuration file by
> the proxmox-network-interface-pinning tool.
>
> Signed-off-by: Stefan Hanreich <s.hanreich@proxmox.com>
> Tested-by: Christian Ebner <c.ebner@proxmox.com>
> ---
> proxmox-network-api/src/config/mod.rs | 68 +++++++++++++++++++++++++++
> 1 file changed, 68 insertions(+)
>
> diff --git a/proxmox-network-api/src/config/mod.rs b/proxmox-network-api/src/config/mod.rs
> index e8cb81d1..3b2ffdc2 100644
> --- a/proxmox-network-api/src/config/mod.rs
> +++ b/proxmox-network-api/src/config/mod.rs
> @@ -267,6 +267,74 @@ impl NetworkConfig {
> Ok(interface)
> }
>
> + pub fn rename_interfaces(&mut self, mapping: &HashMap<String, String>) -> Result<(), Error> {
> + for (old_name, new_name) in mapping.iter() {
> + self.interfaces
> + .remove(old_name)
> + .map(|interface| self.interfaces.insert(new_name.to_string(), interface));
A freestanding `.map` with the `map` doing a thing like that is bad
style.
And shouldn't we also update `.name` right away?
Also consider that we should probably issue a warning if the new name
already existed?
if let Some(mut interface) == self.interfaces.remove(old_name) {
interface.name = new_name.clone();
if self.interfaces.insert(new_name.to_string(), interface).is_some() {
warn about this
}
}
> +
> + if let Some(idx) = self
> + .order
> + .iter()
> + .position(|elem| matches!(elem, NetworkOrderEntry::Iface(name) if name == new_name))
Shouldn't this check for `old_name`?
Also, could use `iter_mut().find()` and then assign to resulting mut ref
if let Some(elem) = self.order.iter_mut().find(...) {
*elem = ...
}
> + {
> + self.order[idx] = NetworkOrderEntry::Iface(new_name.to_string());
> + }
> + }
> +
> + for interface in self.interfaces.values_mut() {
> + if let Some(new_name) = mapping.get(&interface.name) {
> + interface.name = new_name.to_string();
> + }
> +
> + if let Some(bridge_ports) = interface.bridge_ports.take() {
> + interface.bridge_ports = Some(
> + bridge_ports
> + .into_iter()
> + .map(|interface| {
> + mapping
> + .get(&interface)
> + .map(String::from)
could just use `.cloned()`
> + .unwrap_or(interface)
> + })
> + .collect(),
> + )
> + }
> +
> + if let Some(vlan_raw_device) = interface.vlan_raw_device.take() {
> + if let Some(new_name) = mapping.get(&vlan_raw_device) {
> + interface.vlan_raw_device = Some(new_name.to_string());
> + } else {
> + interface.vlan_raw_device = Some(vlan_raw_device);
> + }
> + }
could skip the else if we modify in-place with `.as_mut()`:
if let Some(vlan_raw_device) = interface.vlan_raw_device.as_mut() {
if let Some(new_name) = mapping.get(&*vlan_raw_device) { // note the extra '*' deref
*vlan_raw_device = Some(new_name.clone());
}
}
> +
> + if let Some(slaves) = interface.slaves.take() {
> + interface.slaves = Some(
> + slaves
> + .into_iter()
> + .map(|interface| {
> + mapping
> + .get(&interface)
> + .map(String::from)
> + .unwrap_or(interface)
> + })
> + .collect(),
> + )
> + }
> +
> + if let Some(bond_primary) = interface.bond_primary.take() {
same as the vlan_raw_device case
> + if let Some(new_name) = mapping.get(&bond_primary) {
> + interface.bond_primary = Some(new_name.to_string());
> + } else {
> + interface.bond_primary = Some(bond_primary);
> + }
> + }
> + }
> +
> + Ok(())
> + }
> +
> /// Check that there is no other gateway.
> ///
> /// The gateway property is only allowed on passed 'iface'. This should be
> --
> 2.47.2
_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel
next prev parent reply other threads:[~2025-08-04 12:54 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-07-31 14:08 [pbs-devel] [PATCH proxmox{-ve-rs, , -backup, -firewall, -network-interface-pinning} v4 00/10] proxmox-network-interface-pinning Stefan Hanreich
2025-07-31 14:08 ` [pbs-devel] [PATCH proxmox-ve-rs v4 1/1] host: network: move to proxmox-network-api Stefan Hanreich
2025-07-31 14:08 ` [pbs-devel] [PATCH proxmox v4 1/3] pbs-api-types: use proxmox-network-api types Stefan Hanreich
2025-07-31 14:08 ` [pbs-devel] [PATCH proxmox v4 2/3] proxmox-network-api: use ip link for querying interface information Stefan Hanreich
2025-07-31 14:08 ` [pbs-devel] [PATCH proxmox v4 3/3] network-api: add rename_interfaces method Stefan Hanreich
2025-08-04 12:55 ` Wolfgang Bumiller [this message]
2025-08-04 14:24 ` Stefan Hanreich
2025-07-31 14:08 ` [pbs-devel] [PATCH proxmox-backup v4 1/4] config: network: move to proxmox-network-api Stefan Hanreich
2025-07-31 14:08 ` [pbs-devel] [PATCH proxmox-backup v4 2/4] metric_collection: use ip link for determining the type of interfaces Stefan Hanreich
2025-07-31 14:08 ` [pbs-devel] [PATCH proxmox-backup v4 3/4] docs: add documentation for proxmox-network-interface-pinning Stefan Hanreich
2025-07-31 14:08 ` [pbs-devel] [PATCH proxmox-backup v4 4/4] ui: show altnames Stefan Hanreich
2025-07-31 14:08 ` [pbs-devel] [PATCH proxmox-firewall v4 1/1] firewall: config: use proxmox-network-api Stefan Hanreich
2025-07-31 14:08 ` [pbs-devel] [PATCH proxmox-network-interface-pinning v4 1/1] initial commit Stefan Hanreich
2025-08-04 13:48 ` Wolfgang Bumiller
2025-08-04 14:46 ` Stefan Hanreich
2025-08-01 9:51 ` [pbs-devel] [PATCH proxmox{-ve-rs, , -backup, -firewall, -network-interface-pinning} v4 00/10] proxmox-network-interface-pinning Christian Ebner
2025-08-01 10:26 ` Christian Ebner
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=4bs5sbsu3jnjtxtz3jiryeepw6od4nd2dd6anzdcc4n66lthbv@eqqjtpokkw2m \
--to=w.bumiller@proxmox.com \
--cc=pbs-devel@lists.proxmox.com \
--cc=s.hanreich@proxmox.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox