From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id 9261576887 for ; Fri, 16 Jul 2021 10:54:04 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id A9772E2A2 for ; Fri, 16 Jul 2021 10:53:33 +0200 (CEST) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [94.136.29.106]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS id 92BDBE1A3 for ; Fri, 16 Jul 2021 10:53:29 +0200 (CEST) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 6767442152 for ; Fri, 16 Jul 2021 10:53:29 +0200 (CEST) From: Dominik Csapak To: pbs-devel@lists.proxmox.com Date: Fri, 16 Jul 2021 10:53:21 +0200 Message-Id: <20210716085328.3731574-5-d.csapak@proxmox.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210716085328.3731574-1-d.csapak@proxmox.com> References: <20210716085328.3731574-1-d.csapak@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.581 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Subject: [pbs-devel] [PATCH proxmox-backup 04/11] api: admin/datastore: simplify prune api call X-BeenThere: pbs-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Backup Server development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 16 Jul 2021 08:54:04 -0000 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 --- src/api2/admin/datastore.rs | 135 ++++++++++++------------------------ 1 file changed, 45 insertions(+), 90 deletions(-) diff --git a/src/api2/admin/datastore.rs b/src/api2/admin/datastore.rs index 184def5a..0bf6a86b 100644 --- a/src/api2/admin/datastore.rs +++ b/src/api2/admin/datastore.rs @@ -16,7 +16,7 @@ use proxmox::api::{ api, ApiResponseFuture, ApiHandler, ApiMethod, Router, RpcEnvironment, RpcEnvironmentType, Permission }; -use proxmox::api::router::{ReturnType, SubdirMap}; +use proxmox::api::router::SubdirMap; use proxmox::api::schema::*; use proxmox::tools::fs::{ file_read_firstline, file_read_optional_string, replace_file, CreateOptions, @@ -797,106 +797,61 @@ pub fn verify( Ok(json!(upid_str)) } -#[macro_export] -macro_rules! add_common_prune_prameters { - ( [ $( $list1:tt )* ] ) => { - add_common_prune_prameters!([$( $list1 )* ] , []) - }; - ( [ $( $list1:tt )* ] , [ $( $list2:tt )* ] ) => { - [ - $( $list1 )* - ( - "keep-daily", - true, - &PRUNE_SCHEMA_KEEP_DAILY, - ), - ( - "keep-hourly", - true, - &PRUNE_SCHEMA_KEEP_HOURLY, - ), - ( - "keep-last", - true, - &PRUNE_SCHEMA_KEEP_LAST, - ), - ( - "keep-monthly", - true, - &PRUNE_SCHEMA_KEEP_MONTHLY, - ), - ( - "keep-weekly", - true, - &PRUNE_SCHEMA_KEEP_WEEKLY, - ), - ( - "keep-yearly", - true, - &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) -); - +#[api( + input: { + properties: { + "backup-id": { + schema: BACKUP_ID_SCHEMA, + }, + "backup-type": { + schema: BACKUP_TYPE_SCHEMA, + }, + "dry-run": { + optional: true, + type: bool, + default: false, + description: "Just show what prune would do, but do not delete anything.", + }, + "prune-options": { + type: PruneOptions, + flatten: true, + }, + store: { + schema: DATASTORE_SCHEMA, + }, + }, + }, + returns: { + type: Array, + description: "Returns the list of snapshots and a flag indicating if there are kept or removed.", + items: { + type: PruneListItem, + }, + }, + access: { + permission: &Permission::Privilege(&["datastore", "{store}"], PRIV_DATASTORE_MODIFY | PRIV_DATASTORE_PRUNE, true), + }, +)] +/// Prune the datastore pub fn prune( - param: Value, - _info: &ApiMethod, + backup_id: String, + backup_type: String, + dry_run: bool, + prune_options: PruneOptions, + store: String, + _param: Value, rpcenv: &mut dyn RpcEnvironment, ) -> Result { - let store = tools::required_string_param(¶m, "store")?; - let backup_type = tools::required_string_param(¶m, "backup-type")?; - let backup_id = tools::required_string_param(¶m, "backup-id")?; - 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)?; check_priv_or_backup_owner(&datastore, &group, &auth_id, PRIV_DATASTORE_MODIFY)?; - let prune_options = PruneOptions { - 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 worker_id = format!("{}:{}/{}", store, &backup_type, &backup_id); let mut prune_result = Vec::new(); -- 2.30.2