From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) by lore.proxmox.com (Postfix) with ESMTPS id 03CC11FF15E for ; Tue, 14 Jan 2025 15:38:42 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 7C1D3185C5; Tue, 14 Jan 2025 15:38:24 +0100 (CET) From: Dominik Csapak To: pdm-devel@lists.proxmox.com Date: Tue, 14 Jan 2025 15:38:15 +0100 Message-Id: <20250114143816.3297378-1-d.csapak@proxmox.com> X-Mailer: git-send-email 2.39.5 MIME-Version: 1.0 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.014 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment RCVD_IN_VALIDITY_CERTIFIED_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to Validity was blocked. See https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more information. RCVD_IN_VALIDITY_RPBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to Validity was blocked. See https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more information. RCVD_IN_VALIDITY_SAFE_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to Validity was blocked. See https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more information. SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [utils.rs] Subject: [pdm-devel] [PATCH datacenter-manager v2 1/2] ui: improve foreach_drive_* functions X-BeenThere: pdm-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Datacenter Manager development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Proxmox Datacenter Manager development discussion Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: pdm-devel-bounces@lists.proxmox.com Sender: "pdm-devel" by * using the new ArrayMap feature of pve-api-types This new way of de-serialization of the pve array types such as `scsiX` makes it possible to use them directly as an iterator instead of serializing into a serde value again, so make use of that. * improve the error handling of that function Instead of aborting on the first error, pass the error through to the given function parameter and let the caller decide what to do with errors. With this, we can now remove the return value of the foreach_drive_* functions completely. Signed-off-by: Dominik Csapak --- no changes from v1 ui/src/pve/utils.rs | 114 ++++++++++++--------------- ui/src/widget/pve_migrate_mapping.rs | 22 +++++- 2 files changed, 70 insertions(+), 66 deletions(-) diff --git a/ui/src/pve/utils.rs b/ui/src/pve/utils.rs index d00a6b5..3cb45a9 100644 --- a/ui/src/pve/utils.rs +++ b/ui/src/pve/utils.rs @@ -6,7 +6,7 @@ use pdm_client::types::{ LxcConfig, LxcConfigMp, LxcConfigRootfs, LxcConfigUnused, PveQmIde, QemuConfig, QemuConfigSata, QemuConfigScsi, QemuConfigUnused, QemuConfigVirtio, }; -use proxmox_schema::ApiType; +use proxmox_schema::property_string::PropertyString; use proxmox_yew_comp::{GuestState, NodeState, StorageState}; use pwt::{ css::Opacity, @@ -148,56 +148,49 @@ impl PveDriveQemu { // note: uses to_value so we can iterate over the keys // unwrap is ok here, since we know that those are structs and strings /// Iterates over every drive from the config -pub fn foreach_drive_qemu(config: &QemuConfig, mut f: F) -> Result<(), Error> +pub fn foreach_drive_qemu(config: &QemuConfig, mut f: F) where - F: FnMut(&str, PveDriveQemu), + F: FnMut(&str, Result), { - let ide = serde_json::to_value(&config.ide)?; - for (key, value) in ide.as_object().unwrap() { - let value = serde_json::from_value( - PveQmIde::API_SCHEMA.parse_property_string(value.as_str().unwrap())?, - )?; - - f(key, PveDriveQemu::Ide(value)) + for (idx, value) in (&*config.ide).into_iter() { + let key = format!("ide{idx}"); + let res = value + .parse::>() + .map(|value| PveDriveQemu::Ide(value.into_inner())); + f(&key, res.map_err(Error::from)); } - let sata = serde_json::to_value(&config.sata)?; - for (key, value) in sata.as_object().unwrap() { - let value = serde_json::from_value( - QemuConfigSata::API_SCHEMA.parse_property_string(value.as_str().unwrap())?, - )?; - - f(key, PveDriveQemu::Sata(value)) + for (idx, value) in (&*config.sata).into_iter() { + let key = format!("sata{idx}"); + let res = value + .parse::>() + .map(|value| PveDriveQemu::Sata(value.into_inner())); + f(&key, res.map_err(Error::from)); } - let scsi = serde_json::to_value(&config.scsi)?; - for (key, value) in scsi.as_object().unwrap() { - let value = serde_json::from_value( - QemuConfigScsi::API_SCHEMA.parse_property_string(value.as_str().unwrap())?, - )?; - - f(key, PveDriveQemu::Scsi(value)) + for (idx, value) in (&*config.scsi).into_iter() { + let key = format!("scsi{idx}"); + let res = value + .parse::>() + .map(|value| PveDriveQemu::Scsi(value.into_inner())); + f(&key, res.map_err(Error::from)); } - let virtio = serde_json::to_value(&config.virtio)?; - for (key, value) in virtio.as_object().unwrap() { - let value = serde_json::from_value( - QemuConfigVirtio::API_SCHEMA.parse_property_string(value.as_str().unwrap())?, - )?; - - f(key, PveDriveQemu::Virtio(value)) + for (idx, value) in (&*config.virtio).into_iter() { + let key = format!("virtio{idx}"); + let res = value + .parse::>() + .map(|value| PveDriveQemu::Virtio(value.into_inner())); + f(&key, res.map_err(Error::from)); } - let unused = serde_json::to_value(&config.unused)?; - for (key, value) in unused.as_object().unwrap() { - let value = serde_json::from_value( - QemuConfigUnused::API_SCHEMA.parse_property_string(value.as_str().unwrap())?, - )?; - - f(key, PveDriveQemu::Unused(value)) + for (idx, value) in (&*config.unused).into_iter() { + let key = format!("unused{idx}"); + let res = value + .parse::>() + .map(|value| PveDriveQemu::Unused(value.into_inner())); + f(&key, res.map_err(Error::from)); } - - Ok(()) } /// Represents a drive from an LXC config @@ -219,34 +212,31 @@ impl PveDriveLxc { } /// Iterates over every drive from the config -pub fn foreach_drive_lxc(config: &LxcConfig, mut f: F) -> Result<(), Error> +pub fn foreach_drive_lxc(config: &LxcConfig, mut f: F) where - F: FnMut(&str, PveDriveLxc), + F: FnMut(&str, Result), { if let Some(rootfs) = &config.rootfs { - let value = - serde_json::from_value(LxcConfigRootfs::API_SCHEMA.parse_property_string(&rootfs)?)?; - - f("rootfs", PveDriveLxc::RootFs(value)) + let key = "rootfs"; + let res = rootfs + .parse::>() + .map(|value| PveDriveLxc::RootFs(value.into_inner())); + f(key, res.map_err(Error::from)); } - let mp = serde_json::to_value(&config.mp)?; - for (key, value) in mp.as_object().unwrap() { - let value = serde_json::from_value( - LxcConfigMp::API_SCHEMA.parse_property_string(value.as_str().unwrap())?, - )?; - - f(key, PveDriveLxc::Mp(value)) + for (idx, value) in (&*config.mp).into_iter() { + let key = format!("mp{idx}"); + let res = value + .parse::>() + .map(|value| PveDriveLxc::Mp(value.into_inner())); + f(&key, res.map_err(Error::from)); } - let unused = serde_json::to_value(&config.unused)?; - for (key, value) in unused.as_object().unwrap() { - let value = serde_json::from_value( - LxcConfigUnused::API_SCHEMA.parse_property_string(value.as_str().unwrap())?, - )?; - - f(key, PveDriveLxc::Unused(value)) + for (idx, value) in (&*config.unused).into_iter() { + let key = format!("unused{idx}"); + let res = value + .parse::>() + .map(|value| PveDriveLxc::Unused(value.into_inner())); + f(&key, res.map_err(Error::from)); } - - Ok(()) } diff --git a/ui/src/widget/pve_migrate_mapping.rs b/ui/src/widget/pve_migrate_mapping.rs index cce4453..b8ab3f9 100644 --- a/ui/src/widget/pve_migrate_mapping.rs +++ b/ui/src/widget/pve_migrate_mapping.rs @@ -139,7 +139,14 @@ impl PveMigrateMapComp { let mut storages = HashSet::new(); - foreach_drive_qemu(&config, |key, value| { + foreach_drive_qemu(&config, |key, res| { + let value = match res { + Ok(value) => value, + Err(err) => { + log::error!("could not parse {key}: {err}"); + return; + } + }; let file = value.get_file(); if let Some(captures) = pdm_client::types::VOLUME_ID.captures(file) { let storage = captures.get(1).unwrap(); @@ -147,7 +154,7 @@ impl PveMigrateMapComp { } else { log::error!("could not parse 'file' property of '{key}"); } - })?; + }); let mut networks = HashSet::new(); @@ -178,7 +185,14 @@ impl PveMigrateMapComp { let mut storages = HashSet::new(); - foreach_drive_lxc(&config, |key, value| { + foreach_drive_lxc(&config, |key, res| { + let value = match res { + Ok(value) => value, + Err(err) => { + log::error!("could not parse {key}: {err}"); + return; + } + }; let volume = value.get_volume(); if let Some(captures) = pdm_client::types::VOLUME_ID.captures(volume) { let storage = captures.get(1).unwrap(); @@ -186,7 +200,7 @@ impl PveMigrateMapComp { } else { log::error!("could not parse 'file' property of '{key}"); } - })?; + }); let mut networks = HashSet::new(); -- 2.39.5 _______________________________________________ pdm-devel mailing list pdm-devel@lists.proxmox.com https://lists.proxmox.com/cgi-bin/mailman/listinfo/pdm-devel