VirtWeb/virtweb_backend/src/nat/nat_conf_mode.rs

80 lines
2.0 KiB
Rust

use crate::constants;
use crate::libvirt_rest_structures::net::NetworkName;
use crate::nat::nat_definition::NetNat;
use crate::utils::net_utils;
use clap::Parser;
use std::collections::HashMap;
use std::net::IpAddr;
use std::path::{Path, PathBuf};
/// VirtWeb NAT configuration mode. This executable should never be executed manually
#[derive(Parser, Debug, Clone)]
#[clap(author, version, about, long_about = None)]
struct NatArgs {
/// Storage directory
#[clap(short, long)]
storage: String,
/// Network name
#[clap(short, long)]
network_name: String,
/// Operation
#[clap(short, long)]
operation: String,
/// Sub operation
#[clap(long)]
sub_operation: String,
}
impl NatArgs {
pub fn network_file(&self) -> PathBuf {
let network_name = NetworkName(self.network_name.to_string());
Path::new(&self.storage)
.join(constants::STORAGE_NAT_DIR)
.join(network_name.nat_file_name())
}
}
/// NAT sub main function
pub async fn sub_main() -> anyhow::Result<()> {
let args = NatArgs::parse();
if !args.network_file().exists() {
log::warn!("Cannot do anything for the network, because the NAT configuration file does not exixsts!");
return Ok(());
}
let conf_json = std::fs::read_to_string(args.network_file())?;
let conf: NetNat = serde_json::from_str(&conf_json)?;
let ips = net_utils::net_list_and_ips()?;
match (args.operation.as_str(), args.sub_operation.as_str()) {
("started", "begin") => network_started_begin(&conf, &ips).await?,
("stopped", "end") => network_stopped_end(&conf, &ips).await?,
_ => log::debug!(
"Operation {} - {} not supported",
args.operation,
args.sub_operation
),
}
Ok(())
}
pub async fn network_started_begin(
conf: &NetNat,
ips: &HashMap<String, Vec<IpAddr>>,
) -> anyhow::Result<()> {
todo!()
}
pub async fn network_stopped_end(
conf: &NetNat,
ips: &HashMap<String, Vec<IpAddr>>,
) -> anyhow::Result<()> {
todo!()
}