diff --git a/src/api2/admin/datastore.rs b/src/api2/admin/datastore.rs index a74b1666..54fa8c70 100644 --- a/src/api2/admin/datastore.rs +++ b/src/api2/admin/datastore.rs @@ -280,26 +280,26 @@ fn list_snapshots ( Ok(snapshots) } -#[sortable] -const API_METHOD_STATUS: ApiMethod = ApiMethod::new( - &ApiHandler::Sync(&status), - &ObjectSchema::new( - "Get datastore status.", - &sorted!([ - ("store", false, &DATASTORE_SCHEMA), - ]), - ) -); - +#[api( + input: { + properties: { + store: { + schema: DATASTORE_SCHEMA, + }, + }, + }, + returns: { + type: StorageStatus, + }, +)] +/// Get datastore status. fn status( - param: Value, + store: String, _info: &ApiMethod, _rpcenv: &mut dyn RpcEnvironment, -) -> Result { +) -> Result { - let store = param["store"].as_str().unwrap(); - - let datastore = DataStore::lookup_datastore(store)?; + let datastore = DataStore::lookup_datastore(&store)?; let base_path = datastore.base_path(); @@ -311,11 +311,12 @@ fn status( nix::errno::Errno::result(res)?; let bsize = stat.f_bsize as u64; - Ok(json!({ - "total": stat.f_blocks*bsize, - "used": (stat.f_blocks-stat.f_bfree)*bsize, - "avail": stat.f_bavail*bsize, - })) + + Ok(StorageStatus { + total: stat.f_blocks*bsize, + used: (stat.f_blocks-stat.f_bfree)*bsize, + avail: stat.f_bavail*bsize, + }) } #[macro_export] diff --git a/src/api2/types.rs b/src/api2/types.rs index 35606486..393c32a6 100644 --- a/src/api2/types.rs +++ b/src/api2/types.rs @@ -276,7 +276,17 @@ pub struct BackupContent { pub size: Option, } - +#[api()] +#[derive(Serialize, Deserialize)] +/// Storage space usage information. +pub struct StorageStatus { + /// Total space (bytes). + pub total: u64, + /// Used space (bytes). + pub used: u64, + /// Available space (bytes). + pub avail: u64, +} // Regression tests diff --git a/src/bin/proxmox-backup-client.rs b/src/bin/proxmox-backup-client.rs index e7e8b4de..93112e1a 100644 --- a/src/bin/proxmox-backup-client.rs +++ b/src/bin/proxmox-backup-client.rs @@ -1437,26 +1437,24 @@ async fn status(param: Value) -> Result { let path = format!("api2/json/admin/datastore/{}/status", repo.store()); - let result = client.get(&path, None).await?; - let data = &result["data"]; + let mut result = client.get(&path, None).await?; record_repository(&repo); if output_format == "text" { - let total = data["total"].as_u64().unwrap(); - let used = data["used"].as_u64().unwrap(); - let avail = data["avail"].as_u64().unwrap(); - let roundup = total/200; + let result: StorageStatus = serde_json::from_value(result["data"].take())?; + + let roundup = result.total/200; println!( "total: {} used: {} ({} %) available: {}", - total, - used, - ((used+roundup)*100)/total, - avail, + result.total, + result.used, + ((result.used+roundup)*100)/result.total, + result.avail, ); } else { - format_and_print_result(data, &output_format); + format_and_print_result(&result["data"], &output_format); } Ok(Value::Null)