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 A83B3E3D0 for ; Tue, 18 Jul 2023 13:54:18 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 891C01A13F for ; Tue, 18 Jul 2023 13:54:18 +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 13:54:17 +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 9808E430A8 for ; Tue, 18 Jul 2023 13:54:17 +0200 (CEST) Date: Tue, 18 Jul 2023 13:54:16 +0200 From: Wolfgang Bumiller To: Lukas Wagner Cc: pve-devel@lists.proxmox.com Message-ID: References: <20230717150051.710464-1-l.wagner@proxmox.com> <20230717150051.710464-3-l.wagner@proxmox.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230717150051.710464-3-l.wagner@proxmox.com> X-SPAM-LEVEL: Spam detection results: 0 AWL 0.120 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 - URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [schema.rs, p.name, config.name] Subject: Re: [pve-devel] [PATCH v3 proxmox 02/66] notify: preparation for the first endpoint plugin 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 11:54:18 -0000 On Mon, Jul 17, 2023 at 04:59:47PM +0200, Lukas Wagner wrote: > Signed-off-by: Lukas Wagner > --- (...) > +/// Notification bus - distributes notifications to all registered endpoints > +// The reason for the split between `Config` and this struct is to make testing with mocked > +// endpoints a bit easier. > +#[derive(Default)] > +pub struct Bus { > + endpoints: HashMap>, > +} > + > +#[allow(unused_macros)] > +macro_rules! parse_endpoints_with_private_config { > + ($config:ident, $public_config:ty, $private_config:ty, $endpoint_type:ident, $type_name:expr) => { > + (|| -> Result>, Error> { > + let mut endpoints: Vec> = Vec::new(); nit: Less to type would be let mut endpoints = Vec::>::new(); ;-) > + > + let configs: Vec<$public_config> = $config > + .config > + .convert_to_typed_array($type_name) > + .map_err(|err| Error::ConfigDeserialization(err.into()))?; > + > + let private_configs: Vec<$private_config> = $config > + .private_config > + .convert_to_typed_array($type_name) > + .map_err(|err| Error::ConfigDeserialization(err.into()))?; > + > + for config in configs { > + if let Some(private_config) = private_configs.iter().find(|p| p.name == config.name) If you use `private_config` only for this kind of lookup, maybe this should use `$config.private_config.sections.get(&config.name)` and match the type name after the lookup, since you're now linear-searching an ID for something that was previously a HashMap from id to data ;-) > + { > + endpoints.push(Box::new($endpoint_type { > + config, > + private_config: private_config.clone(), > + })); > + } else { > + log::error!( > + "Could not instantiate endpoint '{name}': private config does not exist", > + name = config.name > + ); > + } > + } > + > + Ok(endpoints) > + })() > + }; > +} > + > +#[allow(unused_macros)] > +macro_rules! parse_endpoints_without_private_config { > + ($config:ident, $public_config:ty, $endpoint_type:ident, $type_name:expr) => { > + (|| -> Result>, Error> { > + let mut endpoints: Vec> = Vec::new(); > + > + let configs: Vec<$public_config> = $config > + .config > + .convert_to_typed_array($type_name) > + .map_err(|err| Error::ConfigDeserialization(err.into()))?; > + > + for config in configs { > + endpoints.push(Box::new($endpoint_type { config })); > + } > + > + Ok(endpoints) > + })() > + }; > +} > + (...) > diff --git a/proxmox-notify/src/schema.rs b/proxmox-notify/src/schema.rs > new file mode 100644 > index 00000000..68f11959 > --- /dev/null > +++ b/proxmox-notify/src/schema.rs > @@ -0,0 +1,43 @@ > +use proxmox_schema::{const_regex, ApiStringFormat, Schema, StringSchema}; > + > +// Copied from PBS > +macro_rules! proxmox_safe_id_regex_str { ^ You can drop this, since you depend on proxmox_schema, where by now we have `SAFE_ID_REGEX` and `SAFE_ID_FORMAT` if you enable the `api-types` feature :-) > + () => { > + r"(?:[A-Za-z0-9_][A-Za-z0-9._\-]*)" > + }; > +} > + > +const_regex! { > + pub SINGLE_LINE_COMMENT_REGEX = r"^[[:^cntrl:]]*$"; ^ Feel free to move this to `proxmox_schema::api_types` as well. > + pub PROXMOX_SAFE_ID_REGEX = concat!(r"^", proxmox_safe_id_regex_str!(), r"$"); > +} > + > +const SINGLE_LINE_COMMENT_FORMAT: ApiStringFormat = > + ApiStringFormat::Pattern(&SINGLE_LINE_COMMENT_REGEX); ^ And this > + > +pub const COMMENT_SCHEMA: Schema = StringSchema::new("Comment.") ^ And this. > + .format(&SINGLE_LINE_COMMENT_FORMAT) > + .max_length(128) > + .schema(); > + > +pub const EMAIL_SCHEMA: Schema = StringSchema::new("E-Mail Address.") > + .format(&SINGLE_LINE_COMMENT_FORMAT) > + .min_length(2) > + .max_length(64) > + .schema(); > + > +pub const PROXMOX_SAFE_ID_FORMAT: ApiStringFormat = > + ApiStringFormat::Pattern(&PROXMOX_SAFE_ID_REGEX); > + > +pub const BACKEND_NAME_SCHEMA: Schema = StringSchema::new("Notification backend name.") > + .format(&PROXMOX_SAFE_ID_FORMAT) > + .min_length(3) > + .max_length(32) > + .schema(); > + > +pub const ENTITY_NAME_SCHEMA: Schema = > + StringSchema::new("Name schema for endpoints, filters and groups") > + .format(&PROXMOX_SAFE_ID_FORMAT) > + .min_length(2) > + .max_length(32) > + .schema(); > -- > 2.39.2