src/section_config.rs: implement generic lookup
This commit is contained in:
		@ -385,7 +385,9 @@ async fn start_datastore_sync(
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    let mut client = connect()?;
 | 
					    let mut client = connect()?;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    let remote = proxmox_backup::config::remotes::lookup(&remote)?;
 | 
					    use proxmox_backup::config::remotes::{self, Remote};
 | 
				
			||||||
 | 
					    let remote_config = remotes::config()?;
 | 
				
			||||||
 | 
					    let remote: Remote = remote_config.lookup("remote", &remote)?;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    let args = json!({
 | 
					    let args = json!({
 | 
				
			||||||
        "store": store,
 | 
					        "store": store,
 | 
				
			||||||
 | 
				
			|||||||
@ -80,27 +80,6 @@ pub fn config() -> Result<SectionConfigData, Error> {
 | 
				
			|||||||
    CONFIG.parse(REMOTES_CFG_FILENAME, &content)
 | 
					    CONFIG.parse(REMOTES_CFG_FILENAME, &content)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
pub fn lookup(remote: &str) -> Result<Remote, Error> {
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    let remotes = config()?;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    let config = match remotes.sections.get(remote) {
 | 
					 | 
				
			||||||
        Some((type_name, config)) => {
 | 
					 | 
				
			||||||
            if type_name != "remote" {
 | 
					 | 
				
			||||||
                bail!("got unexpected type '{}' for remote '{}'", type_name, remote);
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
            config
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
        None => {
 | 
					 | 
				
			||||||
            bail!("no such remote '{}'", remote);
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
    };
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    let remote: Remote = serde_json::from_value(config.clone())?;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    Ok(remote)
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
pub fn save_config(config: &SectionConfigData) -> Result<(), Error> {
 | 
					pub fn save_config(config: &SectionConfigData) -> Result<(), Error> {
 | 
				
			||||||
    let raw = CONFIG.write(REMOTES_CFG_FILENAME, &config)?;
 | 
					    let raw = CONFIG.write(REMOTES_CFG_FILENAME, &config)?;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -5,6 +5,7 @@ use std::collections::HashSet;
 | 
				
			|||||||
use std::collections::VecDeque;
 | 
					use std::collections::VecDeque;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
use serde_json::{json, Value};
 | 
					use serde_json::{json, Value};
 | 
				
			||||||
 | 
					use serde::de::DeserializeOwned;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
use proxmox::api::schema::*;
 | 
					use proxmox::api::schema::*;
 | 
				
			||||||
use proxmox::tools::try_block;
 | 
					use proxmox::tools::try_block;
 | 
				
			||||||
@ -53,6 +54,25 @@ impl SectionConfigData {
 | 
				
			|||||||
        self.sections.insert(section_id.to_string(), (type_name.to_string(), config));
 | 
					        self.sections.insert(section_id.to_string(), (type_name.to_string(), config));
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    pub fn lookup<T: DeserializeOwned>(&self, type_name: &str, remote: &str) -> Result<T, Error> {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        let config = match self.sections.get(remote) {
 | 
				
			||||||
 | 
					            Some((section_type_name, config)) => {
 | 
				
			||||||
 | 
					                if type_name != section_type_name {
 | 
				
			||||||
 | 
					                    bail!("got unexpected type '{}' for {} '{}'", section_type_name, type_name, remote);
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					                config
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            None => {
 | 
				
			||||||
 | 
					                bail!("no such {} '{}'", type_name, remote);
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        let data = T::deserialize(config.clone())?;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        Ok(data)
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    fn record_order(&mut self, section_id: &str) {
 | 
					    fn record_order(&mut self, section_id: &str) {
 | 
				
			||||||
        self.order.push_back(section_id.to_string());
 | 
					        self.order.push_back(section_id.to_string());
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user