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 B61D6B9718 for ; Mon, 11 Dec 2023 13:50:38 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 97F001615B for ; Mon, 11 Dec 2023 13:50:08 +0100 (CET) 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 ; Mon, 11 Dec 2023 13:50:07 +0100 (CET) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 37F404538A for ; Mon, 11 Dec 2023 13:50:07 +0100 (CET) From: Lukas Wagner To: pve-devel@lists.proxmox.com Date: Mon, 11 Dec 2023 13:49:36 +0100 Message-Id: <20231211124955.211219-3-l.wagner@proxmox.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20231211124955.211219-1-l.wagner@proxmox.com> References: <20231211124955.211219-1-l.wagner@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL -0.007 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: [pve-devel] [PATCH proxmox 02/21] notify: matcher: support lists of values for 'exact' match-field mode 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: Mon, 11 Dec 2023 12:50:38 -0000 For example, one can now use: match-field exact:type=vzdump,replication to match on vzdump AND replication events. Signed-off-by: Lukas Wagner --- proxmox-notify/src/matcher.rs | 43 +++++++++++++++++++++++++++-------- 1 file changed, 34 insertions(+), 9 deletions(-) diff --git a/proxmox-notify/src/matcher.rs b/proxmox-notify/src/matcher.rs index 42d988a..3fdc887 100644 --- a/proxmox-notify/src/matcher.rs +++ b/proxmox-notify/src/matcher.rs @@ -160,7 +160,7 @@ trait MatchDirective { pub enum FieldMatcher { Exact { field: String, - matched_value: String, + matched_values: Vec, }, Regex { field: String, @@ -176,12 +176,12 @@ impl MatchDirective for FieldMatcher { Ok(match self { FieldMatcher::Exact { field, - matched_value, + matched_values, } => { let value = notification.metadata.additional_fields.get(field); if let Some(value) = value { - matched_value == value + matched_values.contains(value) } else { // Metadata field does not exist, so we do not match false @@ -212,9 +212,10 @@ impl fmt::Display for FieldMatcher { match self { FieldMatcher::Exact { field, - matched_value, + matched_values, } => { - write!(f, "exact:{field}={matched_value}") + let values = matched_values.join(","); + write!(f, "exact:{field}={values}") } FieldMatcher::Regex { field, @@ -256,10 +257,17 @@ impl FromStr for FieldMatcher { None => Err(Error::FilterFailed(format!( "invalid match-field statement: {s}" ))), - Some((field, expected_value)) => Ok(Self::Exact { - field: field.into(), - matched_value: expected_value.into(), - }), + Some((field, expected_values)) => { + let values: Vec = expected_values + .split(',') + .map(str::trim) + .map(String::from) + .collect(); + Ok(Self::Exact { + field: field.into(), + matched_values: values, + }) + } } } else { Err(Error::FilterFailed(format!( @@ -460,6 +468,23 @@ mod tests { let matcher: FieldMatcher = "regex:notthere=b.*".parse().unwrap(); assert!(!matcher.matches(¬ification).unwrap()); + let matcher: FieldMatcher = "exact:foo=bar,test".parse().unwrap(); + assert!(matcher.matches(¬ification).unwrap()); + + let mut fields = HashMap::new(); + fields.insert("foo".into(), "test".into()); + + let notification = + Notification::new_templated(Severity::Notice, "test", "test", Value::Null, fields); + assert!(matcher.matches(¬ification).unwrap()); + + let mut fields = HashMap::new(); + fields.insert("foo".into(), "notthere".into()); + + let notification = + Notification::new_templated(Severity::Notice, "test", "test", Value::Null, fields); + assert!(!matcher.matches(¬ification).unwrap()); + assert!("regex:'3=b.*".parse::().is_err()); assert!("invalid:'bar=b.*".parse::().is_err()); } -- 2.39.2