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 F2B818214 for ; Wed, 26 Jul 2023 16:19:13 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id D02B3771 for ; Wed, 26 Jul 2023 16:18:43 +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 ; Wed, 26 Jul 2023 16:18:42 +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 9F6F245A4F for ; Wed, 26 Jul 2023 16:18:42 +0200 (CEST) From: Lukas Wagner To: pve-devel@lists.proxmox.com Date: Wed, 26 Jul 2023 16:18:24 +0200 Message-Id: <20230726141824.813621-5-l.wagner@proxmox.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230726141824.813621-1-l.wagner@proxmox.com> References: <20230726141824.813621-1-l.wagner@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL -0.055 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. [notify.rs] Subject: [pve-devel] [PATCH v2 proxmox-perl-rs 4/4] notify: use new HttpError type 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: Wed, 26 Jul 2023 14:19:14 -0000 Use `proxmox-http-error::HttpError` instead of `proxmox-notify::api::ApiError`. Also factoring out the digest decoding into a small helper. Signed-off-by: Lukas Wagner --- common/src/notify.rs | 77 +++++++++++++++++++++++--------------------- pve-rs/Cargo.toml | 1 + 2 files changed, 42 insertions(+), 36 deletions(-) diff --git a/common/src/notify.rs b/common/src/notify.rs index c8ca533..9f44225 100644 --- a/common/src/notify.rs +++ b/common/src/notify.rs @@ -5,6 +5,7 @@ mod export { use serde_json::Value as JSONValue; use std::sync::Mutex; + use proxmox_http_error::HttpError; use proxmox_notify::endpoints::gotify::{ DeleteableGotifyProperty, GotifyConfig, GotifyConfigUpdater, GotifyPrivateConfig, GotifyPrivateConfigUpdater, @@ -16,7 +17,7 @@ mod export { DeleteableFilterProperty, FilterConfig, FilterConfigUpdater, FilterModeOperator, }; use proxmox_notify::group::{DeleteableGroupProperty, GroupConfig, GroupConfigUpdater}; - use proxmox_notify::{api, api::ApiError, Config, Notification, Severity}; + use proxmox_notify::{api, Config, Notification, Severity}; pub struct NotificationConfig { config: Mutex, @@ -91,7 +92,7 @@ mod export { title: String, body: String, properties: Option, - ) -> Result<(), ApiError> { + ) -> Result<(), HttpError> { let config = this.config.lock().unwrap(); let notification = Notification { @@ -108,13 +109,15 @@ mod export { fn test_target( #[try_from_ref] this: &NotificationConfig, target: &str, - ) -> Result<(), ApiError> { + ) -> Result<(), HttpError> { let config = this.config.lock().unwrap(); api::common::test_target(&config, target) } #[export(serialize_error)] - fn get_groups(#[try_from_ref] this: &NotificationConfig) -> Result, ApiError> { + fn get_groups( + #[try_from_ref] this: &NotificationConfig, + ) -> Result, HttpError> { let config = this.config.lock().unwrap(); api::group::get_groups(&config) } @@ -123,7 +126,7 @@ mod export { fn get_group( #[try_from_ref] this: &NotificationConfig, id: &str, - ) -> Result { + ) -> Result { let config = this.config.lock().unwrap(); api::group::get_group(&config, id) } @@ -135,7 +138,7 @@ mod export { endpoints: Vec, comment: Option, filter: Option, - ) -> Result<(), ApiError> { + ) -> Result<(), HttpError> { let mut config = this.config.lock().unwrap(); api::group::add_group( &mut config, @@ -157,11 +160,9 @@ mod export { filter: Option, delete: Option>, digest: Option<&str>, - ) -> Result<(), ApiError> { + ) -> Result<(), HttpError> { let mut config = this.config.lock().unwrap(); - let digest = digest.map(hex::decode).transpose().map_err(|e| { - ApiError::internal_server_error(format!("invalid digest: {e}"), Some(Box::new(e))) - })?; + let digest = decode_digest(digest)?; api::group::update_group( &mut config, @@ -177,7 +178,10 @@ mod export { } #[export(serialize_error)] - fn delete_group(#[try_from_ref] this: &NotificationConfig, name: &str) -> Result<(), ApiError> { + fn delete_group( + #[try_from_ref] this: &NotificationConfig, + name: &str, + ) -> Result<(), HttpError> { let mut config = this.config.lock().unwrap(); api::group::delete_group(&mut config, name) } @@ -185,7 +189,7 @@ mod export { #[export(serialize_error)] fn get_sendmail_endpoints( #[try_from_ref] this: &NotificationConfig, - ) -> Result, ApiError> { + ) -> Result, HttpError> { let config = this.config.lock().unwrap(); api::sendmail::get_endpoints(&config) } @@ -194,7 +198,7 @@ mod export { fn get_sendmail_endpoint( #[try_from_ref] this: &NotificationConfig, id: &str, - ) -> Result { + ) -> Result { let config = this.config.lock().unwrap(); api::sendmail::get_endpoint(&config, id) } @@ -210,7 +214,7 @@ mod export { author: Option, comment: Option, filter: Option, - ) -> Result<(), ApiError> { + ) -> Result<(), HttpError> { let mut config = this.config.lock().unwrap(); api::sendmail::add_endpoint( @@ -240,11 +244,9 @@ mod export { filter: Option, delete: Option>, digest: Option<&str>, - ) -> Result<(), ApiError> { + ) -> Result<(), HttpError> { let mut config = this.config.lock().unwrap(); - let digest = digest.map(hex::decode).transpose().map_err(|e| { - ApiError::internal_server_error(format!("invalid digest: {e}"), Some(Box::new(e))) - })?; + let digest = decode_digest(digest)?; api::sendmail::update_endpoint( &mut config, @@ -266,7 +268,7 @@ mod export { fn delete_sendmail_endpoint( #[try_from_ref] this: &NotificationConfig, name: &str, - ) -> Result<(), ApiError> { + ) -> Result<(), HttpError> { let mut config = this.config.lock().unwrap(); api::sendmail::delete_endpoint(&mut config, name) } @@ -274,7 +276,7 @@ mod export { #[export(serialize_error)] fn get_gotify_endpoints( #[try_from_ref] this: &NotificationConfig, - ) -> Result, ApiError> { + ) -> Result, HttpError> { let config = this.config.lock().unwrap(); api::gotify::get_endpoints(&config) } @@ -283,7 +285,7 @@ mod export { fn get_gotify_endpoint( #[try_from_ref] this: &NotificationConfig, id: &str, - ) -> Result { + ) -> Result { let config = this.config.lock().unwrap(); api::gotify::get_endpoint(&config, id) } @@ -296,7 +298,7 @@ mod export { token: String, comment: Option, filter: Option, - ) -> Result<(), ApiError> { + ) -> Result<(), HttpError> { let mut config = this.config.lock().unwrap(); api::gotify::add_endpoint( &mut config, @@ -321,11 +323,9 @@ mod export { filter: Option, delete: Option>, digest: Option<&str>, - ) -> Result<(), ApiError> { + ) -> Result<(), HttpError> { let mut config = this.config.lock().unwrap(); - let digest = digest.map(hex::decode).transpose().map_err(|e| { - ApiError::internal_server_error(format!("invalid digest: {e}"), Some(Box::new(e))) - })?; + let digest = decode_digest(digest)?; api::gotify::update_endpoint( &mut config, @@ -345,7 +345,7 @@ mod export { fn delete_gotify_endpoint( #[try_from_ref] this: &NotificationConfig, name: &str, - ) -> Result<(), ApiError> { + ) -> Result<(), HttpError> { let mut config = this.config.lock().unwrap(); api::gotify::delete_gotify_endpoint(&mut config, name) } @@ -353,7 +353,7 @@ mod export { #[export(serialize_error)] fn get_filters( #[try_from_ref] this: &NotificationConfig, - ) -> Result, ApiError> { + ) -> Result, HttpError> { let config = this.config.lock().unwrap(); api::filter::get_filters(&config) } @@ -362,7 +362,7 @@ mod export { fn get_filter( #[try_from_ref] this: &NotificationConfig, id: &str, - ) -> Result { + ) -> Result { let config = this.config.lock().unwrap(); api::filter::get_filter(&config, id) } @@ -376,7 +376,7 @@ mod export { mode: Option, invert_match: Option, comment: Option, - ) -> Result<(), ApiError> { + ) -> Result<(), HttpError> { let mut config = this.config.lock().unwrap(); api::filter::add_filter( &mut config, @@ -401,11 +401,9 @@ mod export { comment: Option, delete: Option>, digest: Option<&str>, - ) -> Result<(), ApiError> { + ) -> Result<(), HttpError> { let mut config = this.config.lock().unwrap(); - let digest = digest.map(hex::decode).transpose().map_err(|e| { - ApiError::internal_server_error(format!("invalid digest: {e}"), Some(Box::new(e))) - })?; + let digest = decode_digest(digest)?; api::filter::update_filter( &mut config, @@ -425,7 +423,7 @@ mod export { fn delete_filter( #[try_from_ref] this: &NotificationConfig, name: &str, - ) -> Result<(), ApiError> { + ) -> Result<(), HttpError> { let mut config = this.config.lock().unwrap(); api::filter::delete_filter(&mut config, name) } @@ -434,8 +432,15 @@ mod export { fn get_referenced_entities( #[try_from_ref] this: &NotificationConfig, name: &str, - ) -> Result, ApiError> { + ) -> Result, HttpError> { let config = this.config.lock().unwrap(); api::common::get_referenced_entities(&config, name) } + + fn decode_digest(digest: Option<&str>) -> Result>, HttpError> { + digest + .map(hex::decode) + .transpose() + .map_err(|e| api::http_err!(BAD_REQUEST, "invalid digest: {e}")) + } } diff --git a/pve-rs/Cargo.toml b/pve-rs/Cargo.toml index 2b375c0..222fdf5 100644 --- a/pve-rs/Cargo.toml +++ b/pve-rs/Cargo.toml @@ -35,6 +35,7 @@ perlmod = { version = "0.13", features = [ "exporter" ] } proxmox-apt = "0.10" proxmox-http = { version = "0.9", features = ["client-sync", "client-trait"] } +proxmox-http-error = "0.1.0" proxmox-notify = "0.1" proxmox-openid = "0.10" proxmox-resource-scheduling = "0.3.0" -- 2.39.2