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 554FB95608 for ; Fri, 12 Apr 2024 12:07:18 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 883317A5E for ; Fri, 12 Apr 2024 12:06:47 +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 ; Fri, 12 Apr 2024 12:06:43 +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 5FE64451A9 for ; Fri, 12 Apr 2024 12:06:41 +0200 (CEST) From: Lukas Wagner To: pbs-devel@lists.proxmox.com Date: Fri, 12 Apr 2024 12:06:09 +0200 Message-Id: <20240412100631.94218-12-l.wagner@proxmox.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240412100631.94218-1-l.wagner@proxmox.com> References: <20240412100631.94218-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 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 11/33] 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: , X-List-Received-Date: Fri, 12 Apr 2024 10:07:18 -0000 These endpoints require Sys.Audit/Sys.Modify permissions on /system/notifications. Signed-off-by: Lukas Wagner --- src/api2/config/notifications/mod.rs | 146 ++++++++++++++++++++++++++- 1 file changed, 145 insertions(+), 1 deletion(-) diff --git a/src/api2/config/notifications/mod.rs b/src/api2/config/notifications/mod.rs index 9c4a0cd9..2c8b780e 100644 --- a/src/api2/config/notifications/mod.rs +++ b/src/api2/config/notifications/mod.rs @@ -1,7 +1,16 @@ -use proxmox_router::list_subdirs_api_method; +use anyhow::Error; +use serde::Serialize; +use serde_json::Value; + +use proxmox_router::{list_subdirs_api_method, Permission, RpcEnvironment}; use proxmox_router::{Router, SubdirMap}; +use proxmox_schema::api; use proxmox_sortable_macro::sortable; +use crate::api2::config::datastore::list_datastores; +use crate::api2::config::media_pool::list_pools; +use pbs_api_types::PRIV_SYS_AUDIT; + mod gotify; mod matchers; mod sendmail; @@ -11,6 +20,8 @@ mod targets; #[sortable] const SUBDIRS: SubdirMap = &sorted!([ ("endpoints", &ENDPOINT_ROUTER), + ("fields", &FIELD_ROUTER), + ("values", &VALUE_ROUTER), ("targets", &targets::ROUTER), ("matchers", &matchers::ROUTER), ]); @@ -29,3 +40,136 @@ 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, + /// Set if `value` was generated by the system and can safely be used + /// as a base for translations. + internal: 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( + _param: Value, + _rpcenv: &mut dyn RpcEnvironment, +) -> Result, Error> { + let fields = vec![ + MatchableField { + name: "type".into(), + }, + MatchableField { + name: "hostname".into(), + }, + MatchableField { + name: "datastore".into(), + }, + MatchableField { + name: "media-pool".into(), + }, + ]; + + 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, rpcenv)?; + + for datastore in datastores { + values.push(MatchableValue { + field: "datastore".into(), + value: datastore.name.clone(), + comment: datastore.comment.clone(), + internal: Some(false), + }); + } + + let pools = list_pools(rpcenv)?; + for pool in pools { + values.push(MatchableValue { + field: "media-pool".into(), + value: pool.name.clone(), + comment: None, + internal: Some(false), + }); + } + + values.push(MatchableValue { + field: "hostname".into(), + value: proxmox_sys::nodename().into(), + comment: None, + internal: Some(false), + }); + + for ty in [ + "prune", + "gc", + "sync", + "verify", + "tape-backup", + "tape-load", + "package-updates", + "acme", + ] { + values.push(MatchableValue { + field: "type".into(), + value: ty.into(), + comment: None, + internal: Some(true), + }); + } + + Ok(values) +} -- 2.39.2