public inbox for pbs-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Stefan Sterz <s.sterz@proxmox.com>
To: pbs-devel@lists.proxmox.com
Subject: [pbs-devel] [PATCH proxmox 1/6] type: move `ProductType` type to `proxmox-subscription` from pom
Date: Thu,  9 Nov 2023 16:33:58 +0100	[thread overview]
Message-ID: <20231109153403.529870-2-s.sterz@proxmox.com> (raw)
In-Reply-To: <20231109153403.529870-1-s.sterz@proxmox.com>

previously this type lived inside of pom. this made it harder to
access the product type from a `SubscriptionInfo` trait in other
products. move the type here so we can check product types more
consistently across products (e. g. in pom and pbs)

Signed-off-by: Stefan Sterz <s.sterz@proxmox.com>
---
 proxmox-subscription/src/lib.rs               |  4 +-
 proxmox-subscription/src/subscription_info.rs | 51 ++++++++++++++++++-
 2 files changed, 53 insertions(+), 2 deletions(-)

diff --git a/proxmox-subscription/src/lib.rs b/proxmox-subscription/src/lib.rs
index 6c30a91..8a298f2 100644
--- a/proxmox-subscription/src/lib.rs
+++ b/proxmox-subscription/src/lib.rs
@@ -1,5 +1,7 @@
 mod subscription_info;
-pub use subscription_info::{get_hardware_address, SubscriptionInfo, SubscriptionStatus};
+pub use subscription_info::{
+    get_hardware_address, ProductType, SubscriptionInfo, SubscriptionStatus,
+};

 pub mod check;
 pub mod files;
diff --git a/proxmox-subscription/src/subscription_info.rs b/proxmox-subscription/src/subscription_info.rs
index f455392..dc10a2a 100644
--- a/proxmox-subscription/src/subscription_info.rs
+++ b/proxmox-subscription/src/subscription_info.rs
@@ -1,4 +1,4 @@
-use std::path::Path;
+use std::{fmt::Display, path::Path, str::FromStr};

 use anyhow::{bail, format_err, Error};
 use openssl::hash::{hash, DigestBytes, MessageDigest};
@@ -59,6 +59,48 @@ impl std::fmt::Display for SubscriptionStatus {
     }
 }

+#[cfg_attr(feature = "api-types", api())]
+#[cfg_attr(feature = "api-types", derive(Updater))]
+#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
+#[serde(rename_all = "lowercase")]
+/// Product type
+pub enum ProductType {
+    /// Proxmox Virtual Environment
+    Pve,
+    /// Proxmox Backup Server
+    Pbs,
+    /// Proxmox Mail Gateway
+    Pmg,
+    /// Proxmox Offline Mirror
+    Pom,
+}
+
+impl Display for ProductType {
+    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+        let txt = match self {
+            ProductType::Pve => "pve",
+            ProductType::Pbs => "pbs",
+            ProductType::Pmg => "pmg",
+            ProductType::Pom => "pom",
+        };
+        f.write_str(txt)
+    }
+}
+
+impl FromStr for ProductType {
+    type Err = Error;
+
+    fn from_str(s: &str) -> Result<Self, Self::Err> {
+        match s {
+            "pve" => Ok(ProductType::Pve),
+            "pmg" => Ok(ProductType::Pmg),
+            "pbs" => Ok(ProductType::Pbs),
+            "pom" => Ok(ProductType::Pom),
+            _ => bail!("unknown product type '{s}'"),
+        }
+    }
+}
+
 #[cfg_attr(feature = "api-types", api(
     properties: {
         status: {
@@ -237,6 +279,13 @@ impl SubscriptionInfo {
             }
         }
     }
+
+    pub fn get_product_type(&self) -> Result<ProductType, Error> {
+        self.key
+            .as_ref()
+            .ok_or_else(|| format_err!("no product key set"))
+            .map(|key| key[..3].parse::<ProductType>())?
+    }
 }

 /// Shortcut for md5 sums.
--
2.39.2





  reply	other threads:[~2023-11-09 15:34 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 ` Stefan Sterz [this message]
2023-11-27 13:12   ` [pbs-devel] applied: [PATCH proxmox 1/6] type: move `ProductType` type to `proxmox-subscription` from pom 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
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=20231109153403.529870-2-s.sterz@proxmox.com \
    --to=s.sterz@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal