api: admin/datastore: simplify prune api call

by using the api macro and reusing the PruneOptions from pbs-datastore

this means we can now drop the 'add_common_prune_prameters' macro

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
This commit is contained in:
Dominik Csapak 2021-07-16 10:53:21 +02:00 committed by Dietmar Maurer
parent e0665a64bd
commit 0a240aaa9a

View File

@ -16,7 +16,7 @@ use proxmox::api::{
api, ApiResponseFuture, ApiHandler, ApiMethod, Router, api, ApiResponseFuture, ApiHandler, ApiMethod, Router,
RpcEnvironment, RpcEnvironmentType, Permission RpcEnvironment, RpcEnvironmentType, Permission
}; };
use proxmox::api::router::{ReturnType, SubdirMap}; use proxmox::api::router::SubdirMap;
use proxmox::api::schema::*; use proxmox::api::schema::*;
use proxmox::tools::fs::{ use proxmox::tools::fs::{
file_read_firstline, file_read_optional_string, replace_file, CreateOptions, file_read_firstline, file_read_optional_string, replace_file, CreateOptions,
@ -797,106 +797,61 @@ pub fn verify(
Ok(json!(upid_str)) Ok(json!(upid_str))
} }
#[macro_export] #[api(
macro_rules! add_common_prune_prameters { input: {
( [ $( $list1:tt )* ] ) => { properties: {
add_common_prune_prameters!([$( $list1 )* ] , []) "backup-id": {
}; schema: BACKUP_ID_SCHEMA,
( [ $( $list1:tt )* ] , [ $( $list2:tt )* ] ) => { },
[ "backup-type": {
$( $list1 )* schema: BACKUP_TYPE_SCHEMA,
( },
"keep-daily", "dry-run": {
true, optional: true,
&PRUNE_SCHEMA_KEEP_DAILY, type: bool,
), default: false,
( description: "Just show what prune would do, but do not delete anything.",
"keep-hourly", },
true, "prune-options": {
&PRUNE_SCHEMA_KEEP_HOURLY, type: PruneOptions,
), flatten: true,
( },
"keep-last", store: {
true, schema: DATASTORE_SCHEMA,
&PRUNE_SCHEMA_KEEP_LAST, },
), },
( },
"keep-monthly", returns: {
true, type: Array,
&PRUNE_SCHEMA_KEEP_MONTHLY, description: "Returns the list of snapshots and a flag indicating if there are kept or removed.",
), items: {
( type: PruneListItem,
"keep-weekly", },
true, },
&PRUNE_SCHEMA_KEEP_WEEKLY, access: {
), permission: &Permission::Privilege(&["datastore", "{store}"], PRIV_DATASTORE_MODIFY | PRIV_DATASTORE_PRUNE, true),
( },
"keep-yearly", )]
true, /// Prune the datastore
&PRUNE_SCHEMA_KEEP_YEARLY,
),
$( $list2 )*
]
}
}
pub const API_RETURN_SCHEMA_PRUNE: Schema = ArraySchema::new(
"Returns the list of snapshots and a flag indicating if there are kept or removed.",
&PruneListItem::API_SCHEMA
).schema();
pub const API_METHOD_PRUNE: ApiMethod = ApiMethod::new(
&ApiHandler::Sync(&prune),
&ObjectSchema::new(
"Prune the datastore.",
&add_common_prune_prameters!([
("backup-id", false, &BACKUP_ID_SCHEMA),
("backup-type", false, &BACKUP_TYPE_SCHEMA),
("dry-run", true, &BooleanSchema::new(
"Just show what prune would do, but do not delete anything.")
.schema()
),
],[
("store", false, &DATASTORE_SCHEMA),
])
))
.returns(ReturnType::new(false, &API_RETURN_SCHEMA_PRUNE))
.access(None, &Permission::Privilege(
&["datastore", "{store}"],
PRIV_DATASTORE_MODIFY | PRIV_DATASTORE_PRUNE,
true)
);
pub fn prune( pub fn prune(
param: Value, backup_id: String,
_info: &ApiMethod, backup_type: String,
dry_run: bool,
prune_options: PruneOptions,
store: String,
_param: Value,
rpcenv: &mut dyn RpcEnvironment, rpcenv: &mut dyn RpcEnvironment,
) -> Result<Value, Error> { ) -> Result<Value, Error> {
let store = tools::required_string_param(&param, "store")?;
let backup_type = tools::required_string_param(&param, "backup-type")?;
let backup_id = tools::required_string_param(&param, "backup-id")?;
let auth_id: Authid = rpcenv.get_auth_id().unwrap().parse()?; let auth_id: Authid = rpcenv.get_auth_id().unwrap().parse()?;
let dry_run = param["dry-run"].as_bool().unwrap_or(false); let group = BackupGroup::new(&backup_type, &backup_id);
let group = BackupGroup::new(backup_type, backup_id);
let datastore = DataStore::lookup_datastore(&store)?; let datastore = DataStore::lookup_datastore(&store)?;
check_priv_or_backup_owner(&datastore, &group, &auth_id, PRIV_DATASTORE_MODIFY)?; check_priv_or_backup_owner(&datastore, &group, &auth_id, PRIV_DATASTORE_MODIFY)?;
let prune_options = PruneOptions { let worker_id = format!("{}:{}/{}", store, &backup_type, &backup_id);
keep_last: param["keep-last"].as_u64(),
keep_hourly: param["keep-hourly"].as_u64(),
keep_daily: param["keep-daily"].as_u64(),
keep_weekly: param["keep-weekly"].as_u64(),
keep_monthly: param["keep-monthly"].as_u64(),
keep_yearly: param["keep-yearly"].as_u64(),
};
let worker_id = format!("{}:{}/{}", store, backup_type, backup_id);
let mut prune_result = Vec::new(); let mut prune_result = Vec::new();