public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: "Fabian Grünbichler" <f.gruenbichler@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [pve-devel] [PATCH v2 proxmox-backup 07/10] remote: add backup group scanning
Date: Wed, 15 Sep 2021 15:41:54 +0200	[thread overview]
Message-ID: <20210915134157.19762-8-f.gruenbichler@proxmox.com> (raw)
In-Reply-To: <20210915134157.19762-1-f.gruenbichler@proxmox.com>

Signed-off-by: Fabian Grünbichler <f.gruenbichler@proxmox.com>
---

Notes:
    used for bash completion (next patch) and potentially GUI (future work). could also be integrated into the 'remote' CLI/GUI..

 src/api2/config/remote.rs | 73 ++++++++++++++++++++++++++++++++++++---
 1 file changed, 69 insertions(+), 4 deletions(-)

diff --git a/src/api2/config/remote.rs b/src/api2/config/remote.rs
index acf7cfcf..ceca8109 100644
--- a/src/api2/config/remote.rs
+++ b/src/api2/config/remote.rs
@@ -3,13 +3,14 @@ use serde_json::Value;
 use ::serde::{Deserialize, Serialize};
 
 use proxmox::api::{api, ApiMethod, Router, RpcEnvironment, Permission};
-use proxmox::http_err;
+use proxmox::api::router::SubdirMap;
+use proxmox::{http_err, list_subdirs_api_method, sortable};
 
 use pbs_client::{HttpClient, HttpClientOptions};
 use pbs_api_types::{
     REMOTE_ID_SCHEMA, REMOTE_PASSWORD_SCHEMA, Remote, RemoteConfig, RemoteConfigUpdater,
-    Authid, PROXMOX_CONFIG_DIGEST_SCHEMA, DataStoreListItem, SyncJobConfig,
-    PRIV_REMOTE_AUDIT, PRIV_REMOTE_MODIFY,
+    Authid, PROXMOX_CONFIG_DIGEST_SCHEMA, DATASTORE_SCHEMA, GroupListItem,
+    DataStoreListItem, SyncJobConfig, PRIV_REMOTE_AUDIT, PRIV_REMOTE_MODIFY,
 };
 use pbs_config::sync;
 
@@ -340,8 +341,72 @@ pub async fn scan_remote_datastores(name: String) -> Result<Vec<DataStoreListIte
     }
 }
 
+#[api(
+    input: {
+        properties: {
+            name: {
+                schema: REMOTE_ID_SCHEMA,
+            },
+            store: {
+                schema: DATASTORE_SCHEMA,
+            },
+        },
+    },
+    access: {
+        permission: &Permission::Privilege(&["remote", "{name}"], PRIV_REMOTE_AUDIT, false),
+    },
+    returns: {
+        description: "Lists the accessible backup groups in a remote datastore.",
+        type: Array,
+        items: { type: GroupListItem },
+    },
+)]
+/// List groups of a remote.cfg entry's datastore
+pub async fn scan_remote_groups(name: String, store: String) -> Result<Vec<GroupListItem>, Error> {
+    let (remote_config, _digest) = pbs_config::remote::config()?;
+    let remote: Remote = remote_config.lookup("remote", &name)?;
+
+    let map_remote_err = |api_err| {
+        http_err!(INTERNAL_SERVER_ERROR,
+                  "failed to scan remote '{}' - {}",
+                  &name,
+                  api_err)
+    };
+
+    let client = remote_client(remote)
+        .await
+        .map_err(map_remote_err)?;
+    let api_res = client
+        .get(&format!("api2/json/admin/datastore/{}/groups", store), None)
+        .await
+        .map_err(map_remote_err)?;
+    let parse_res = match api_res.get("data") {
+        Some(data) => serde_json::from_value::<Vec<GroupListItem>>(data.to_owned()),
+        None => bail!("remote {} did not return any group list data", &name),
+    };
+
+    match parse_res {
+        Ok(parsed) => Ok(parsed),
+        Err(_) => bail!("Failed to parse remote scan api result."),
+    }
+}
+
+#[sortable]
+const DATASTORE_SCAN_SUBDIRS: SubdirMap = &[
+    (
+        "groups",
+        &Router::new()
+            .get(&API_METHOD_SCAN_REMOTE_GROUPS)
+    ),
+];
+
+const DATASTORE_SCAN_ROUTER: Router = Router::new()
+    .get(&list_subdirs_api_method!(DATASTORE_SCAN_SUBDIRS))
+    .subdirs(DATASTORE_SCAN_SUBDIRS);
+
 const SCAN_ROUTER: Router = Router::new()
-    .get(&API_METHOD_SCAN_REMOTE_DATASTORES);
+    .get(&API_METHOD_SCAN_REMOTE_DATASTORES)
+    .match_all("store", &DATASTORE_SCAN_ROUTER);
 
 const ITEM_ROUTER: Router = Router::new()
     .get(&API_METHOD_READ_REMOTE)
-- 
2.30.2





  parent reply	other threads:[~2021-09-15 13:42 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-15 13:41 [pve-devel] [PATCH v2 proxmox-backup 00/10] pull/sync group filter Fabian Grünbichler
2021-09-15 13:41 ` [pve-devel] [PATCH v2 proxmox-backup 01/10] api-types: add schema for backup group Fabian Grünbichler
2021-09-15 13:41 ` [pve-devel] [PATCH v2 proxmox-backup 02/10] api: add GroupFilter(List) type Fabian Grünbichler
2021-09-16 14:46   ` Dominik Csapak
2021-09-17  6:39     ` Fabian Grünbichler
2021-09-15 13:41 ` [pve-devel] [PATCH v2 proxmox-backup 03/10] BackupGroup: add filter helper Fabian Grünbichler
2021-09-16 14:46   ` Dominik Csapak
2021-09-15 13:41 ` [pve-devel] [PATCH v2 proxmox-backup 04/10] pull: use BackupGroup consistently Fabian Grünbichler
2021-09-15 13:41 ` [pve-devel] [PATCH v2 proxmox-backup 05/10] pull: allow pulling groups selectively Fabian Grünbichler
2021-09-15 13:41 ` [pve-devel] [PATCH v2 proxmox-backup 06/10] sync: add group filtering Fabian Grünbichler
2021-09-16  7:19   ` Thomas Lamprecht
2021-09-16  7:44     ` Fabian Grünbichler
2021-09-17 12:33   ` Dominik Csapak
2021-09-15 13:41 ` Fabian Grünbichler [this message]
2021-09-15 13:41 ` [pve-devel] [PATCH v2 proxmox-backup 08/10] manager: extend sync/pull completion Fabian Grünbichler
2021-09-15 13:41 ` [pve-devel] [PATCH v2 proxmox-backup 09/10] manager: render group filter properly Fabian Grünbichler
2021-09-15 13:41 ` [pve-devel] [PATCH v2 proxmox-backup 10/10] docs: mention group filter in sync docs Fabian Grünbichler

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=20210915134157.19762-8-f.gruenbichler@proxmox.com \
    --to=f.gruenbichler@proxmox.com \
    --cc=pve-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