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 82E9DA0B1A for ; Thu, 9 Nov 2023 16:34:46 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 6809117052 for ; Thu, 9 Nov 2023 16:34:16 +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) server-digest SHA256) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS for ; Thu, 9 Nov 2023 16:34:15 +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 634E6476E4 for ; Thu, 9 Nov 2023 16:34:15 +0100 (CET) From: Stefan Sterz To: pbs-devel@lists.proxmox.com Date: Thu, 9 Nov 2023 16:33:58 +0100 Message-Id: <20231109153403.529870-2-s.sterz@proxmox.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20231109153403.529870-1-s.sterz@proxmox.com> References: <20231109153403.529870-1-s.sterz@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL -0.088 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 - URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [lib.rs] Subject: [pbs-devel] [PATCH proxmox 1/6] type: move `ProductType` type to `proxmox-subscription` from pom 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: Thu, 09 Nov 2023 15:34:46 -0000 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 --- 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 { + 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 { + self.key + .as_ref() + .ok_or_else(|| format_err!("no product key set")) + .map(|key| key[..3].parse::())? + } } /// Shortcut for md5 sums. -- 2.39.2