From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [IPv6:2a01:7e0:0:424::9]) by lore.proxmox.com (Postfix) with ESMTPS id 87BCD1FF17C for ; Wed, 3 Sep 2025 15:23:44 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 8E9C7DDB9; Wed, 3 Sep 2025 15:23:55 +0200 (CEST) From: Dominik Csapak To: pdm-devel@lists.proxmox.com Date: Wed, 3 Sep 2025 15:09:17 +0200 Message-ID: <20250903132351.841830-2-d.csapak@proxmox.com> X-Mailer: git-send-email 2.47.2 In-Reply-To: <20250903132351.841830-1-d.csapak@proxmox.com> References: <20250903132351.841830-1-d.csapak@proxmox.com> MIME-Version: 1.0 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.020 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_MSPIKE_H2 0.001 Average reputation (+2) 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. [resource.rs] Subject: [pdm-devel] [PATCH datacenter-manager v5 01/10] pdm-api-types: resources: add helper methods for fields 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" namely 'resource_type' and 'status'. All resources have those fields in one way or another, so adding a helper that one does not have to use `match` on every call site makes code there shorter. Adds a ResourceType enum as a helper, since that can come in handy when comparing/showing/etc. the types Signed-off-by: Dominik Csapak --- changes from v4: * added ResourceType enum, that converts from str and to str lib/pdm-api-types/src/resource.rs | 72 +++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/lib/pdm-api-types/src/resource.rs b/lib/pdm-api-types/src/resource.rs index 6227855..fd2d49b 100644 --- a/lib/pdm-api-types/src/resource.rs +++ b/lib/pdm-api-types/src/resource.rs @@ -1,3 +1,4 @@ +use anyhow::{bail, Error}; use serde::{Deserialize, Serialize}; use proxmox_schema::api; @@ -62,6 +63,77 @@ impl Resource { Resource::PbsDatastore(r) => r.name.as_str(), } } + + pub fn resource_type(&self) -> ResourceType { + match self { + Resource::PveStorage(_) => ResourceType::PveStorage, + Resource::PveQemu(_) => ResourceType::PveQemu, + Resource::PveLxc(_) => ResourceType::PveLxc, + Resource::PveNode(_) | Resource::PbsNode(_) => ResourceType::Node, + Resource::PbsDatastore(_) => ResourceType::PbsDatastore, + } + } + + pub fn status(&self) -> &str { + match self { + Resource::PveStorage(r) => r.status.as_str(), + Resource::PveQemu(r) => r.status.as_str(), + Resource::PveLxc(r) => r.status.as_str(), + Resource::PveNode(r) => r.status.as_str(), + Resource::PbsNode(r) => { + if r.uptime > 0 { + "online" + } else { + "offline" + } + } + Resource::PbsDatastore(_) => "online", + } + } +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum ResourceType { + PveStorage, + PveQemu, + PveLxc, + PbsDatastore, + Node, +} + +impl ResourceType { + /// Returns a string representation of the type + pub fn as_str(&self) -> &'static str { + match self { + ResourceType::PveStorage => "storage", + ResourceType::PveQemu => "qemu", + ResourceType::PveLxc => "lxc", + ResourceType::PbsDatastore => "datastore", + ResourceType::Node => "node", + } + } +} + +impl std::fmt::Display for ResourceType { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}", self.as_str()) + } +} + +impl std::str::FromStr for ResourceType { + type Err = Error; + + fn from_str(s: &str) -> Result { + let resource_type = match s { + "storage" => ResourceType::PveStorage, + "qemu" => ResourceType::PveQemu, + "lxc" => ResourceType::PveLxc, + "datastore" => ResourceType::PbsDatastore, + "node" => ResourceType::Node, + _ => bail!("invalid resource type"), + }; + Ok(resource_type) + } } #[api( -- 2.47.2 _______________________________________________ pdm-devel mailing list pdm-devel@lists.proxmox.com https://lists.proxmox.com/cgi-bin/mailman/listinfo/pdm-devel