src/api2/types.rs: define and use struct StorageStatus

This commit is contained in:
Dietmar Maurer 2020-01-23 12:42:40 +01:00
parent 09b1f7b202
commit 1dc117bbba
3 changed files with 42 additions and 33 deletions

View File

@ -280,26 +280,26 @@ fn list_snapshots (
Ok(snapshots) Ok(snapshots)
} }
#[sortable] #[api(
const API_METHOD_STATUS: ApiMethod = ApiMethod::new( input: {
&ApiHandler::Sync(&status), properties: {
&ObjectSchema::new( store: {
"Get datastore status.", schema: DATASTORE_SCHEMA,
&sorted!([ },
("store", false, &DATASTORE_SCHEMA), },
]), },
) returns: {
); type: StorageStatus,
},
)]
/// Get datastore status.
fn status( fn status(
param: Value, store: String,
_info: &ApiMethod, _info: &ApiMethod,
_rpcenv: &mut dyn RpcEnvironment, _rpcenv: &mut dyn RpcEnvironment,
) -> Result<Value, Error> { ) -> Result<StorageStatus, Error> {
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(); let base_path = datastore.base_path();
@ -311,11 +311,12 @@ fn status(
nix::errno::Errno::result(res)?; nix::errno::Errno::result(res)?;
let bsize = stat.f_bsize as u64; let bsize = stat.f_bsize as u64;
Ok(json!({
"total": stat.f_blocks*bsize, Ok(StorageStatus {
"used": (stat.f_blocks-stat.f_bfree)*bsize, total: stat.f_blocks*bsize,
"avail": stat.f_bavail*bsize, used: (stat.f_blocks-stat.f_bfree)*bsize,
})) avail: stat.f_bavail*bsize,
})
} }
#[macro_export] #[macro_export]

View File

@ -276,7 +276,17 @@ pub struct BackupContent {
pub size: Option<u64>, pub size: Option<u64>,
} }
#[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 // Regression tests

View File

@ -1437,26 +1437,24 @@ async fn status(param: Value) -> Result<Value, Error> {
let path = format!("api2/json/admin/datastore/{}/status", repo.store()); let path = format!("api2/json/admin/datastore/{}/status", repo.store());
let result = client.get(&path, None).await?; let mut result = client.get(&path, None).await?;
let data = &result["data"];
record_repository(&repo); record_repository(&repo);
if output_format == "text" { if output_format == "text" {
let total = data["total"].as_u64().unwrap(); let result: StorageStatus = serde_json::from_value(result["data"].take())?;
let used = data["used"].as_u64().unwrap();
let avail = data["avail"].as_u64().unwrap(); let roundup = result.total/200;
let roundup = total/200;
println!( println!(
"total: {} used: {} ({} %) available: {}", "total: {} used: {} ({} %) available: {}",
total, result.total,
used, result.used,
((used+roundup)*100)/total, ((result.used+roundup)*100)/result.total,
avail, result.avail,
); );
} else { } else {
format_and_print_result(data, &output_format); format_and_print_result(&result["data"], &output_format);
} }
Ok(Value::Null) Ok(Value::Null)