From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id AD8F1E4E1 for ; Tue, 18 Jul 2023 15:02:44 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 903F01B3BD for ; Tue, 18 Jul 2023 15:02:14 +0200 (CEST) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [94.136.29.106]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS for ; Tue, 18 Jul 2023 15:02:14 +0200 (CEST) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id D34D342CAA for ; Tue, 18 Jul 2023 15:02:13 +0200 (CEST) Date: Tue, 18 Jul 2023 15:02:12 +0200 From: Wolfgang Bumiller To: Lukas Wagner Cc: pve-devel@lists.proxmox.com Message-ID: <7oiyvye7rjxoltdh4zlqqjxblvsonbvish7mv4xfplcozz56t6@t6xyhvye2wlp> References: <20230717150051.710464-1-l.wagner@proxmox.com> <20230717150051.710464-20-l.wagner@proxmox.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230717150051.710464-20-l.wagner@proxmox.com> X-SPAM-LEVEL: Spam detection results: 0 AWL 0.119 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 SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record T_SCC_BODY_TEXT_LINE -0.01 - Subject: Re: [pve-devel] [PATCH v3 proxmox 19/66] notify: api: allow to query entities referenced by filter/target X-BeenThere: pve-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox VE development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 18 Jul 2023 13:02:44 -0000 On Mon, Jul 17, 2023 at 05:00:04PM +0200, Lukas Wagner wrote: > Signed-off-by: Lukas Wagner > --- > proxmox-notify/src/api/common.rs | 11 +++ > proxmox-notify/src/api/mod.rs | 125 +++++++++++++++++++++++++++++++ > 2 files changed, 136 insertions(+) > > diff --git a/proxmox-notify/src/api/common.rs b/proxmox-notify/src/api/common.rs > index 518caa8f..48761fbb 100644 > --- a/proxmox-notify/src/api/common.rs > +++ b/proxmox-notify/src/api/common.rs > @@ -42,3 +42,14 @@ pub fn test_target(config: &Config, endpoint: &str) -> Result<(), ApiError> { > > Ok(()) > } > + > +/// Return all entities (targets, groups, filters) that are linked to the entity. > +/// For instance, if a group 'grp1' contains the targets 'a', 'b' and 'c', > +/// where grp1 has 'filter1' and 'a' has 'filter2' as filters, then > +/// the result for 'grp1' would be [grp1, a, b, c, filter1, filter2]. > +/// The result will always contain the entity that was passed as a parameter. > +/// If the entity does not exist, the result will only contain the entity. > +pub fn get_referenced_entities(config: &Config, entity: &str) -> Result, ApiError> { > + let entities = super::get_referenced_entities(config, entity); > + Ok(Vec::from_iter(entities.into_iter())) > +} > diff --git a/proxmox-notify/src/api/mod.rs b/proxmox-notify/src/api/mod.rs > index 12811baf..d8a44bf2 100644 > --- a/proxmox-notify/src/api/mod.rs > +++ b/proxmox-notify/src/api/mod.rs > @@ -1,3 +1,4 @@ > +use std::collections::HashSet; > use std::error::Error as StdError; > use std::fmt::Display; > > @@ -101,6 +102,48 @@ fn endpoint_exists(config: &Config, name: &str) -> bool { > exists > } > > +fn get_referenced_entities(config: &Config, entity: &str) -> HashSet { > + let mut to_expand = HashSet::new(); > + let mut expanded = HashSet::new(); > + to_expand.insert(entity.to_string()); > + > + let expand = |entities: &HashSet| -> HashSet { > + let mut new = HashSet::new(); > + > + for entity in entities { > + if let Ok(group) = group::get_group(config, entity) { > + for target in group.endpoint { > + new.insert(target.clone()); > + } > + } > + > + #[cfg(feature = "sendmail")] > + if let Ok(target) = sendmail::get_endpoint(config, entity) { > + if let Some(filter) = target.filter { > + new.insert(filter.clone()); > + } > + } > + > + #[cfg(feature = "gotify")] > + if let Ok(target) = gotify::get_endpoint(config, entity) { > + if let Some(filter) = target.filter { > + new.insert(filter.clone()); > + } > + } > + } > + > + new > + }; > + > + while !to_expand.is_empty() { > + let new = expand(&to_expand); > + expanded.extend(to_expand.drain()); ^ could drop the `drain()` call and move it in since you replace it with new afterwards anyway > + to_expand = new; > + } > + > + expanded > +} > + > #[cfg(test)] > mod test_helpers { > use crate::Config; (...)