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

Lukas Wagner l.wagner at proxmox.com
Mon Apr 22 14:38:34 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>
---
 .../notifications/matchers.rs                 | 93 +++++++++++++++++++
 .../notifications/mod.rs                      |  5 +-
 2 files changed, 97 insertions(+), 1 deletion(-)
 create mode 100644 src/bin/proxmox_backup_manager/notifications/matchers.rs

diff --git a/src/bin/proxmox_backup_manager/notifications/matchers.rs b/src/bin/proxmox_backup_manager/notifications/matchers.rs
new file mode 100644
index 00000000..cfa3aaa4
--- /dev/null
+++ b/src/bin/proxmox_backup_manager/notifications/matchers.rs
@@ -0,0 +1,93 @@
+use anyhow::Error;
+use proxmox_notify::schema::ENTITY_NAME_SCHEMA;
+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 notification matchers.
+fn list_matchers(param: Value, rpcenv: &mut dyn RpcEnvironment) -> Result<Value, Error> {
+    let output_format = get_output_format(&param);
+
+    let info = &api2::config::notifications::matchers::API_METHOD_LIST_MATCHERS;
+    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("origin"))
+        .column(ColumnConfig::new("comment"));
+
+    format_and_print_result_full(&mut data, &info.returns, &output_format, &options);
+
+    Ok(Value::Null)
+}
+
+#[api(
+    input: {
+        properties: {
+            name: {
+                schema: ENTITY_NAME_SCHEMA,
+            },
+            "output-format": {
+                schema: OUTPUT_FORMAT,
+                optional: true,
+            },
+        }
+    }
+)]
+/// Show a single matcher.
+fn show_matcher(param: Value, rpcenv: &mut dyn RpcEnvironment) -> Result<Value, Error> {
+    let output_format = get_output_format(&param);
+
+    let info = &api2::config::notifications::matchers::API_METHOD_GET_MATCHER;
+    let mut data = match info.handler {
+        ApiHandler::Sync(handler) => (handler)(param, info, rpcenv)?,
+        _ => unreachable!(),
+    };
+
+    let options = default_table_format_options();
+    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_MATCHERS))
+        .insert(
+            "show",
+            CliCommand::new(&API_METHOD_SHOW_MATCHER).arg_param(&["name"]),
+        )
+        .insert(
+            "create",
+            CliCommand::new(&api2::config::notifications::matchers::API_METHOD_ADD_MATCHER)
+                .arg_param(&["name"]),
+        )
+        .insert(
+            "update",
+            CliCommand::new(&api2::config::notifications::matchers::API_METHOD_UPDATE_MATCHER)
+                .arg_param(&["name"]),
+        )
+        .insert(
+            "delete",
+            CliCommand::new(&api2::config::notifications::matchers::API_METHOD_DELETE_MATCHER)
+                .arg_param(&["name"]),
+        );
+    cmd_def.into()
+}
diff --git a/src/bin/proxmox_backup_manager/notifications/mod.rs b/src/bin/proxmox_backup_manager/notifications/mod.rs
index fdd5abe3..d83563f5 100644
--- a/src/bin/proxmox_backup_manager/notifications/mod.rs
+++ b/src/bin/proxmox_backup_manager/notifications/mod.rs
@@ -1,9 +1,12 @@
 use proxmox_router::cli::{CliCommandMap, CommandLineInterface};
 
+mod matchers;
 mod targets;
 
 pub fn notification_commands() -> CommandLineInterface {
-    let cmd_def = CliCommandMap::new().insert("target", targets::commands());
+    let cmd_def = CliCommandMap::new()
+        .insert("target", targets::commands())
+        .insert("matcher", matchers::commands());
 
     cmd_def.into()
 }
-- 
2.39.2





More information about the pbs-devel mailing list