remote scan/completion: add namespace support

Signed-off-by: Fabian Grünbichler <f.gruenbichler@proxmox.com>
Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
This commit is contained in:
Fabian Grünbichler
2022-05-05 15:02:41 +02:00
committed by Thomas Lamprecht
parent 40d495de6d
commit d4037525a8
2 changed files with 164 additions and 13 deletions

View File

@ -1,6 +1,8 @@
use ::serde::{Deserialize, Serialize};
use anyhow::{bail, format_err, Error};
use hex::FromHex;
use pbs_api_types::BackupNamespace;
use pbs_api_types::NamespaceListItem;
use proxmox_router::list_subdirs_api_method;
use proxmox_router::SubdirMap;
use proxmox_sys::sortable;
@ -18,6 +20,7 @@ use pbs_client::{HttpClient, HttpClientOptions};
use pbs_config::sync;
use pbs_config::CachedUserInfo;
use serde_json::json;
#[api(
input: {
@ -393,13 +396,16 @@ pub async fn scan_remote_datastores(name: String) -> Result<Vec<DataStoreListIte
permission: &Permission::Privilege(&["remote", "{name}"], PRIV_REMOTE_AUDIT, false),
},
returns: {
description: "Lists the accessible backup groups in a remote datastore.",
description: "List the accessible namespaces of a remote datastore.",
type: Array,
items: { type: GroupListItem },
items: { type: NamespaceListItem },
},
)]
/// List groups of a remote.cfg entry's datastore
pub async fn scan_remote_groups(name: String, store: String) -> Result<Vec<GroupListItem>, Error> {
/// List namespaces of a datastore of a remote.cfg entry
pub async fn scan_remote_namespaces(
name: String,
store: String,
) -> Result<Vec<NamespaceListItem>, Error> {
let (remote_config, _digest) = pbs_config::remote::config()?;
let remote: Remote = remote_config.lookup("remote", &name)?;
@ -414,7 +420,75 @@ pub async fn scan_remote_groups(name: String, store: String) -> Result<Vec<Group
let client = remote_client(&remote, None).await.map_err(map_remote_err)?;
let api_res = client
.get(&format!("api2/json/admin/datastore/{}/groups", store), None)
.get(
&format!("api2/json/admin/datastore/{}/namespace", store),
None,
)
.await
.map_err(map_remote_err)?;
let parse_res = match api_res.get("data") {
Some(data) => serde_json::from_value::<Vec<NamespaceListItem>>(data.to_owned()),
None => bail!("remote {} did not return any datastore list data", &name),
};
match parse_res {
Ok(parsed) => Ok(parsed),
Err(_) => bail!("Failed to parse remote scan api result."),
}
}
#[api(
input: {
properties: {
name: {
schema: REMOTE_ID_SCHEMA,
},
store: {
schema: DATASTORE_SCHEMA,
},
namespace: {
type: BackupNamespace,
optional: true,
},
},
},
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,
namespace: Option<BackupNamespace>,
) -> 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, None).await.map_err(map_remote_err)?;
let args = if let Some(ns) = namespace {
Some(json!({ "backup-ns": ns }))
} else {
None
};
let api_res = client
.get(&format!("api2/json/admin/datastore/{}/groups", store), args)
.await
.map_err(map_remote_err)?;
let parse_res = match api_res.get("data") {
@ -429,8 +503,13 @@ pub async fn scan_remote_groups(name: String, store: String) -> Result<Vec<Group
}
#[sortable]
const DATASTORE_SCAN_SUBDIRS: SubdirMap =
&[("groups", &Router::new().get(&API_METHOD_SCAN_REMOTE_GROUPS))];
const DATASTORE_SCAN_SUBDIRS: SubdirMap = &[
("groups", &Router::new().get(&API_METHOD_SCAN_REMOTE_GROUPS)),
(
"namespaces",
&Router::new().get(&API_METHOD_SCAN_REMOTE_NAMESPACES),
),
];
const DATASTORE_SCAN_ROUTER: Router = Router::new()
.get(&list_subdirs_api_method!(DATASTORE_SCAN_SUBDIRS))