From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id 415C19E4C1 for ; Mon, 27 Nov 2023 14:11:17 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 22C6B7AA4 for ; Mon, 27 Nov 2023 14:11:17 +0100 (CET) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [94.136.29.106]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS for ; Mon, 27 Nov 2023 14:11:16 +0100 (CET) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 43D5044B58 for ; Mon, 27 Nov 2023 14:11:16 +0100 (CET) Date: Mon, 27 Nov 2023 14:10:34 +0100 From: Fabian =?iso-8859-1?q?Gr=FCnbichler?= To: Proxmox Backup Server development discussion References: <20231109153403.529870-1-s.sterz@proxmox.com> <20231109153403.529870-5-s.sterz@proxmox.com> In-Reply-To: <20231109153403.529870-5-s.sterz@proxmox.com> MIME-Version: 1.0 User-Agent: astroid/0.16.0 (https://github.com/astroidmail/astroid) Message-Id: <1701089580.nq1gekd5wb.astroid@yuna.none> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-SPAM-LEVEL: Spam detection results: 0 AWL 0.064 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 SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record T_SCC_BODY_TEXT_LINE -0.01 - Subject: Re: [pbs-devel] [PATCH proxmox-offline-mirror 4/6] helper: improve handling of multiple keys when activating them X-BeenThere: pbs-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Backup Server development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 27 Nov 2023 13:11:17 -0000 On November 9, 2023 4:34 pm, Stefan Sterz wrote: > [..] >=20 > let server_id =3D proxmox_subscription::get_hardware_address()?; > - let subscription =3D state.subscriptions.iter().find(|s| { > - if let Some(key) =3D s.key.as_ref() { > - if let Ok(found_product) =3D key[..3].parse::()= { > - return product =3D=3D found_product; > - } > + > + let mut subscriptions: Vec<(ProductType, &SubscriptionInfo)> =3D sta= te > + .subscriptions > + .iter() > + .filter(|sub| sub.serverid.as_ref() =3D=3D Some(&server_id)) > + .filter_map(|sub| sub.get_product_type().ok().map(|prod| (prod, = sub))) > + .filter(|(found_product, _)| { > + (product.is_none() || Some(found_product) =3D=3D product.as_= ref()) > + && found_product !=3D &ProductType::Pom > + }) > + .collect(); > + > + if subscriptions.is_empty() { > + bail!("No matching subscription key found for server ID '{server= _id}'"); > + } > + > + subscriptions.sort_by(|(prod_a, sub_a), (prod_b, sub_b)| { > + if prod_a =3D=3D prod_b { > + return sub_b > + .get_next_due_date() > + .ok() > + .cmp(&sub_a.get_next_due_date().ok()); > } > - false > + > + prod_a.cmp(prod_b) > }); >=20 > - match subscription { > - Some(subscription) =3D> { > - eprintln!("Setting offline subscription key for {product}.."= ); > - match set_subscription_key(product, subscription) { > - Ok(output) if !output.is_empty() =3D> eprintln!("success= : {output}"), > - Ok(_) =3D> eprintln!("success."), > - Err(err) =3D> eprintln!("error: {err}"), > - } > - Ok(()) > + subscriptions.dedup_by(|(prod_a, _), (prod_b, _)| prod_a =3D=3D prod= _b); this is a bit opaque from a readability standpoint, would something like this as follow-up be agreeable for you? it makes the whole "try to find the single key per product" a bit more explicit. diff --git a/src/bin/proxmox-offline-mirror-helper.rs b/src/bin/proxmox-off= line-mirror-helper.rs index a231e84..def10ad 100644 --- a/src/bin/proxmox-offline-mirror-helper.rs +++ b/src/bin/proxmox-offline-mirror-helper.rs @@ -303,7 +303,7 @@ async fn setup_offline_key( =20 let server_id =3D proxmox_subscription::get_hardware_address()?; =20 - let mut subscriptions: Vec<(ProductType, &SubscriptionInfo)> =3D state + let subscriptions: HashMap =3D state .subscriptions .iter() .filter(|sub| sub.serverid.as_ref() =3D=3D Some(&server_id)) @@ -312,25 +312,21 @@ async fn setup_offline_key( (product.is_none() || Some(found_product) =3D=3D product.as_re= f()) && found_product !=3D &ProductType::Pom }) - .collect(); + .fold(HashMap::new(), |mut map, (found_product, sub)| { + if let Some(existing) =3D map.get(&found_product) { + if existing.get_next_due_date().ok() < sub.get_next_due_da= te().ok() { + map.insert(found_product, sub); + } + } else { + map.insert(found_product, sub); + } + map + }); =20 if subscriptions.is_empty() { bail!("No matching subscription key found for server ID '{server_i= d}'"); } =20 - subscriptions.sort_by(|(prod_a, sub_a), (prod_b, sub_b)| { - if prod_a =3D=3D prod_b { - return sub_b - .get_next_due_date() - .ok() - .cmp(&sub_a.get_next_due_date().ok()); - } - - prod_a.cmp(prod_b) - }); - - subscriptions.dedup_by(|(prod_a, _), (prod_b, _)| prod_a =3D=3D prod_b= ); - for (product, subscription) in subscriptions { eprintln!("Setting offline subscription key for {product}..."); match set_subscription_key(&product, subscription) { > + > + for (product, subscription) in subscriptions { > + eprintln!("Setting offline subscription key for {product}..."); > + match set_subscription_key(&product, subscription) { > + Ok(output) if !output.is_empty() =3D> eprintln!("success: {o= utput}"), > + Ok(_) =3D> eprintln!("success."), > + Err(err) =3D> eprintln!("error: {err}"), > } > - None =3D> bail!("No matching subscription key found for product = '{product}' and server ID '{server_id}'"), > } > + > + Ok(()) > } >=20 > #[api( > -- > 2.39.2