feat: Can now save configuration, refactored repository into manager as it also executes commands to reload services and calling it a repository was found misleading by @stremblay"

This commit is contained in:
Jean-Gabriel Gill-Couture
2024-11-22 14:15:23 -05:00
parent 9a37aa1321
commit b14d0ab686
10 changed files with 334 additions and 173 deletions

View File

@@ -2,17 +2,17 @@ use crate::{error::Error, modules::dhcp::DhcpConfig};
use log::trace;
use opnsense_config_xml::OPNsense;
use super::ConfigRepository;
use super::ConfigManager;
#[derive(Debug)]
pub struct Config {
opnsense: OPNsense,
repository: Box<dyn ConfigRepository + Send + Sync>,
repository: Box<dyn ConfigManager + Send + Sync>,
}
impl Config {
pub async fn new(repository: Box<dyn ConfigRepository + Send + Sync>) -> Result<Self, Error> {
let xml = repository.load().await?;
pub async fn new(repository: Box<dyn ConfigManager + Send + Sync>) -> Result<Self, Error> {
let xml = repository.load_as_str().await?;
trace!("xml {}", xml);
let opnsense = OPNsense::from(xml);
@@ -27,14 +27,14 @@ impl Config {
DhcpConfig::new(&mut self.opnsense)
}
pub async fn save(&self) -> Result<(), Error> {
self.repository.save(&self.opnsense.to_xml()).await
pub async fn apply(&self) -> Result<(), Error> {
self.repository.apply_new_config(&self.opnsense.to_xml()).await
}
}
#[cfg(test)]
mod tests {
use crate::config::LocalFileConfigRepository;
use crate::config::LocalFileConfigManager;
use crate::modules::dhcp::DhcpConfig;
use std::fs;
use std::net::Ipv4Addr;
@@ -55,8 +55,8 @@ mod tests {
let config_file_path = test_file_path.to_str().unwrap().to_string();
println!("File path {config_file_path}");
let repository = Box::new(LocalFileConfigRepository::new(config_file_path));
let config_file_str = repository.load().await.unwrap();
let repository = Box::new(LocalFileConfigManager::new(config_file_path));
let config_file_str = repository.load_as_str().await.unwrap();
let config = Config::new(repository)
.await
.expect("Failed to load config");
@@ -82,7 +82,7 @@ mod tests {
let config_file_path = test_file_path.to_str().unwrap().to_string();
println!("File path {config_file_path}");
let repository = Box::new(LocalFileConfigRepository::new(config_file_path));
let repository = Box::new(LocalFileConfigManager::new(config_file_path));
let mut config = Config::new(repository)
.await
.expect("Failed to load config");
@@ -107,8 +107,8 @@ mod tests {
let config_file_path = test_file_path.to_str().unwrap().to_string();
println!("File path {config_file_path}");
let repository = Box::new(LocalFileConfigRepository::new(config_file_path));
let expected_config_file_str = repository.load().await.unwrap();
let repository = Box::new(LocalFileConfigManager::new(config_file_path));
let expected_config_file_str = repository.load_as_str().await.unwrap();
assert_eq!(expected_config_file_str, serialized);
}
}