From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) by lore.proxmox.com (Postfix) with ESMTPS id 4AD8D20EC89 for ; Tue, 23 Apr 2024 13:53:43 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 0C6163098B; Tue, 23 Apr 2024 13:53:42 +0200 (CEST) From: Lukas Wagner To: pbs-devel@lists.proxmox.com Date: Tue, 23 Apr 2024 13:52:03 +0200 Message-Id: <20240423115230.170113-18-l.wagner@proxmox.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240423115230.170113-1-l.wagner@proxmox.com> References: <20240423115230.170113-1-l.wagner@proxmox.com> MIME-Version: 1.0 X-SPAM-LEVEL: Spam detection results: 0 AWL -0.048 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 POISEN_SPAM_PILL_3 0.1 random spam to be learned in bayes SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Subject: [pbs-devel] [PATCH proxmox-backup v5 17/44] api: add endpoints for querying known notification values/fields 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: , Reply-To: Proxmox Backup Server development discussion Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: pbs-devel-bounces@lists.proxmox.com Sender: "pbs-devel" These endpoints require Sys.Audit/Sys.Modify permissions on /system/notifications. Signed-off-by: Lukas Wagner Tested-by: Maximiliano Sandoval --- src/api2/config/notifications/mod.rs | 175 ++++++++++++++++++++++++++- 1 file changed, 174 insertions(+), 1 deletion(-) diff --git a/src/api2/config/notifications/mod.rs b/src/api2/config/notifications/mod.rs index 9c4a0cd9..5c2ca497 100644 --- a/src/api2/config/notifications/mod.rs +++ b/src/api2/config/notifications/mod.rs @@ -1,7 +1,22 @@ -use proxmox_router::list_subdirs_api_method; +use anyhow::Error; +use serde::Serialize; +use serde_json::Value; +use std::cmp::Ordering; + +use proxmox_router::{list_subdirs_api_method, Permission, RpcEnvironment}; use proxmox_router::{Router, SubdirMap}; +use proxmox_schema::api; use proxmox_sortable_macro::sortable; +use pbs_api_types::PRIV_SYS_AUDIT; + +use crate::api2::admin::prune::list_prune_jobs; +use crate::api2::admin::sync::list_sync_jobs; +use crate::api2::admin::verify::list_verification_jobs; +use crate::api2::config::datastore::list_datastores; +use crate::api2::config::media_pool::list_pools; +use crate::api2::tape::backup::list_tape_backup_jobs; + mod gotify; mod matchers; mod sendmail; @@ -11,6 +26,8 @@ mod targets; #[sortable] const SUBDIRS: SubdirMap = &sorted!([ ("endpoints", &ENDPOINT_ROUTER), + ("matcher-fields", &FIELD_ROUTER), + ("matcher-field-values", &VALUE_ROUTER), ("targets", &targets::ROUTER), ("matchers", &matchers::ROUTER), ]); @@ -29,3 +46,159 @@ const ENDPOINT_SUBDIRS: SubdirMap = &sorted!([ const ENDPOINT_ROUTER: Router = Router::new() .get(&list_subdirs_api_method!(ENDPOINT_SUBDIRS)) .subdirs(ENDPOINT_SUBDIRS); + +const FIELD_ROUTER: Router = Router::new().get(&API_METHOD_GET_FIELDS); +const VALUE_ROUTER: Router = Router::new().get(&API_METHOD_GET_VALUES); + +#[api] +#[derive(Serialize)] +/// A matchable field. +pub struct MatchableField { + /// Name of the field + name: String, +} + +#[api] +#[derive(Serialize)] +/// A matchable metadata field value. +pub struct MatchableValue { + /// Field this value belongs to. + field: String, + /// Notification metadata value known by the system. + value: String, + /// Additional comment for this value. + comment: Option, +} + +#[api( + protected: false, + input: { + properties: {}, + }, + returns: { + description: "List of known metadata fields.", + type: Array, + items: { type: MatchableField }, + }, + access: { + permission: &Permission::Privilege(&["system", "notifications"], PRIV_SYS_AUDIT, false), + }, +)] +/// Get all known metadata fields. +pub fn get_fields() -> Result, Error> { + let fields = ["datastore", "hostname", "job-id", "media-pool", "type"] + .into_iter() + .map(Into::into) + .map(|name| MatchableField { name }) + .collect(); + + Ok(fields) +} + +#[api( + protected: false, + input: { + properties: {}, + }, + returns: { + description: "List of known metadata field values.", + type: Array, + items: { type: MatchableValue }, + }, + access: { + permission: &Permission::Privilege(&["system", "notifications"], PRIV_SYS_AUDIT, false), + }, +)] +/// List all known, matchable metadata field values. +pub fn get_values( + param: Value, + rpcenv: &mut dyn RpcEnvironment, +) -> Result, Error> { + let mut values = Vec::new(); + + let datastores = list_datastores(param.clone(), rpcenv)?; + + for datastore in datastores { + values.push(MatchableValue { + field: "datastore".into(), + value: datastore.name.clone(), + comment: datastore.comment.clone(), + }); + } + + let pools = list_pools(rpcenv)?; + for pool in pools { + values.push(MatchableValue { + field: "media-pool".into(), + value: pool.name.clone(), + comment: None, + }); + } + + let tape_backup_jobs = list_tape_backup_jobs(param.clone(), rpcenv)?; + for job in tape_backup_jobs { + values.push(MatchableValue { + field: "job-id".into(), + value: job.config.id, + comment: job.config.comment, + }); + } + + let prune_jobs = list_prune_jobs(None, param.clone(), rpcenv)?; + for job in prune_jobs { + values.push(MatchableValue { + field: "job-id".into(), + value: job.config.id, + comment: job.config.comment, + }); + } + + let sync_jobs = list_sync_jobs(None, param.clone(), rpcenv)?; + for job in sync_jobs { + values.push(MatchableValue { + field: "job-id".into(), + value: job.config.id, + comment: job.config.comment, + }); + } + + let verify_jobs = list_verification_jobs(None, param.clone(), rpcenv)?; + for job in verify_jobs { + values.push(MatchableValue { + field: "job-id".into(), + value: job.config.id, + comment: job.config.comment, + }); + } + + values.push(MatchableValue { + field: "hostname".into(), + value: proxmox_sys::nodename().into(), + comment: None, + }); + + for ty in [ + "acme", + "gc", + "package-updates", + "prune", + "sync", + "system-mail", + "tape-backup", + "tape-load", + "verify", + ] { + values.push(MatchableValue { + field: "type".into(), + value: ty.into(), + comment: None, + }); + } + + values.sort_by(|a, b| match a.field.cmp(&b.field) { + Ordering::Equal => a.value.cmp(&b.value), + ord => ord, + }); + + Ok(values) +} -- 2.39.2 _______________________________________________ pbs-devel mailing list pbs-devel@lists.proxmox.com https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel