From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <s.reiter@proxmox.com>
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) server-digest SHA256)
 (No client certificate requested)
 by lists.proxmox.com (Postfix) with ESMTPS id 75A1A74454
 for <pbs-devel@lists.proxmox.com>; Thu,  8 Jul 2021 16:45:37 +0200 (CEST)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
 by firstgate.proxmox.com (Proxmox) with ESMTP id 6260A1A278
 for <pbs-devel@lists.proxmox.com>; Thu,  8 Jul 2021 16:45:37 +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 id D568B1A26D
 for <pbs-devel@lists.proxmox.com>; Thu,  8 Jul 2021 16:45:35 +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 A714740F21
 for <pbs-devel@lists.proxmox.com>; Thu,  8 Jul 2021 16:45:35 +0200 (CEST)
From: Stefan Reiter <s.reiter@proxmox.com>
To: pbs-devel@lists.proxmox.com
Date: Thu,  8 Jul 2021 16:45:27 +0200
Message-Id: <20210708144528.1405534-1-s.reiter@proxmox.com>
X-Mailer: git-send-email 2.30.2
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
X-SPAM-LEVEL: Spam detection results:  0
 AWL 0.634 Adjusted score from AWL reputation of From: address
 BAYES_00                 -1.9 Bayes spam probability is 0 to 1%
 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
 URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See
 http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more
 information. [mod.rs, datastore.rs]
Subject: [pbs-devel] [PATCH proxmox-backup 1/2] api: add support for notes
 on backup groups
X-BeenThere: pbs-devel@lists.proxmox.com
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: Proxmox Backup Server development discussion
 <pbs-devel.lists.proxmox.com>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pbs-devel>, 
 <mailto:pbs-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/pbs-devel/>
List-Post: <mailto:pbs-devel@lists.proxmox.com>
List-Help: <mailto:pbs-devel-request@lists.proxmox.com?subject=help>
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel>, 
 <mailto:pbs-devel-request@lists.proxmox.com?subject=subscribe>
X-List-Received-Date: Thu, 08 Jul 2021 14:45:37 -0000

Stored in atomically-updated 'notes' file in backup group directory.
Available via dedicated GET/PUT API calls, as well as the first line
being included in list_groups (similar to list_snapshots).

Signed-off-by: Stefan Reiter <s.reiter@proxmox.com>
---
 src/api2/admin/datastore.rs | 104 +++++++++++++++++++++++++++++++++++-
 src/api2/types/mod.rs       |   3 ++
 2 files changed, 106 insertions(+), 1 deletion(-)

diff --git a/src/api2/admin/datastore.rs b/src/api2/admin/datastore.rs
index b65c12ad..184def5a 100644
--- a/src/api2/admin/datastore.rs
+++ b/src/api2/admin/datastore.rs
@@ -3,6 +3,7 @@
 use std::collections::HashSet;
 use std::ffi::OsStr;
 use std::os::unix::ffi::OsStrExt;
+use std::path::PathBuf;
 
 use anyhow::{bail, format_err, Error};
 use futures::*;
@@ -17,7 +18,9 @@ use proxmox::api::{
 };
 use proxmox::api::router::{ReturnType, SubdirMap};
 use proxmox::api::schema::*;
-use proxmox::tools::fs::{replace_file, CreateOptions};
+use proxmox::tools::fs::{
+    file_read_firstline, file_read_optional_string, replace_file, CreateOptions,
+};
 use proxmox::{http_err, identity, list_subdirs_api_method, sortable};
 
 use pxar::accessor::aio::Accessor;
@@ -46,6 +49,15 @@ use crate::config::acl::{
     PRIV_DATASTORE_VERIFY,
 };
 
+const GROUP_NOTES_FILE_NAME: &str = "notes";
+
+fn get_group_note_path(store: &DataStore, group: &BackupGroup) -> PathBuf {
+    let mut note_path = store.base_path();
+    note_path.push(group.group_path());
+    note_path.push(GROUP_NOTES_FILE_NAME);
+    note_path
+}
+
 fn check_priv_or_backup_owner(
     store: &DataStore,
     group: &BackupGroup,
@@ -204,6 +216,9 @@ pub fn list_groups(
                 })
                 .to_owned();
 
