From: "Fabian Grünbichler" <f.gruenbichler@proxmox.com>
To: Proxmox Backup Server development discussion
<pbs-devel@lists.proxmox.com>
Subject: Re: [pbs-devel] [PATCH proxmox-offline-mirror 4/6] helper: improve handling of multiple keys when activating them
Date: Mon, 27 Nov 2023 14:10:34 +0100 [thread overview]
Message-ID: <1701089580.nq1gekd5wb.astroid@yuna.none> (raw)
In-Reply-To: <20231109153403.529870-5-s.sterz@proxmox.com>
On November 9, 2023 4:34 pm, Stefan Sterz wrote:
> [..]
>
> let server_id = proxmox_subscription::get_hardware_address()?;
> - let subscription = state.subscriptions.iter().find(|s| {
> - if let Some(key) = s.key.as_ref() {
> - if let Ok(found_product) = key[..3].parse::<ProductType>() {
> - return product == found_product;
> - }
> +
> + let mut subscriptions: Vec<(ProductType, &SubscriptionInfo)> = state
> + .subscriptions
> + .iter()
> + .filter(|sub| sub.serverid.as_ref() == Some(&server_id))
> + .filter_map(|sub| sub.get_product_type().ok().map(|prod| (prod, sub)))
> + .filter(|(found_product, _)| {
> + (product.is_none() || Some(found_product) == product.as_ref())
> + && found_product != &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 == prod_b {
> + return sub_b
> + .get_next_due_date()
> + .ok()
> + .cmp(&sub_a.get_next_due_date().ok());
> }
> - false
> +
> + prod_a.cmp(prod_b)
> });
>
> - match subscription {
> - Some(subscription) => {
> - eprintln!("Setting offline subscription key for {product}..");
> - match set_subscription_key(product, subscription) {
> - Ok(output) if !output.is_empty() => eprintln!("success: {output}"),
> - Ok(_) => eprintln!("success."),
> - Err(err) => eprintln!("error: {err}"),
> - }
> - Ok(())
> + subscriptions.dedup_by(|(prod_a, _), (prod_b, _)| prod_a == 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-offline-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(
let server_id = proxmox_subscription::get_hardware_address()?;
- let mut subscriptions: Vec<(ProductType, &SubscriptionInfo)> = state
+ let subscriptions: HashMap<ProductType, &SubscriptionInfo> = state
.subscriptions
.iter()
.filter(|sub| sub.serverid.as_ref() == Some(&server_id))
@@ -312,25 +312,21 @@ async fn setup_offline_key(
(product.is_none() || Some(found_product) == product.as_ref())
&& found_product != &ProductType::Pom
})
- .collect();
+ .fold(HashMap::new(), |mut map, (found_product, sub)| {
+ if let Some(existing) = map.get(&found_product) {
+ if existing.get_next_due_date().ok() < sub.get_next_due_date().ok() {
+ map.insert(found_product, sub);
+ }
+ } else {
+ map.insert(found_product, sub);
+ }
+ map
+ });
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 == 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 == 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() => eprintln!("success: {output}"),
> + Ok(_) => eprintln!("success."),
> + Err(err) => eprintln!("error: {err}"),
> }
> - None => bail!("No matching subscription key found for product '{product}' and server ID '{server_id}'"),
> }
> +
> + Ok(())
> }
>
> #[api(
> --
> 2.39.2
next prev parent reply other threads:[~2023-11-27 13:11 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-11-09 15:33 [pbs-devel] [PATCH offline-mirror/proxmox/backup-server 0/6] improve pom multi-key handling and pbs key check Stefan Sterz
2023-11-09 15:33 ` [pbs-devel] [PATCH proxmox 1/6] type: move `ProductType` type to `proxmox-subscription` from pom Stefan Sterz
2023-11-27 13:12 ` [pbs-devel] applied: " Fabian Grünbichler
2023-11-09 15:33 ` [pbs-devel] [PATCH proxmox 2/6] subscription: expose the `next_due_date` as an `i64` Stefan Sterz
2023-11-27 13:12 ` [pbs-devel] applied: " Fabian Grünbichler
2023-11-09 15:34 ` [pbs-devel] [PATCH proxmox-offline-mirror 3/6] type: move `ProductType` enum to `proxmox-subscription` Stefan Sterz
2023-11-09 15:34 ` [pbs-devel] [PATCH proxmox-offline-mirror 4/6] helper: improve handling of multiple keys when activating them Stefan Sterz
2023-11-27 13:10 ` Fabian Grünbichler [this message]
2023-11-09 15:34 ` [pbs-devel] [PATCH proxmox-offline-mirror 5/6] offline mirror binary: rustfmt clean up Stefan Sterz
2023-11-09 15:34 ` [pbs-devel] [PATCH proxmox-backup 6/6] manager: check if offline subscription is for the correct product Stefan Sterz
2023-11-27 13:14 ` [pbs-devel] applied: " Fabian Grünbichler
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=1701089580.nq1gekd5wb.astroid@yuna.none \
--to=f.gruenbichler@proxmox.com \
--cc=pbs-devel@lists.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.