From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) by lore.proxmox.com (Postfix) with ESMTPS id BCB261FF191 for ; Tue, 9 Sep 2025 12:09:09 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 3FACD5A6C; Tue, 9 Sep 2025 12:09:13 +0200 (CEST) From: Stefan Hanreich To: pdm-devel@lists.proxmox.com Date: Tue, 9 Sep 2025 12:08:30 +0200 Message-ID: <20250909100838.234778-4-s.hanreich@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20250909100838.234778-1-s.hanreich@proxmox.com> References: <20250909100838.234778-1-s.hanreich@proxmox.com> MIME-Version: 1.0 X-SPAM-LEVEL: Spam detection results: 0 AWL -0.183 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 KAM_LAZY_DOMAIN_SECURITY 1 Sending domain does not have any anti-forgery methods RDNS_NONE 0.793 Delivered to internal network by a host with no rDNS SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_NONE 0.001 SPF: sender does not publish an 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. [lib.rs, resources.rs, resource.rs] Subject: [pdm-devel] [PATCH proxmox-datacenter-manager 2/5] server: api: add resources_by_type api call 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" 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 --- 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 PdmClient { Ok(self.0.get(&path).await?.expect_json()?.data) } + pub async fn resources_by_type( + &self, + max_age: Option, + resource_type: ResourceType, + ) -> Result, 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, 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 _______________________________________________ pdm-devel mailing list pdm-devel@lists.proxmox.com https://lists.proxmox.com/cgi-bin/mailman/listinfo/pdm-devel