+            let note_path = get_group_note_path(&datastore, &group);
+            let comment = file_read_firstline(&note_path).ok();
+
             group_info.push(GroupListItem {
                 backup_type: group.backup_type().to_string(),
                 backup_id: group.backup_id().to_string(),
@@ -211,6 +226,7 @@ pub fn list_groups(
                 owner: Some(owner),
                 backup_count,
                 files: last_backup.files,
+                comment,
             });
 
             group_info
@@ -1558,6 +1574,86 @@ pub fn get_rrd_stats(
     )
 }
 
+#[api(
+    input: {
+        properties: {
+            store: {
+                schema: DATASTORE_SCHEMA,
+            },
+            "backup-type": {
+                schema: BACKUP_TYPE_SCHEMA,
+            },
+            "backup-id": {
+                schema: BACKUP_ID_SCHEMA,
+            },
+        },
+    },
+    access: {
+        permission: &Permission::Privilege(&["datastore", "{store}"], PRIV_DATASTORE_AUDIT | PRIV_DATASTORE_BACKUP, true),
+    },
+)]
+/// Get "notes" for a backup group
+pub fn get_group_notes(
+    store: String,
+    backup_type: String,
+    backup_id: String,
+    rpcenv: &mut dyn RpcEnvironment,
+) -> Result<String, Error> {
+    let datastore = DataStore::lookup_datastore(&store)?;
+
+    let auth_id: Authid = rpcenv.get_auth_id().unwrap().parse()?;
+    let backup_group = BackupGroup::new(backup_type, backup_id);
+
+    check_priv_or_backup_owner(&datastore, &backup_group, &auth_id, PRIV_DATASTORE_AUDIT)?;
+
+    let note_path = get_group_note_path(&datastore, &backup_group);
+    Ok(file_read_optional_string(note_path)?.unwrap_or_else(|| "".to_owned()))
+}
+
+#[api(
+    input: {
+        properties: {
+            store: {
+                schema: DATASTORE_SCHEMA,
+            },
+            "backup-type": {
+                schema: BACKUP_TYPE_SCHEMA,
+            },
+            "backup-id": {
+                schema: BACKUP_ID_SCHEMA,
+            },
+            notes: {
+                description: "A multiline text.",
+            },
+        },
+    },
+    access: {
+        permission: &Permission::Privilege(&["datastore", "{store}"],
+                                           PRIV_DATASTORE_MODIFY | PRIV_DATASTORE_BACKUP,
+                                           true),
+    },
+)]
+/// Set "notes" for a backup group
+pub fn set_group_notes(
+    store: String,
+    backup_type: String,
+    backup_id: String,
+    notes: String,
+    rpcenv: &mut dyn RpcEnvironment,
+) -> Result<(), Error> {
+    let datastore = DataStore::lookup_datastore(&store)?;
+
+    let auth_id: Authid = rpcenv.get_auth_id().unwrap().parse()?;
+    let backup_group = BackupGroup::new(backup_type, backup_id);
+
+    check_priv_or_backup_owner(&datastore, &backup_group, &auth_id, PRIV_DATASTORE_MODIFY)?;
+
+    let note_path = get_group_note_path(&datastore, &backup_group);
+    replace_file(note_path, notes.as_bytes(), CreateOptions::new())?;
+
+    Ok(())
+}
+
 #[api(
     input: {
         properties: {
@@ -1782,6 +1878,12 @@ const DATASTORE_INFO_SUBDIRS: SubdirMap = &[
             .get(&API_METHOD_GARBAGE_COLLECTION_STATUS)
             .post(&API_METHOD_START_GARBAGE_COLLECTION)
     ),
+    (
+        "group-notes",
+        &Router::new()
+            .get(&API_METHOD_GET_GROUP_NOTES)
+            .put(&API_METHOD_SET_GROUP_NOTES)
+    ),
     (
         "groups",
         &Router::new()
diff --git a/src/api2/types/mod.rs b/src/api2/types/mod.rs
index 6698f4b7..c9ac56e4 100644
--- a/src/api2/types/mod.rs
+++ b/src/api2/types/mod.rs
@@ -513,6 +513,9 @@ pub struct GroupListItem {
     /// The owner of group
     #[serde(skip_serializing_if="Option::is_none")]
     pub owner: Option<Authid>,
+    /// The first line from group "notes"
+    #[serde(skip_serializing_if="Option::is_none")]
+    pub comment: Option<String>,
 }
 
 #[api()]
-- 
2.30.2