From 15c847f1364854359a42687f9c25e9b58aa215a7 Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Thu, 18 Jul 2019 09:11:43 +0200 Subject: [PATCH] src/api2/admin/datastore.rs: allow to list all snapshots --- src/api2/admin/datastore.rs | 56 +++++++----------------------- src/bin/proxmox-backup-client.rs | 59 +++++--------------------------- 2 files changed, 21 insertions(+), 94 deletions(-) diff --git a/src/api2/admin/datastore.rs b/src/api2/admin/datastore.rs index 6c6d81af..086c62e4 100644 --- a/src/api2/admin/datastore.rs +++ b/src/api2/admin/datastore.rs @@ -135,20 +135,25 @@ fn list_snapshots ( ) -> 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 group = BackupGroup::new(backup_type, backup_id); + let backup_type = param["backup-type"].as_str(); + let backup_id = param["backup-id"].as_str(); let datastore = DataStore::lookup_datastore(store)?; let base_path = datastore.base_path(); - let backup_list = group.list_backups(&base_path)?; + let backup_list = BackupInfo::list_backups(&base_path)?; let mut snapshots = vec![]; for info in backup_list { + let group = info.backup_dir.group(); + if let Some(backup_type) = backup_type { + if backup_type != group.backup_type() { continue; } + } + if let Some(backup_id) = backup_id { + if backup_id != group.backup_id() { continue; } + } snapshots.push(json!({ "backup-type": group.backup_type(), "backup-id": group.backup_id(), @@ -391,36 +396,6 @@ pub fn api_method_garbage_collection_status() -> ApiMethod { ) } -fn get_backup_list( - param: Value, - _info: &ApiMethod, - _rpcenv: &mut dyn RpcEnvironment, -) -> Result { - - //let config = datastore::config()?; - - let store = param["store"].as_str().unwrap(); - - let datastore = DataStore::lookup_datastore(store)?; - - let mut list = vec![]; - - let backup_list = BackupInfo::list_backups(&datastore.base_path())?; - - for info in backup_list { - list.push(json!({ - "backup-type": info.backup_dir.group().backup_type(), - "backup-id": info.backup_dir.group().backup_id(), - "backup-time": info.backup_dir.backup_time().timestamp(), - "files": info.files, - })); - } - - let result = json!(list); - - Ok(result) -} - fn get_datastore_list( _param: Value, _info: &ApiMethod, @@ -499,13 +474,6 @@ pub fn router() -> Router { ); let datastore_info = Router::new() - .subdir( - "backups", - Router::new() - .get(ApiMethod::new( - get_backup_list, - ObjectSchema::new("List backups.") - .required("store", store_schema.clone())))) .subdir( "download", Router::new() @@ -546,8 +514,8 @@ pub fn router() -> Router { list_snapshots, ObjectSchema::new("List backup groups.") .required("store", store_schema.clone()) - .required("backup-type", StringSchema::new("Backup type.")) - .required("backup-id", StringSchema::new("Backup ID.")) + .optional("backup-type", StringSchema::new("Backup type.")) + .optional("backup-id", StringSchema::new("Backup ID.")) ) ) .delete( diff --git a/src/bin/proxmox-backup-client.rs b/src/bin/proxmox-backup-client.rs index 8cac735a..a94fad83 100644 --- a/src/bin/proxmox-backup-client.rs +++ b/src/bin/proxmox-backup-client.rs @@ -219,48 +219,6 @@ fn strip_server_file_expenstions(list: Vec) -> Vec { result } -/* not used: -fn list_backups( - param: Value, - _info: &ApiMethod, - _rpcenv: &mut dyn RpcEnvironment, -) -> Result { - - let repo = extract_repository_from_value(¶m)?; - - let mut client = HttpClient::new(repo.host(), repo.user())?; - - let path = format!("api2/json/admin/datastore/{}/backups", repo.store()); - - let result = client.get(&path, None)?; - - record_repository(&repo); - - // fixme: implement and use output formatter instead .. - let list = result["data"].as_array().unwrap(); - - for item in list { - - let id = item["backup-id"].as_str().unwrap(); - let btype = item["backup-type"].as_str().unwrap(); - let epoch = item["backup-time"].as_i64().unwrap(); - - let backup_dir = BackupDir::new(btype, id, epoch); - - let files = item["files"].as_array().unwrap().iter().map(|v| v.as_str().unwrap().to_owned()).collect(); - let files = strip_server_file_expenstions(files); - - for filename in files { - let path = backup_dir.relative_path().to_str().unwrap().to_owned(); - println!("{} | {}/{}", backup_dir.backup_time().format("%c"), path, filename); - } - } - - //Ok(result) - Ok(Value::Null) -} - */ - fn list_backup_groups( param: Value, _info: &ApiMethod, @@ -340,19 +298,20 @@ fn list_snapshots( let repo = extract_repository_from_value(¶m)?; - let path = tools::required_string_param(¶m, "group")?; - let group = BackupGroup::parse(path)?; - let output_format = param["output-format"].as_str().unwrap_or("text").to_owned(); let client = HttpClient::new(repo.host(), repo.user())?; let path = format!("api2/json/admin/datastore/{}/snapshots", repo.store()); - let result = client.get(&path, Some(json!({ - "backup-type": group.backup_type(), - "backup-id": group.backup_id(), - }))).wait()?; + let mut args = json!({}); + if let Some(path) = param["group"].as_str() { + let group = BackupGroup::parse(path)?; + args["backup-type"] = group.backup_type().into(); + args["backup-id"] = group.backup_id().into(); + } + + let result = client.get(&path, Some(args)).wait()?; record_repository(&repo); @@ -1291,7 +1250,7 @@ fn main() { ApiMethod::new( list_snapshots, ObjectSchema::new("List backup snapshots.") - .required("group", StringSchema::new("Backup group.")) + .optional("group", StringSchema::new("Backup group.")) .optional("repository", REPO_URL_SCHEMA.clone()) .optional("output-format", OUTPUT_FORMAT.clone()) ))