[pdm-devel] [PATCH datacenter-manager 1/7] pdm-api-types: resources: add helper methods for fields

Dominik Csapak d.csapak at proxmox.com
Wed Apr 16 13:49:19 CEST 2025


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.

Signed-off-by: Dominik Csapak <d.csapak at proxmox.com>
---
 lib/pdm-api-types/src/resource.rs | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)

diff --git a/lib/pdm-api-types/src/resource.rs b/lib/pdm-api-types/src/resource.rs
index 6227855..0dfadd5 100644
--- a/lib/pdm-api-types/src/resource.rs
+++ b/lib/pdm-api-types/src/resource.rs
@@ -62,6 +62,33 @@ impl Resource {
             Resource::PbsDatastore(r) => r.name.as_str(),
         }
     }
+
+    pub fn resource_type(&self) -> &str {
+        match self {
+            Resource::PveStorage(_) => "storage",
+            Resource::PveQemu(_) => "qemu",
+            Resource::PveLxc(_) => "lxc",
+            Resource::PveNode(_) | Resource::PbsNode(_) => "node",
+            Resource::PbsDatastore(_) => "datastore",
+        }
+    }
+
+    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",
+        }
+    }
 }
 
 #[api(
-- 
2.39.5





More information about the pdm-devel mailing list