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 C984B7A093 for ; Thu, 28 Oct 2021 15:02:04 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id BFB3720D5E for ; Thu, 28 Oct 2021 15:01:34 +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 id 0C93920D53 for ; Thu, 28 Oct 2021 15:01:34 +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 DADB245D26 for ; Thu, 28 Oct 2021 15:01:33 +0200 (CEST) From: =?UTF-8?q?Fabian=20Gr=C3=BCnbichler?= To: pbs-devel@lists.proxmox.com Date: Thu, 28 Oct 2021 15:00:49 +0200 Message-Id: <20211028130058.1308810-4-f.gruenbichler@proxmox.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20211028130058.1308810-1-f.gruenbichler@proxmox.com> References: <20211028130058.1308810-1-f.gruenbichler@proxmox.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.337 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% 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 URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [jobs.rs] Subject: [pbs-devel] [PATCH v3 proxmox-backup 02/11] api: add GroupFilter(List) type X-BeenThere: pbs-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Backup Server development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 28 Oct 2021 13:02:04 -0000 at the API level, this is a simple (wrapped) Vec of Strings with a verifier function. all users should use the provided helper to get the actual GroupFilter enum values, which can't be directly used in the API schema because of restrictions of the api macro. validation of the schema + parsing into the proper type uses the same fn intentionally to avoid running out of sync, even if it means compiling the REs twice. Signed-off-by: Fabian Grünbichler --- Notes: v2->v3: - use ArraySchema directly now that proxmox-schema supports updating Vecs - use forward_(de)_serialize_to_.. Requires bumped proxmox-schema with UpdaterType impl for Vec! pbs-api-types/src/jobs.rs | 56 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/pbs-api-types/src/jobs.rs b/pbs-api-types/src/jobs.rs index f47a294a..eda4ef95 100644 --- a/pbs-api-types/src/jobs.rs +++ b/pbs-api-types/src/jobs.rs @@ -1,3 +1,7 @@ +use anyhow::format_err; +use std::str::FromStr; + +use regex::Regex; use serde::{Deserialize, Serialize}; use proxmox_schema::*; @@ -5,6 +9,7 @@ use proxmox_schema::*; use crate::{ Userid, Authid, REMOTE_ID_SCHEMA, DRIVE_NAME_SCHEMA, MEDIA_POOL_NAME_SCHEMA, SINGLE_LINE_COMMENT_SCHEMA, PROXMOX_SAFE_ID_FORMAT, DATASTORE_SCHEMA, + BACKUP_GROUP_SCHEMA, BACKUP_TYPE_SCHEMA, }; const_regex!{ @@ -317,6 +322,57 @@ pub struct TapeBackupJobStatus { pub next_media_label: Option, } +#[derive(Clone, Debug)] +/// Filter for matching `BackupGroup`s, for use with `BackupGroup::filter`. +pub enum GroupFilter { + /// BackupGroup type - either `vm`, `ct`, or `host`. + BackupType(String), + /// Full identifier of BackupGroup, including type + Group(String), + /// A regular expression matched against the full identifier of the BackupGroup + Regex(Regex), +} + +impl std::str::FromStr for GroupFilter { + type Err = anyhow::Error; + + fn from_str(s: &str) -> Result { + match s.split_once(":") { + Some(("group", value)) => parse_simple_value(value, &BACKUP_GROUP_SCHEMA).map(|_| GroupFilter::Group(value.to_string())), + Some(("type", value)) => parse_simple_value(value, &BACKUP_TYPE_SCHEMA).map(|_| GroupFilter::BackupType(value.to_string())), + Some(("regex", value)) => Ok(GroupFilter::Regex(Regex::new(value)?)), + Some((ty, _value)) => Err(format_err!("expected 'group', 'type' or 'regex' prefix, got '{}'", ty)), + None => Err(format_err!("input doesn't match expected format '|regex:REGEX>'")), + }.map_err(|err| format_err!("'{}' - {}", s, err)) + } +} + +// used for serializing below, caution! +impl std::fmt::Display for GroupFilter { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + GroupFilter::BackupType(backup_type) => write!(f, "type:{}", backup_type), + GroupFilter::Group(backup_group) => write!(f, "group:{}", backup_group), + GroupFilter::Regex(regex) => write!(f, "regex:{}", regex.as_str()), + } + } +} + +proxmox::forward_deserialize_to_from_str!(GroupFilter); +proxmox::forward_serialize_to_display!(GroupFilter); + +fn verify_group_filter(input: &str) -> Result<(), anyhow::Error> { + GroupFilter::from_str(input).map(|_| ()) +} + +pub const GROUP_FILTER_SCHEMA: Schema = StringSchema::new( + "Group filter based on group identifier ('group:GROUP'), group type ('type:'), or regex ('regex:RE').") + .format(&ApiStringFormat::VerifyFn(verify_group_filter)) + .type_text("|group:GROUP|regex:RE>") + .schema(); + +pub const GROUP_FILTER_LIST_SCHEMA: Schema = ArraySchema::new("List of group filters.", &GROUP_FILTER_SCHEMA).schema(); + #[api( properties: { id: { -- 2.30.2