diff --git a/src/bin/proxmox-backup-client.rs b/src/bin/proxmox-backup-client.rs index 760e59df..2d05f622 100644 --- a/src/bin/proxmox-backup-client.rs +++ b/src/bin/proxmox-backup-client.rs @@ -193,8 +193,12 @@ pub fn complete_repository(_arg: &str, _param: &HashMap) -> Vec< result } -fn connect(server: &str, port: u16, auth_id: &Authid) -> Result { +fn connect(repo: &BackupRepository) -> Result { + connect_do(repo.host(), repo.port(), repo.auth_id()) + .map_err(|err| format_err!("error building client for repository {} - {}", repo, err)) +} +fn connect_do(server: &str, port: u16, auth_id: &Authid) -> Result { let fingerprint = std::env::var(ENV_VAR_PBS_FINGERPRINT).ok(); use std::env::VarError::*; @@ -366,7 +370,7 @@ async fn list_backup_groups(param: Value) -> Result { let repo = extract_repository_from_value(¶m)?; - let client = connect(repo.host(), repo.port(), repo.auth_id())?; + let client = connect(&repo)?; let path = format!("api2/json/admin/datastore/{}/groups", repo.store()); @@ -435,7 +439,7 @@ async fn change_backup_owner(group: String, mut param: Value) -> Result<(), Erro let repo = extract_repository_from_value(¶m)?; - let mut client = connect(repo.host(), repo.port(), repo.auth_id())?; + let mut client = connect(&repo)?; param.as_object_mut().unwrap().remove("repository"); @@ -478,7 +482,7 @@ async fn list_snapshots(param: Value) -> Result { let output_format = get_output_format(¶m); - let client = connect(repo.host(), repo.port(), repo.auth_id())?; + let client = connect(&repo)?; let group: Option = if let Some(path) = param["group"].as_str() { Some(path.parse()?) @@ -543,7 +547,7 @@ async fn forget_snapshots(param: Value) -> Result { let path = tools::required_string_param(¶m, "snapshot")?; let snapshot: BackupDir = path.parse()?; - let mut client = connect(repo.host(), repo.port(), repo.auth_id())?; + let mut client = connect(&repo)?; let path = format!("api2/json/admin/datastore/{}/snapshots", repo.store()); @@ -573,7 +577,7 @@ async fn api_login(param: Value) -> Result { let repo = extract_repository_from_value(¶m)?; - let client = connect(repo.host(), repo.port(), repo.auth_id())?; + let client = connect(&repo)?; client.login().await?; record_repository(&repo); @@ -630,7 +634,7 @@ async fn api_version(param: Value) -> Result<(), Error> { let repo = extract_repository_from_value(¶m); if let Ok(repo) = repo { - let client = connect(repo.host(), repo.port(), repo.auth_id())?; + let client = connect(&repo)?; match client.get("api2/json/version", None).await { Ok(mut result) => version_info["server"] = result["data"].take(), @@ -680,7 +684,7 @@ async fn list_snapshot_files(param: Value) -> Result { let output_format = get_output_format(¶m); - let client = connect(repo.host(), repo.port(), repo.auth_id())?; + let client = connect(&repo)?; let path = format!("api2/json/admin/datastore/{}/files", repo.store()); @@ -724,7 +728,7 @@ async fn start_garbage_collection(param: Value) -> Result { let output_format = get_output_format(¶m); - let mut client = connect(repo.host(), repo.port(), repo.auth_id())?; + let mut client = connect(&repo)?; let path = format!("api2/json/admin/datastore/{}/gc", repo.store()); @@ -1036,7 +1040,7 @@ async fn create_backup( let backup_time = backup_time_opt.unwrap_or_else(|| epoch_i64()); - let client = connect(repo.host(), repo.port(), repo.auth_id())?; + let client = connect(&repo)?; record_repository(&repo); println!("Starting backup: {}/{}/{}", backup_type, backup_id, BackupDir::backup_time_to_string(backup_time)?); @@ -1339,7 +1343,7 @@ async fn restore(param: Value) -> Result { let archive_name = tools::required_string_param(¶m, "archive-name")?; - let client = connect(repo.host(), repo.port(), repo.auth_id())?; + let client = connect(&repo)?; record_repository(&repo); @@ -1512,7 +1516,7 @@ async fn upload_log(param: Value) -> Result { let snapshot = tools::required_string_param(¶m, "snapshot")?; let snapshot: BackupDir = snapshot.parse()?; - let mut client = connect(repo.host(), repo.port(), repo.auth_id())?; + let mut client = connect(&repo)?; let (keydata, crypt_mode) = keyfile_parameters(¶m)?; @@ -1583,7 +1587,7 @@ fn prune<'a>( async fn prune_async(mut param: Value) -> Result { let repo = extract_repository_from_value(¶m)?; - let mut client = connect(repo.host(), repo.port(), repo.auth_id())?; + let mut client = connect(&repo)?; let path = format!("api2/json/admin/datastore/{}/prune", repo.store()); @@ -1669,7 +1673,7 @@ async fn status(param: Value) -> Result { let output_format = get_output_format(¶m); - let client = connect(repo.host(), repo.port(), repo.auth_id())?; + let client = connect(&repo)?; let path = format!("api2/json/admin/datastore/{}/status", repo.store()); diff --git a/src/bin/proxmox_backup_client/benchmark.rs b/src/bin/proxmox_backup_client/benchmark.rs index b434956d..b0d769e8 100644 --- a/src/bin/proxmox_backup_client/benchmark.rs +++ b/src/bin/proxmox_backup_client/benchmark.rs @@ -225,7 +225,7 @@ async fn test_upload_speed( let backup_time = proxmox::tools::time::epoch_i64(); - let client = connect(repo.host(), repo.port(), repo.auth_id())?; + let client = connect(&repo)?; record_repository(&repo); if verbose { eprintln!("Connecting to backup server"); } diff --git a/src/bin/proxmox_backup_client/catalog.rs b/src/bin/proxmox_backup_client/catalog.rs index 87d80f3b..e4931e10 100644 --- a/src/bin/proxmox_backup_client/catalog.rs +++ b/src/bin/proxmox_backup_client/catalog.rs @@ -79,7 +79,7 @@ async fn dump_catalog(param: Value) -> Result { } }; - let client = connect(repo.host(), repo.port(), repo.auth_id())?; + let client = connect(&repo)?; let client = BackupReader::start( client, @@ -153,7 +153,7 @@ async fn dump_catalog(param: Value) -> Result { /// Shell to interactively inspect and restore snapshots. async fn catalog_shell(param: Value) -> Result<(), Error> { let repo = extract_repository_from_value(¶m)?; - let client = connect(repo.host(), repo.port(), repo.auth_id())?; + let client = connect(&repo)?; let path = tools::required_string_param(¶m, "snapshot")?; let archive_name = tools::required_string_param(¶m, "archive-name")?; diff --git a/src/bin/proxmox_backup_client/mount.rs b/src/bin/proxmox_backup_client/mount.rs index 6c91c132..415fbf9d 100644 --- a/src/bin/proxmox_backup_client/mount.rs +++ b/src/bin/proxmox_backup_client/mount.rs @@ -163,7 +163,7 @@ fn mount( async fn mount_do(param: Value, pipe: Option) -> Result { let repo = extract_repository_from_value(¶m)?; let archive_name = tools::required_string_param(¶m, "archive-name")?; - let client = connect(repo.host(), repo.port(), repo.auth_id())?; + let client = connect(&repo)?; let target = param["target"].as_str(); diff --git a/src/bin/proxmox_backup_client/task.rs b/src/bin/proxmox_backup_client/task.rs index 3bf817d8..29b2cc00 100644 --- a/src/bin/proxmox_backup_client/task.rs +++ b/src/bin/proxmox_backup_client/task.rs @@ -48,7 +48,7 @@ async fn task_list(param: Value) -> Result { let output_format = get_output_format(¶m); let repo = extract_repository_from_value(¶m)?; - let client = connect(repo.host(), repo.port(), repo.auth_id())?; + let client = connect(&repo)?; let limit = param["limit"].as_u64().unwrap_or(50) as usize; let running = !param["all"].as_bool().unwrap_or(false); @@ -96,7 +96,7 @@ async fn task_log(param: Value) -> Result { let repo = extract_repository_from_value(¶m)?; let upid = tools::required_string_param(¶m, "upid")?; - let client = connect(repo.host(), repo.port(), repo.auth_id())?; + let client = connect(&repo)?; display_task_log(client, upid, true).await?; @@ -122,7 +122,7 @@ async fn task_stop(param: Value) -> Result { let repo = extract_repository_from_value(¶m)?; let upid_str = tools::required_string_param(¶m, "upid")?; - let mut client = connect(repo.host(), repo.port(), repo.auth_id())?; + let mut client = connect(&repo)?; let path = format!("api2/json/nodes/localhost/tasks/{}", upid_str); let _ = client.delete(&path, None).await?;