public inbox for pbs-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Dominik Csapak <d.csapak@proxmox.com>
To: pbs-devel@lists.proxmox.com
Subject: [pbs-devel] [PATCH proxmox-backup 5/5] proxmox-tape: add groups filter to backup command
Date: Thu,  4 Nov 2021 10:56:22 +0100	[thread overview]
Message-ID: <20211104095622.2207427-6-d.csapak@proxmox.com> (raw)
In-Reply-To: <20211104095622.2207427-1-d.csapak@proxmox.com>

and add a completion handler to complete the backup groups

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
---
 src/bin/proxmox-tape.rs | 43 +++++++++++++++++++++++++++++++++++++++--
 1 file changed, 41 insertions(+), 2 deletions(-)

diff --git a/src/bin/proxmox-tape.rs b/src/bin/proxmox-tape.rs
index b7979026..f49857dd 100644
--- a/src/bin/proxmox-tape.rs
+++ b/src/bin/proxmox-tape.rs
@@ -1,4 +1,6 @@
-use anyhow::{format_err, Error};
+use std::collections::HashMap;
+
+use anyhow::{bail, format_err, Error};
 use serde_json::{json, Value};
 
 use proxmox_io::ReadExt;
@@ -22,7 +24,7 @@ use pbs_config::datastore::complete_datastore_name;
 use pbs_api_types::{
     Userid, Authid, DATASTORE_SCHEMA, DATASTORE_MAP_LIST_SCHEMA,
     DRIVE_NAME_SCHEMA, MEDIA_LABEL_SCHEMA, MEDIA_POOL_NAME_SCHEMA,
-    TAPE_RESTORE_SNAPSHOT_SCHEMA,
+    TAPE_RESTORE_SNAPSHOT_SCHEMA, GROUP_FILTER_LIST_SCHEMA, GroupListItem,
 };
 use pbs_tape::{
     PROXMOX_BACKUP_CONTENT_HEADER_MAGIC_1_0, BlockReadError, MediaContentHeader,
@@ -49,6 +51,38 @@ use proxmox_backup::{
 mod proxmox_tape;
 use proxmox_tape::*;
 
+async fn get_backup_groups(store: &str) -> Result<Vec<GroupListItem>, Error> {
+    let client = connect_to_localhost()?;
+    let api_res = client
+        .get(&format!("api2/json/admin/datastore/{}/groups", store), None)
+        .await?;
+
+    match api_res.get("data") {
+        Some(data) => Ok(serde_json::from_value::<Vec<GroupListItem>>(data.to_owned())?),
+        None => bail!("could not get group list"),
+    }
+}
+
+// shell completion helper
+pub fn complete_datastore_group_filter(_arg: &str, param: &HashMap<String, String>) -> Vec<String> {
+
+    let mut list = Vec::new();
+
+    list.push("regex:".to_string());
+    list.push("type:ct".to_string());
+    list.push("type:host".to_string());
+    list.push("type:vm".to_string());
+
+    if let Some(store) =  param.get("store") {
+        let groups = pbs_runtime::block_on(async { get_backup_groups(store).await });
+        if let Ok(groups) = groups {
+            list.extend(groups.iter().map(|group| format!("group:{}/{}", group.backup_type, group.backup_id)));
+        }
+    }
+
+    list
+}
+
 pub fn extract_drive_name(
     param: &mut Value,
     config: &SectionConfigData,
@@ -834,6 +868,10 @@ async fn clean_drive(mut param: Value) -> Result<(), Error> {
                 optional: true,
                 type: Userid,
             },
+            groups: {
+                schema: GROUP_FILTER_LIST_SCHEMA,
+                optional: true,
+            },
             "force-media-set": {
                 description: "Ignore the allocation policy and start a new media-set.",
                 optional: true,
@@ -978,6 +1016,7 @@ fn main() {
                 .completion_cb("drive", complete_drive_name)
                 .completion_cb("store", complete_datastore_name)
                 .completion_cb("pool", complete_pool_name)
+                .completion_cb("groups", complete_datastore_group_filter)
         )
         .insert(
             "restore",
-- 
2.30.2





  parent reply	other threads:[~2021-11-04  9:56 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-11-04  9:56 [pbs-devel] [PATCH proxmox-backup 0/5] add group filters to tape backup (jobs) Dominik Csapak
2021-11-04  9:56 ` [pbs-devel] [PATCH proxmox-backup 1/5] proxmox-tape: add missing 'notify-user' option to backup command Dominik Csapak
2021-11-04  9:56 ` [pbs-devel] [PATCH proxmox-backup 2/5] tape backup jobs: add group filters to config/api Dominik Csapak
2021-11-04  9:56 ` [pbs-devel] [PATCH proxmox-backup 3/5] fix #3533: tape backup: filter groups according to config Dominik Csapak
2021-11-04  9:56 ` [pbs-devel] [PATCH proxmox-backup 4/5] ui: tape: show configred group filters Dominik Csapak
2021-11-04  9:56 ` Dominik Csapak [this message]
2021-11-18 10:13 ` [pbs-devel] applied-series: [PATCH proxmox-backup 0/5] add group filters to tape backup (jobs) Thomas Lamprecht

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20211104095622.2207427-6-d.csapak@proxmox.com \
    --to=d.csapak@proxmox.com \
    --cc=pbs-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal