feat(opnsense-config): Public API now complete for dhcp add_static_mapping and remove_static_mapping, not perfect but good enough to move forward

This commit is contained in:
Jean-Gabriel Gill-Couture
2024-11-23 15:07:04 -05:00
parent b14d0ab686
commit d30e909b83
10 changed files with 461 additions and 198 deletions

View File

@@ -1,17 +1,23 @@
use std::sync::Arc;
use crate::{error::Error, modules::dhcp::DhcpConfig};
use log::trace;
use opnsense_config_xml::OPNsense;
use super::ConfigManager;
use super::{ConfigManager, OPNsenseShell};
#[derive(Debug)]
pub struct Config {
opnsense: OPNsense,
repository: Box<dyn ConfigManager + Send + Sync>,
repository: Arc<dyn ConfigManager>,
shell: Arc<dyn OPNsenseShell>,
}
impl Config {
pub async fn new(repository: Box<dyn ConfigManager + Send + Sync>) -> Result<Self, Error> {
pub async fn new(
repository: Arc<dyn ConfigManager>,
shell: Arc<dyn OPNsenseShell>,
) -> Result<Self, Error> {
let xml = repository.load_as_str().await?;
trace!("xml {}", xml);
@@ -20,21 +26,24 @@ impl Config {
Ok(Self {
opnsense,
repository,
shell,
})
}
pub fn dhcp(&mut self) -> DhcpConfig {
DhcpConfig::new(&mut self.opnsense)
DhcpConfig::new(&mut self.opnsense, self.shell.clone())
}
pub async fn apply(&self) -> Result<(), Error> {
self.repository.apply_new_config(&self.opnsense.to_xml()).await
self.repository
.apply_new_config(&self.opnsense.to_xml())
.await
}
}
#[cfg(test)]
mod tests {
use crate::config::LocalFileConfigManager;
use crate::config::{DummyOPNSenseShell, LocalFileConfigManager};
use crate::modules::dhcp::DhcpConfig;
use std::fs;
use std::net::Ipv4Addr;
@@ -55,9 +64,10 @@ mod tests {
let config_file_path = test_file_path.to_str().unwrap().to_string();
println!("File path {config_file_path}");
let repository = Box::new(LocalFileConfigManager::new(config_file_path));
let repository = Arc::new(LocalFileConfigManager::new(config_file_path));
let shell = Arc::new(DummyOPNSenseShell {});
let config_file_str = repository.load_as_str().await.unwrap();
let config = Config::new(repository)
let config = Config::new(repository, shell)
.await
.expect("Failed to load config");
@@ -82,14 +92,15 @@ mod tests {
let config_file_path = test_file_path.to_str().unwrap().to_string();
println!("File path {config_file_path}");
let repository = Box::new(LocalFileConfigManager::new(config_file_path));
let mut config = Config::new(repository)
let repository = Arc::new(LocalFileConfigManager::new(config_file_path));
let shell = Arc::new(DummyOPNSenseShell {});
let mut config = Config::new(repository, shell.clone())
.await
.expect("Failed to load config");
println!("Config {:?}", config);
let mut dhcp_config = DhcpConfig::new(&mut config.opnsense);
let mut dhcp_config = DhcpConfig::new(&mut config.opnsense, shell);
dhcp_config
.add_static_mapping(
"00:00:00:00:00:00",