[pbs-devel] [PATCH proxmox-backup v4 35/43] proxmox-backup-manager: add CLI for notification targets

Lukas Wagner l.wagner at proxmox.com
Mon Apr 22 14:38:33 CEST 2024


Signed-off-by: Lukas Wagner <l.wagner at proxmox.com>
Tested-by: Gabriel Goller <g.goller at proxmox.com>
Reviewed-by: Gabriel Goller <g.goller at proxmox.com>
---
 src/api2/config/notifications/mod.rs          | 10 ++--
 src/bin/proxmox-backup-manager.rs             |  2 +
 src/bin/proxmox_backup_manager/mod.rs         |  2 +
 .../notifications/mod.rs                      |  9 ++++
 .../notifications/targets.rs                  | 51 +++++++++++++++++++
 5 files changed, 69 insertions(+), 5 deletions(-)
 create mode 100644 src/bin/proxmox_backup_manager/notifications/mod.rs
 create mode 100644 src/bin/proxmox_backup_manager/notifications/targets.rs

diff --git a/src/api2/config/notifications/mod.rs b/src/api2/config/notifications/mod.rs
index 804626b0..b5589407 100644
--- a/src/api2/config/notifications/mod.rs
+++ b/src/api2/config/notifications/mod.rs
@@ -17,11 +17,11 @@ 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;
-mod smtp;
-mod targets;
+pub mod gotify;
+pub mod matchers;
+pub mod sendmail;
+pub mod smtp;
+pub mod targets;
 
 #[sortable]
 const SUBDIRS: SubdirMap = &sorted!([
diff --git a/src/bin/proxmox-backup-manager.rs b/src/bin/proxmox-backup-manager.rs
index 115207f3..132de290 100644
--- a/src/bin/proxmox-backup-manager.rs
+++ b/src/bin/proxmox-backup-manager.rs
@@ -430,6 +430,7 @@ async fn get_versions(verbose: bool, param: Value) -> Result<Value, Error> {
 
 async fn run() -> Result<(), Error> {
     init_cli_logger("PBS_LOG", "info");
+    proxmox_backup::server::notifications::init()?;
 
     let cmd_def = CliCommandMap::new()
         .insert("acl", acl_commands())
@@ -439,6 +440,7 @@ async fn run() -> Result<(), Error> {
         .insert("ldap", ldap_commands())
         .insert("network", network_commands())
         .insert("node", node_commands())
+        .insert("notification", notification_commands())
         .insert("user", user_commands())
         .insert("openid", openid_commands())
         .insert("remote", remote_commands())
diff --git a/src/bin/proxmox_backup_manager/mod.rs b/src/bin/proxmox_backup_manager/mod.rs
index 8a1c140c..8992a7a7 100644
--- a/src/bin/proxmox_backup_manager/mod.rs
+++ b/src/bin/proxmox_backup_manager/mod.rs
@@ -28,6 +28,8 @@ mod disk;
 pub use disk::*;
 mod node;
 pub use node::*;
+mod notifications;
+pub use notifications::*;
 mod openid;
 pub use openid::*;
 mod traffic_control;
diff --git a/src/bin/proxmox_backup_manager/notifications/mod.rs b/src/bin/proxmox_backup_manager/notifications/mod.rs
new file mode 100644
index 00000000..fdd5abe3
--- /dev/null
+++ b/src/bin/proxmox_backup_manager/notifications/mod.rs
@@ -0,0 +1,9 @@
+use proxmox_router::cli::{CliCommandMap, CommandLineInterface};
+
+mod targets;
+
+pub fn notification_commands() -> CommandLineInterface {
+    let cmd_def = CliCommandMap::new().insert("target", targets::commands());
+
+    cmd_def.into()
+}
diff --git a/src/bin/proxmox_backup_manager/notifications/targets.rs b/src/bin/proxmox_backup_manager/notifications/targets.rs
new file mode 100644
index 00000000..37603a92
--- /dev/null
+++ b/src/bin/proxmox_backup_manager/notifications/targets.rs
@@ -0,0 +1,51 @@
+use anyhow::Error;
+use serde_json::Value;
+
+use proxmox_router::{cli::*, ApiHandler, RpcEnvironment};
+use proxmox_schema::api;
+
+use proxmox_backup::api2;
+
+#[api(
+    input: {
+        properties: {
+            "output-format": {
+                schema: OUTPUT_FORMAT,
+                optional: true,
+            },
+        }
+    }
+)]
+/// List targets.
+fn list_targets(param: Value, rpcenv: &mut dyn RpcEnvironment) -> Result<Value, Error> {
+    let output_format = get_output_format(&param);
+
+    let info = &api2::config::notifications::targets::API_METHOD_LIST_TARGETS;
+    let mut data = match info.handler {
+        ApiHandler::Sync(handler) => (handler)(param, info, rpcenv)?,
+        _ => unreachable!(),
+    };
+
+    let options = default_table_format_options()
+        .column(ColumnConfig::new("disable"))
+        .column(ColumnConfig::new("name"))
+        .column(ColumnConfig::new("type"))
+        .column(ColumnConfig::new("origin"))
+        .column(ColumnConfig::new("comment"));
+
+    format_and_print_result_full(&mut data, &info.returns, &output_format, &options);
+
+    Ok(Value::Null)
+}
+
+pub fn commands() -> CommandLineInterface {
+    let cmd_def = CliCommandMap::new()
+        .insert("list", CliCommand::new(&API_METHOD_LIST_TARGETS))
+        .insert(
+            "test",
+            CliCommand::new(&api2::config::notifications::targets::API_METHOD_TEST_TARGET)
+                .arg_param(&["name"]),
+        );
+
+    cmd_def.into()
+}
-- 
2.39.2





More information about the pbs-devel mailing list