fix: impl configure_vm

This commit is contained in:
2025-03-04 11:03:15 +00:00
parent 4aa96020a6
commit 8ec143bd6b
17 changed files with 179 additions and 155 deletions

View File

@ -3,8 +3,10 @@ use crate::router::{ArpEntry, Router};
use anyhow::Result;
use base64::engine::general_purpose::STANDARD;
use base64::Engine;
use log::debug;
use reqwest::Method;
use rocket::async_trait;
use serde::{Deserialize, Serialize};
use std::net::IpAddr;
pub struct MikrotikRouter {
@ -26,8 +28,8 @@ impl MikrotikRouter {
#[async_trait]
impl Router for MikrotikRouter {
async fn list_arp_entry(&self) -> Result<Vec<ArpEntry>> {
let rsp: Vec<ArpEntry> = self.api.req(Method::GET, "/rest/ip/arp", ()).await?;
Ok(rsp)
let rsp: Vec<MikrotikArpEntry> = self.api.req(Method::GET, "/rest/ip/arp", ()).await?;
Ok(rsp.into_iter().map(|e| e.into()).collect())
}
async fn add_arp_entry(
@ -36,31 +38,57 @@ impl Router for MikrotikRouter {
mac: &str,
arp_interface: &str,
comment: Option<&str>,
) -> Result<()> {
let _rsp: ArpEntry = self
) -> Result<ArpEntry> {
let rsp: MikrotikArpEntry = self
.api
.req(
Method::PUT,
"/rest/ip/arp",
ArpEntry {
MikrotikArpEntry {
id: None,
address: ip.to_string(),
mac_address: Some(mac.to_string()),
interface: arp_interface.to_string(),
comment: comment.map(|c| c.to_string()),
..Default::default()
},
)
.await?;
Ok(())
debug!("{:?}", rsp);
Ok(rsp.into())
}
async fn remove_arp_entry(&self, id: &str) -> Result<()> {
let _rsp: ArpEntry = self
let rsp: MikrotikArpEntry = self
.api
.req(Method::DELETE, &format!("/rest/ip/arp/{id}"), ())
.await?;
debug!("{:?}", rsp);
Ok(())
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MikrotikArpEntry {
#[serde(rename = ".id")]
#[serde(skip_serializing_if = "Option::is_none")]
pub id: Option<String>,
pub address: String,
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(rename = "mac-address")]
pub mac_address: Option<String>,
pub interface: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub comment: Option<String>,
}
impl Into<ArpEntry> for MikrotikArpEntry {
fn into(self) -> ArpEntry {
ArpEntry {
id: self.id.unwrap(),
address: self.address,
mac_address: self.mac_address.unwrap(),
interface: Some(self.interface),
comment: self.comment,
}
}
}

View File

@ -1,6 +1,5 @@
use anyhow::Result;
use rocket::async_trait;
use rocket::serde::{Deserialize, Serialize};
use std::net::IpAddr;
/// Router defines a network device used to access the hosts
@ -19,21 +18,16 @@ pub trait Router: Send + Sync {
mac: &str,
interface: &str,
comment: Option<&str>,
) -> Result<()>;
) -> Result<ArpEntry>;
async fn remove_arp_entry(&self, id: &str) -> Result<()>;
}
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[derive(Debug, Clone)]
pub struct ArpEntry {
#[serde(rename = ".id")]
#[serde(skip_serializing_if = "Option::is_none")]
pub id: Option<String>,
pub id: String,
pub address: String,
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(rename = "mac-address")]
pub mac_address: Option<String>,
pub interface: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub mac_address: String,
pub interface: Option<String>,
pub comment: Option<String>,
}