src/api2/types.rs: define and use struct StorageStatus
This commit is contained in:
		@ -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<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();
 | 
			
		||||
 | 
			
		||||
@ -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]
 | 
			
		||||
 | 
			
		||||
@ -276,7 +276,17 @@ pub struct BackupContent {
 | 
			
		||||
    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
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -1437,26 +1437,24 @@ async fn status(param: Value) -> Result<Value, Error> {
 | 
			
		||||
 | 
			
		||||
    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)
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user