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 A64DE1FF191 for ; Tue, 9 Sep 2025 17:54:55 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 21A02EB1C; Tue, 9 Sep 2025 17:54:59 +0200 (CEST) From: Stefan Hanreich To: pdm-devel@lists.proxmox.com Date: Tue, 9 Sep 2025 17:54:17 +0200 Message-ID: <20250909155423.526917-6-s.hanreich@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20250909155423.526917-1-s.hanreich@proxmox.com> References: <20250909155423.526917-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 Subject: [pdm-devel] [PATCH proxmox-datacenter-manager v2 2/5] server: api: add resource-type parameter to list_resources 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 a new parameter to the list_resources call that filters entities based on their type. While this should already be possible with the search function as well, filtering directly by enum is more efficient than building the search filters and then doing matching based on strings. The type parameter has precedence, so any search filters will be applied only on entities that have the type specified in resource-type. In pdm-client add a separate method to not pollute existing call sites with unnecessary parameters. In the future it might make sense to add another method that takes all possible combinations of parameters. Signed-off-by: Stefan Hanreich --- lib/pdm-api-types/src/resource.rs | 15 ++++++++++++++- lib/pdm-client/src/lib.rs | 15 ++++++++++++++- server/src/api/resources.rs | 18 +++++++++++++++--- server/src/resource_cache.rs | 2 +- 4 files changed, 44 insertions(+), 6 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..f2ff4d4 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,19 @@ 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("/api2/extjs/resources/list") + .maybe_arg("max-age", &max_age) + .arg("resource-type", resource_type) + .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..dcdf0ea 100644 --- a/server/src/api/resources.rs +++ b/server/src/api/resources.rs @@ -150,6 +150,10 @@ fn remote_matches_search_term(remote_name: &str, online: Option, term: &Se description: "Search term to filter for, uses special syntax e.g. ", optional: true, }, + "resource-type": { + type: ResourceType, + optional: true, + }, } }, returns: { @@ -163,10 +167,11 @@ fn remote_matches_search_term(remote_name: &str, online: Option, term: &Se /// List all resources from remote nodes. pub async fn get_resources( max_age: u64, + resource_type: Option, search: Option, rpcenv: &mut dyn RpcEnvironment, ) -> Result, Error> { - get_resources_impl(max_age, search, Some(rpcenv)).await + get_resources_impl(max_age, search, resource_type, Some(rpcenv)).await } // helper to determine if the combination of search terms requires the results @@ -208,6 +213,7 @@ fn is_remotes_only(filters: &Search) -> bool { pub(crate) async fn get_resources_impl( max_age: u64, search: Option, + resource_type: Option, rpcenv: Option<&mut dyn RpcEnvironment>, ) -> Result, Error> { let user_info = CachedUserInfo::new()?; @@ -252,8 +258,14 @@ pub(crate) async fn get_resources_impl( if remotes_only { resources.clear(); - } else if !filter.is_empty() { + } else if resource_type.is_some() || !filter.is_empty() { resources.retain(|resource| { + if let Some(resource_type) = resource_type { + if resource.resource_type() != resource_type { + return false; + } + } + filter.matches(|filter| { // if we get can't decide if it matches, don't filter it out resource_matches_search_term(resource, filter).unwrap_or(true) @@ -318,7 +330,7 @@ pub async fn get_status( max_age: u64, rpcenv: &mut dyn RpcEnvironment, ) -> Result { - let remotes = get_resources(max_age, None, rpcenv).await?; + let remotes = get_resources(max_age, None, None, rpcenv).await?; let mut counts = ResourcesStatus::default(); for remote in remotes { if remote.error.is_some() { diff --git a/server/src/resource_cache.rs b/server/src/resource_cache.rs index 0ae86ee..aa20c54 100644 --- a/server/src/resource_cache.rs +++ b/server/src/resource_cache.rs @@ -21,7 +21,7 @@ pub fn start_task() { async fn resource_caching_task() -> Result<(), Error> { loop { if let Err(err) = - crate::api::resources::get_resources_impl(METRIC_POLL_INTERVALL, None, None).await + crate::api::resources::get_resources_impl(METRIC_POLL_INTERVALL, None, None, None).await { log::error!("could not update resource cache: {err}"); } -- 2.47.3 _______________________________________________ pdm-devel mailing list pdm-devel@lists.proxmox.com https://lists.proxmox.com/cgi-bin/mailman/listinfo/pdm-devel