client: simplify prune api method
by using the api macro on the async method and reusing the PruneOptions from pbs-datastore with 'flatten: true' Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
This commit is contained in:
parent
dc46aa9a00
commit
e0665a64bd
@ -6,7 +6,6 @@ use std::sync::{Arc, Mutex};
|
|||||||
use std::task::Context;
|
use std::task::Context;
|
||||||
|
|
||||||
use anyhow::{bail, format_err, Error};
|
use anyhow::{bail, format_err, Error};
|
||||||
use futures::future::FutureExt;
|
|
||||||
use futures::stream::{StreamExt, TryStreamExt};
|
use futures::stream::{StreamExt, TryStreamExt};
|
||||||
use serde_json::{json, Value};
|
use serde_json::{json, Value};
|
||||||
use tokio::sync::mpsc;
|
use tokio::sync::mpsc;
|
||||||
@ -21,10 +20,8 @@ use proxmox::{
|
|||||||
},
|
},
|
||||||
api::{
|
api::{
|
||||||
api,
|
api,
|
||||||
ApiHandler,
|
|
||||||
ApiMethod,
|
ApiMethod,
|
||||||
RpcEnvironment,
|
RpcEnvironment,
|
||||||
schema::*,
|
|
||||||
cli::*,
|
cli::*,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
@ -65,6 +62,7 @@ use proxmox_backup::backup::{
|
|||||||
IndexFile,
|
IndexFile,
|
||||||
MANIFEST_BLOB_NAME,
|
MANIFEST_BLOB_NAME,
|
||||||
Shell,
|
Shell,
|
||||||
|
PruneOptions,
|
||||||
};
|
};
|
||||||
|
|
||||||
mod proxmox_backup_client;
|
mod proxmox_backup_client;
|
||||||
@ -1225,60 +1223,65 @@ async fn restore(param: Value) -> Result<Value, Error> {
|
|||||||
Ok(Value::Null)
|
Ok(Value::Null)
|
||||||
}
|
}
|
||||||
|
|
||||||
const API_METHOD_PRUNE: ApiMethod = ApiMethod::new(
|
#[api(
|
||||||
&ApiHandler::Async(&prune),
|
input: {
|
||||||
&ObjectSchema::new(
|
properties: {
|
||||||
"Prune a backup repository.",
|
"dry-run": {
|
||||||
&proxmox_backup::add_common_prune_prameters!([
|
type: bool,
|
||||||
("dry-run", true, &BooleanSchema::new(
|
optional: true,
|
||||||
"Just show what prune would do, but do not delete anything.")
|
description: "Just show what prune would do, but do not delete anything.",
|
||||||
.schema()),
|
},
|
||||||
("group", false, &StringSchema::new("Backup group.").schema()),
|
group: {
|
||||||
], [
|
type: String,
|
||||||
("output-format", true, &OUTPUT_FORMAT),
|
description: "Backup group",
|
||||||
(
|
},
|
||||||
"quiet",
|
"prune-options": {
|
||||||
true,
|
type: PruneOptions,
|
||||||
&BooleanSchema::new("Minimal output - only show removals.")
|
flatten: true,
|
||||||
.schema()
|
},
|
||||||
),
|
"output-format": {
|
||||||
("repository", true, &REPO_URL_SCHEMA),
|
schema: OUTPUT_FORMAT,
|
||||||
])
|
optional: true,
|
||||||
)
|
},
|
||||||
);
|
quiet: {
|
||||||
|
type: bool,
|
||||||
fn prune<'a>(
|
optional: true,
|
||||||
param: Value,
|
default: false,
|
||||||
_info: &ApiMethod,
|
description: "Minimal output - only show removals.",
|
||||||
_rpcenv: &'a mut dyn RpcEnvironment,
|
},
|
||||||
) -> proxmox::api::ApiFuture<'a> {
|
repository: {
|
||||||
async move {
|
schema: REPO_URL_SCHEMA,
|
||||||
prune_async(param).await
|
optional: true,
|
||||||
}.boxed()
|
},
|
||||||
}
|
},
|
||||||
|
},
|
||||||
async fn prune_async(mut param: Value) -> Result<Value, Error> {
|
)]
|
||||||
|
/// Prune a backup repository.
|
||||||
|
async fn prune(
|
||||||
|
dry_run: Option<bool>,
|
||||||
|
group: String,
|
||||||
|
prune_options: PruneOptions,
|
||||||
|
quiet: bool,
|
||||||
|
mut param: Value
|
||||||
|
) -> Result<Value, Error> {
|
||||||
let repo = extract_repository_from_value(¶m)?;
|
let repo = extract_repository_from_value(¶m)?;
|
||||||
|
|
||||||
let mut client = connect(&repo)?;
|
let mut client = connect(&repo)?;
|
||||||
|
|
||||||
let path = format!("api2/json/admin/datastore/{}/prune", repo.store());
|
let path = format!("api2/json/admin/datastore/{}/prune", repo.store());
|
||||||
|
|
||||||
let group = tools::required_string_param(¶m, "group")?;
|
|
||||||
let group: BackupGroup = group.parse()?;
|
let group: BackupGroup = group.parse()?;
|
||||||
|
|
||||||
let output_format = extract_output_format(&mut param);
|
let output_format = extract_output_format(&mut param);
|
||||||
|
|
||||||
let quiet = param["quiet"].as_bool().unwrap_or(false);
|
let mut api_param = serde_json::to_value(prune_options)?;
|
||||||
|
if let Some(dry_run) = dry_run {
|
||||||
|
api_param["dry-run"] = dry_run.into();
|
||||||
|
}
|
||||||
|
api_param["backup-type"] = group.backup_type().into();
|
||||||
|
api_param["backup-id"] = group.backup_id().into();
|
||||||
|
|
||||||
param.as_object_mut().unwrap().remove("repository");
|
let mut result = client.post(&path, Some(api_param)).await?;
|
||||||
param.as_object_mut().unwrap().remove("group");
|
|
||||||
param.as_object_mut().unwrap().remove("quiet");
|
|
||||||
|
|
||||||
param["backup-type"] = group.backup_type().into();
|
|
||||||
param["backup-id"] = group.backup_id().into();
|
|
||||||
|
|
||||||
let mut result = client.post(&path, Some(param)).await?;
|
|
||||||
|
|
||||||
record_repository(&repo);
|
record_repository(&repo);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user