[pdm-devel] [PATCH proxmox-datacenter-manager 2/5] server: api: add resources_by_type api call

Stefan Hanreich s.hanreich at proxmox.com
Tue Sep 9 12:08:30 CEST 2025


Add an API call that returns resources of a given type. While the
search endpoint could be used for that, this endpoint is more
efficient since it doesn't instantiate the whole search logic and then
runs string comparison on all resources, but rather directly filters
by the type of the resource.

Signed-off-by: Stefan Hanreich <s.hanreich at proxmox.com>
---
 lib/pdm-api-types/src/resource.rs | 15 ++++-
 lib/pdm-client/src/lib.rs         | 14 ++++-
 server/src/api/resources.rs       | 94 +++++++++++++++++++++++++++++++
 3 files changed, 121 insertions(+), 2 deletions(-)

diff --git a/lib/pdm-api-types/src/resource.rs b/lib/pdm-api-types/src/resource.rs
index f274451..b219250 100644
--- a/lib/pdm-api-types/src/resource.rs
+++ b/lib/pdm-api-types/src/resource.rs
@@ -100,14 +100,27 @@ impl Resource {
     }
 }
 
-#[derive(Clone, Copy, Debug, PartialEq, Eq)]
+#[api]
+#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
 /// Type of a PDM resource.
 pub enum ResourceType {
+    /// PVE Storage Resource
+    #[serde(rename = "storage")]
     PveStorage,
+    /// PVE Qemu Resource
+    #[serde(rename = "qemu")]
     PveQemu,
+    /// PVE LXC Resource
+    #[serde(rename = "lxc")]
     PveLxc,
+    /// PVE SDN Resource
+    #[serde(rename = "sdn-zone")]
     PveSdnZone,
+    /// PBS Datastore Resource
+    #[serde(rename = "datastore")]
     PbsDatastore,
+    /// Node resource
+    #[serde(rename = "node")]
     Node,
 }
 
diff --git a/lib/pdm-client/src/lib.rs b/lib/pdm-client/src/lib.rs
index 845738f..ad1852e 100644
--- a/lib/pdm-client/src/lib.rs
+++ b/lib/pdm-client/src/lib.rs
@@ -4,7 +4,7 @@ use std::collections::HashMap;
 use std::time::Duration;
 
 use pdm_api_types::remotes::TlsProbeOutcome;
-use pdm_api_types::resource::{PveResource, RemoteResources, TopEntities};
+use pdm_api_types::resource::{PveResource, RemoteResources, ResourceType, TopEntities};
 use pdm_api_types::rrddata::{
     LxcDataPoint, NodeDataPoint, PbsDatastoreDataPoint, PbsNodeDataPoint, PveStorageDataPoint,
     QemuDataPoint,
@@ -865,6 +865,18 @@ impl<T: HttpApiClient> PdmClient<T> {
         Ok(self.0.get(&path).await?.expect_json()?.data)
     }
 
+    pub async fn resources_by_type(
+        &self,
+        max_age: Option<u64>,
+        resource_type: ResourceType,
+    ) -> Result<Vec<RemoteResources>, Error> {
+        let path = ApiPathBuilder::new(format!("/api2/extjs/resources/type/{resource_type}"))
+            .maybe_arg("max-age", &max_age)
+            .build();
+
+        Ok(self.0.get(&path).await?.expect_json()?.data)
+    }
+
     pub async fn pve_list_networks(
         &self,
         remote: &str,
diff --git a/server/src/api/resources.rs b/server/src/api/resources.rs
index 736bfb9..03ad03a 100644
--- a/server/src/api/resources.rs
+++ b/server/src/api/resources.rs
@@ -27,6 +27,7 @@ use proxmox_schema::{api, parse_boolean};
 use proxmox_sortable_macro::sortable;
 use proxmox_subscription::SubscriptionStatus;
 use pve_api_types::{ClusterResource, ClusterResourceType};
+use tokio::task::JoinSet;
 
 use crate::connection;
 use crate::metric_collection::top_entities;
@@ -35,9 +36,15 @@ pub const ROUTER: Router = Router::new()
     .get(&list_subdirs_api_method!(SUBDIRS))
     .subdirs(SUBDIRS);
 
+pub const TYPE_ROUTER: Router = Router::new().match_all(
+    "resource-type",
+    &Router::new().get(&API_METHOD_GET_RESOURCES_BY_TYPE),
+);
+
 #[sortable]
 const SUBDIRS: SubdirMap = &sorted!([
     ("list", &Router::new().get(&API_METHOD_GET_RESOURCES)),
+    ("type", &TYPE_ROUTER),
     ("status", &Router::new().get(&API_METHOD_GET_STATUS)),
     (
         "top-entities",
@@ -966,3 +973,90 @@ mod tests {
         }
     }
 }
+
+#[api(
+    // FIXME:: see list-like API calls in resource routers, we probably want more fine-grained
+    // checks..
+    access: {
+        permission: &Permission::Anybody,
+    },
+    input: {
+        properties: {
+            "max-age": {
+                description: "Maximum age (in seconds) of cached remote resources.",
+                // TODO: What is a sensible default max-age?
+                default: 30,
+                optional: true,
+            },
+            "resource-type": {
+                type: ResourceType,
+            },
+        }
+    },
+    returns: {
+        description: "Array of resources, grouped by remote",
+        type: Array,
+        items: {
+            type: RemoteResources,
+        }
+    },
+)]
+/// List all resources of with specific type(s).
+pub async fn get_resources_by_type(
+    max_age: u64,
+    resource_type: ResourceType,
+    rpcenv: &mut dyn RpcEnvironment,
+) -> Result<Vec<RemoteResources>, Error> {
+    let user_info = CachedUserInfo::new()?;
+
+    let auth_id: Authid = rpcenv
+        .get_auth_id()
+        .ok_or_else(|| format_err!("no authid available"))?
+        .parse()?;
+
+    if !user_info.any_privs_below(&auth_id, &["resource"], PRIV_RESOURCE_AUDIT)? {
+        http_bail!(UNAUTHORIZED, "user has no access to resources");
+    }
+
+    let (remotes_config, _) = pdm_config::remotes::config()?;
+
+    let mut join_set = JoinSet::new();
+
+    for (remote_name, remote) in remotes_config {
+        let remote_privs = user_info.lookup_privs(&auth_id, &["resource", &remote_name]);
+
+        if remote_privs & PRIV_RESOURCE_AUDIT == 0 {
+            continue;
+        }
+
+        join_set.spawn(async move {
+            let (resources, error) = match get_resources_for_remote(remote, max_age).await {
+                Ok(mut resources) => {
+                    resources.retain(|resource| resource.resource_type() == resource_type);
+                    (resources, None)
+                }
+                Err(error) => (Vec::new(), Some(error.to_string())),
+            };
+
+            RemoteResources {
+                remote: remote_name,
+                resources,
+                error,
+            }
+        });
+    }
+
+    let mut result = Vec::new();
+    while let Some(res) = join_set.join_next().await {
+        match res {
+            Ok(resources) => {
+                result.push(resources);
+            }
+            Err(error) => {
+                proxmox_log::error!("could not join get_resources task: {error:#}");
+            }
+        }
+    }
+
+    Ok(result)
+}
-- 
2.47.3




More information about the pdm-devel mailing list