src/section_config.rs: implement generic lookup

This commit is contained in:
Dietmar Maurer 2020-01-09 17:35:44 +01:00
parent 0eb0e02451
commit a81af92f9d
3 changed files with 23 additions and 22 deletions

View File

@ -385,7 +385,9 @@ async fn start_datastore_sync(
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!({
"store": store,

View File

@ -80,27 +80,6 @@ pub fn config() -> Result<SectionConfigData, Error> {
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> {
let raw = CONFIG.write(REMOTES_CFG_FILENAME, &config)?;

View File

@ -5,6 +5,7 @@ use std::collections::HashSet;
use std::collections::VecDeque;
use serde_json::{json, Value};
use serde::de::DeserializeOwned;
use proxmox::api::schema::*;
use proxmox::tools::try_block;
@ -53,6 +54,25 @@ impl SectionConfigData {
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) {
self.order.push_back(section_id.to_string());
}