depend on proxmox 0.1.31 - use Value to store result metadata
This commit is contained in:
		@ -2,7 +2,7 @@ use anyhow::{bail, Error};
 | 
			
		||||
use std::sync::{Arc, Mutex};
 | 
			
		||||
use std::collections::HashMap;
 | 
			
		||||
 | 
			
		||||
use serde_json::Value;
 | 
			
		||||
use serde_json::{json, Value};
 | 
			
		||||
 | 
			
		||||
use proxmox::tools::digest_to_hex;
 | 
			
		||||
use proxmox::tools::fs::{replace_file, CreateOptions};
 | 
			
		||||
@ -80,7 +80,7 @@ impl SharedBackupState {
 | 
			
		||||
#[derive(Clone)]
 | 
			
		||||
pub struct BackupEnvironment {
 | 
			
		||||
    env_type: RpcEnvironmentType,
 | 
			
		||||
    result_attributes: HashMap<String, Value>,
 | 
			
		||||
    result_attributes: Value,
 | 
			
		||||
    user: String,
 | 
			
		||||
    pub debug: bool,
 | 
			
		||||
    pub formatter: &'static OutputFormatter,
 | 
			
		||||
@ -110,7 +110,7 @@ impl BackupEnvironment {
 | 
			
		||||
        };
 | 
			
		||||
 | 
			
		||||
        Self {
 | 
			
		||||
            result_attributes: HashMap::new(),
 | 
			
		||||
            result_attributes: json!({}),
 | 
			
		||||
            env_type,
 | 
			
		||||
            user,
 | 
			
		||||
            worker,
 | 
			
		||||
@ -480,12 +480,12 @@ impl BackupEnvironment {
 | 
			
		||||
 | 
			
		||||
impl RpcEnvironment for BackupEnvironment {
 | 
			
		||||
 | 
			
		||||
    fn set_result_attrib(&mut self, name: &str, value: Value) {
 | 
			
		||||
        self.result_attributes.insert(name.into(), value);
 | 
			
		||||
    fn result_attrib_mut(&mut self) -> &mut Value {
 | 
			
		||||
        &mut self.result_attributes
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fn get_result_attrib(&self, name: &str) -> Option<&Value> {
 | 
			
		||||
        self.result_attributes.get(name)
 | 
			
		||||
    fn result_attrib(&self) -> &Value {
 | 
			
		||||
        &self.result_attributes
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fn env_type(&self) -> RpcEnvironmentType {
 | 
			
		||||
 | 
			
		||||
@ -66,7 +66,7 @@ fn check_duplicate_gateway_v6(config: &NetworkConfig, iface: &str) -> Result<(),
 | 
			
		||||
pub fn list_network_devices(
 | 
			
		||||
    _param: Value,
 | 
			
		||||
    _info: &ApiMethod,
 | 
			
		||||
    rpcenv: &mut dyn RpcEnvironment,
 | 
			
		||||
    mut rpcenv: &mut dyn RpcEnvironment,
 | 
			
		||||
) -> Result<Value, Error> {
 | 
			
		||||
 | 
			
		||||
    let (config, digest) = network::config()?;
 | 
			
		||||
@ -84,7 +84,7 @@ pub fn list_network_devices(
 | 
			
		||||
 | 
			
		||||
    let diff = network::changes()?;
 | 
			
		||||
    if !diff.is_empty() {
 | 
			
		||||
        rpcenv.set_result_attrib("changes",  diff.into());
 | 
			
		||||
        rpcenv["changes"] = diff.into();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    Ok(list.into())
 | 
			
		||||
 | 
			
		||||
@ -131,7 +131,7 @@ fn dump_journal(
 | 
			
		||||
fn get_syslog(
 | 
			
		||||
    param: Value,
 | 
			
		||||
    _info: &ApiMethod,
 | 
			
		||||
    rpcenv: &mut dyn RpcEnvironment,
 | 
			
		||||
    mut rpcenv: &mut dyn RpcEnvironment,
 | 
			
		||||
) -> Result<Value, Error> {
 | 
			
		||||
 | 
			
		||||
    let (count, lines) = dump_journal(
 | 
			
		||||
@ -141,7 +141,7 @@ fn get_syslog(
 | 
			
		||||
        param["until"].as_str(),
 | 
			
		||||
        param["service"].as_str())?;
 | 
			
		||||
 | 
			
		||||
    rpcenv.set_result_attrib("total", Value::from(count));
 | 
			
		||||
    rpcenv["total"] = Value::from(count);
 | 
			
		||||
 | 
			
		||||
    Ok(json!(lines))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -156,7 +156,7 @@ fn extract_upid(param: &Value) -> Result<UPID, Error> {
 | 
			
		||||
/// Read task log.
 | 
			
		||||
async fn read_task_log(
 | 
			
		||||
    param: Value,
 | 
			
		||||
    rpcenv: &mut dyn RpcEnvironment,
 | 
			
		||||
    mut rpcenv: &mut dyn RpcEnvironment,
 | 
			
		||||
) -> Result<Value, Error> {
 | 
			
		||||
 | 
			
		||||
    let upid = extract_upid(¶m)?;
 | 
			
		||||
@ -199,11 +199,11 @@ async fn read_task_log(
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    rpcenv.set_result_attrib("total", Value::from(count));
 | 
			
		||||
    rpcenv["total"] = Value::from(count);
 | 
			
		||||
 | 
			
		||||
    if test_status {
 | 
			
		||||
        let active = crate::server::worker_is_active(&upid).await?;
 | 
			
		||||
        rpcenv.set_result_attrib("active", Value::from(active));
 | 
			
		||||
        rpcenv["active"] = Value::from(active);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    Ok(json!(lines))
 | 
			
		||||
@ -304,7 +304,7 @@ pub fn list_tasks(
 | 
			
		||||
    errors: bool,
 | 
			
		||||
    running: bool,
 | 
			
		||||
    param: Value,
 | 
			
		||||
    rpcenv: &mut dyn RpcEnvironment,
 | 
			
		||||
    mut rpcenv: &mut dyn RpcEnvironment,
 | 
			
		||||
) -> Result<Vec<TaskListItem>, Error> {
 | 
			
		||||
 | 
			
		||||
    let username = rpcenv.get_user().unwrap();
 | 
			
		||||
@ -382,7 +382,7 @@ pub fn list_tasks(
 | 
			
		||||
        if (result.len() as u64) < limit { result.push(entry); };
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    rpcenv.set_result_attrib("total", Value::from(count));
 | 
			
		||||
    rpcenv["total"] = Value::from(count);
 | 
			
		||||
 | 
			
		||||
    Ok(result)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -1,8 +1,7 @@
 | 
			
		||||
//use anyhow::{bail, format_err, Error};
 | 
			
		||||
use std::sync::Arc;
 | 
			
		||||
use std::collections::HashMap;
 | 
			
		||||
 | 
			
		||||
use serde_json::Value;
 | 
			
		||||
use serde_json::{json, Value};
 | 
			
		||||
 | 
			
		||||
use proxmox::api::{RpcEnvironment, RpcEnvironmentType};
 | 
			
		||||
 | 
			
		||||
@ -16,7 +15,7 @@ use crate::server::formatter::*;
 | 
			
		||||
#[derive(Clone)]
 | 
			
		||||
pub struct ReaderEnvironment {
 | 
			
		||||
    env_type: RpcEnvironmentType,
 | 
			
		||||
    result_attributes: HashMap<String, Value>,
 | 
			
		||||
    result_attributes: Value,
 | 
			
		||||
    user: String,
 | 
			
		||||
    pub debug: bool,
 | 
			
		||||
    pub formatter: &'static OutputFormatter,
 | 
			
		||||
@ -37,7 +36,7 @@ impl ReaderEnvironment {
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
        Self {
 | 
			
		||||
            result_attributes: HashMap::new(),
 | 
			
		||||
            result_attributes: json!({}),
 | 
			
		||||
            env_type,
 | 
			
		||||
            user,
 | 
			
		||||
            worker,
 | 
			
		||||
@ -61,12 +60,12 @@ impl ReaderEnvironment {
 | 
			
		||||
 | 
			
		||||
impl RpcEnvironment for ReaderEnvironment {
 | 
			
		||||
 | 
			
		||||
    fn set_result_attrib(&mut self, name: &str, value: Value) {
 | 
			
		||||
        self.result_attributes.insert(name.into(), value);
 | 
			
		||||
    fn result_attrib_mut(&mut self) -> &mut Value {
 | 
			
		||||
        &mut self.result_attributes
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fn get_result_attrib(&self, name: &str) -> Option<&Value> {
 | 
			
		||||
        self.result_attributes.get(name)
 | 
			
		||||
    fn result_attrib(&self) -> &Value {
 | 
			
		||||
        &self.result_attributes
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fn env_type(&self) -> RpcEnvironmentType {
 | 
			
		||||
 | 
			
		||||
@ -262,11 +262,9 @@ fn list_network_devices(mut param: Value, rpcenv: &mut dyn RpcEnvironment) -> Re
 | 
			
		||||
        _ => unreachable!(),
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    if let Some(changes) = rpcenv.get_result_attrib("changes") {
 | 
			
		||||
        if let Some(diff) = changes.as_str() {
 | 
			
		||||
            if output_format == "text" {
 | 
			
		||||
                eprintln!("pending changes:\n{}\n", diff);
 | 
			
		||||
            }
 | 
			
		||||
    if let Value::String(ref diff) = rpcenv["changes"] {
 | 
			
		||||
        if output_format == "text" {
 | 
			
		||||
            eprintln!("pending changes:\n{}\n", diff);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@ -339,10 +337,8 @@ fn pending_network_changes(mut param: Value, rpcenv: &mut dyn RpcEnvironment) ->
 | 
			
		||||
        _ => unreachable!(),
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    if let Some(changes) = rpcenv.get_result_attrib("changes") {
 | 
			
		||||
        if let Some(diff) = changes.as_str() {
 | 
			
		||||
            println!("{}", diff);
 | 
			
		||||
        }
 | 
			
		||||
    if let Value::String(ref diff) = rpcenv["changes"] {
 | 
			
		||||
        println!("{}", diff);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    Ok(Value::Null)
 | 
			
		||||
 | 
			
		||||
@ -1,19 +1,18 @@
 | 
			
		||||
use std::collections::HashMap;
 | 
			
		||||
use serde_json::Value;
 | 
			
		||||
use serde_json::{json, Value};
 | 
			
		||||
 | 
			
		||||
use proxmox::api::{RpcEnvironment, RpcEnvironmentType};
 | 
			
		||||
 | 
			
		||||
/// Encapsulates information about the runtime environment
 | 
			
		||||
pub struct RestEnvironment {
 | 
			
		||||
    env_type: RpcEnvironmentType,
 | 
			
		||||
    result_attributes: HashMap<String, Value>,
 | 
			
		||||
    result_attributes: Value,
 | 
			
		||||
    user: Option<String>,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl RestEnvironment {
 | 
			
		||||
    pub fn new(env_type: RpcEnvironmentType) -> Self {
 | 
			
		||||
        Self {
 | 
			
		||||
            result_attributes: HashMap::new(),
 | 
			
		||||
            result_attributes: json!({}),
 | 
			
		||||
            user: None,
 | 
			
		||||
            env_type,
 | 
			
		||||
        }
 | 
			
		||||
@ -22,12 +21,12 @@ impl RestEnvironment {
 | 
			
		||||
 | 
			
		||||
impl RpcEnvironment for RestEnvironment {
 | 
			
		||||
 | 
			
		||||
    fn set_result_attrib(&mut self, name: &str, value: Value) {
 | 
			
		||||
        self.result_attributes.insert(name.into(), value);
 | 
			
		||||
    fn result_attrib_mut (&mut self) -> &mut Value {
 | 
			
		||||
        &mut self.result_attributes
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fn get_result_attrib(&self, name: &str) -> Option<&Value> {
 | 
			
		||||
        self.result_attributes.get(name)
 | 
			
		||||
    fn result_attrib(&self) -> &Value {
 | 
			
		||||
        &self.result_attributes
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fn env_type(&self) -> RpcEnvironmentType {
 | 
			
		||||
 | 
			
		||||
@ -41,16 +41,13 @@ pub fn json_data_response(data: Value) -> Response<Body> {
 | 
			
		||||
 | 
			
		||||
fn add_result_attributes(result: &mut Value, rpcenv: &dyn RpcEnvironment)
 | 
			
		||||
{
 | 
			
		||||
    if let Some(total) = rpcenv.get_result_attrib("total").and_then(|v| v.as_u64()) {
 | 
			
		||||
        result["total"] = Value::from(total);
 | 
			
		||||
    }
 | 
			
		||||
    let attributes = match rpcenv.result_attrib().as_object() {
 | 
			
		||||
        Some(attr) => attr,
 | 
			
		||||
        None => return,
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    if let Some(active) = rpcenv.get_result_attrib("active").and_then(|v| v.as_bool()) {
 | 
			
		||||
        result["active"] = Value::from(active);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if let Some(changes) = rpcenv.get_result_attrib("changes") {
 | 
			
		||||
        result["changes"] = changes.clone();
 | 
			
		||||
    for (key, value) in attributes {
 | 
			
		||||
        result[key] = value.clone();
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user