Added some endpoints in dhcp-server from FIP namespace, updated readme, added tests

master
Ishan Jain 8 months ago
parent e7e9e8510c
commit 320ba1564f
  1. 2
      README.md
  2. 5
      src/interface/interface.rs
  3. 60
      src/ip/dhcp_server.rs
  4. 4
      src/ip/mod.rs
  5. 82
      src/ip/types.rs
  6. 1
      src/lib.rs
  7. 114
      tests/ip_test.rs

@ -6,6 +6,8 @@ This is a REST API wrapper for [Mikrotik's Router OS](https://help.mikrotik.com/
It'll most likely only have methods/functions that I will need for my own use cases. Please create an issue if a method you need is missing or better yet, please send a PR!
1. `.proplist` will not be supported.
2. `.query` may be supported. For now, filter in your code...
# License

@ -1,8 +1,6 @@
use super::types::*;
use crate::Client;
use crate::{interface::types::Interface, Client};
use color_eyre::Report;
use reqwest::{Method, Request};
use tracing::debug;
pub async fn list(client: &mut Client) -> Result<Vec<Interface>, Report> {
let url = client.base_url.clone();
@ -18,7 +16,6 @@ pub async fn list(client: &mut Client) -> Result<Vec<Interface>, Report> {
pub async fn get(client: &mut Client, ifid: &str) -> Result<Interface, Report> {
let url = client.base_url.clone();
let url = url.join(&format!("{}/{}", super::BASE, ifid))?;
debug!("url {}", url);
let req = Request::new(Method::GET, url);

@ -0,0 +1,60 @@
use crate::{
ip::types::{DhcpServer, Lease, Network},
Client,
};
use color_eyre::Report;
use reqwest::{Method, Request};
pub async fn list(client: &mut Client) -> Result<Vec<DhcpServer>, Report> {
let url = client.base_url.clone();
let url = url.join(&format!("{}/dhcp-server", super::BASE))?;
let req = Request::new(Method::GET, url);
let response = client.execute(req).await?.json::<Vec<DhcpServer>>().await?;
Ok(response)
}
pub async fn get(client: &mut Client, dhcp_server_id: &str) -> Result<DhcpServer, Report> {
let url = client.base_url.clone();
let url = url.join(&format!("{}/dhcp-server/{}", super::BASE, dhcp_server_id))?;
let req = Request::new(Method::GET, url);
let response = client.execute(req).await?.json::<DhcpServer>().await?;
Ok(response)
}
pub async fn list_network(client: &mut Client) -> Result<Vec<Network>, Report> {
let url = client.base_url.clone();
let url = url.join(&format!("{}/dhcp-server/network", super::BASE))?;
let req = Request::new(Method::GET, url);
let response = client.execute(req).await?.json::<Vec<Network>>().await?;
Ok(response)
}
pub async fn get_network(client: &mut Client, nid: &str) -> Result<Network, Report> {
let url = client.base_url.clone();
let url = url.join(&format!("{}/dhcp-server/network/{}", super::BASE, nid))?;
let req = Request::new(Method::GET, url);
let response = client.execute(req).await?.json::<Network>().await?;
Ok(response)
}
pub async fn list_leases(client: &mut Client) -> Result<Vec<Lease>, Report> {
let url = client.base_url.clone();
let url = url.join(&format!("{}/dhcp-server/lease", super::BASE))?;
let req = Request::new(Method::GET, url);
let response = client.execute(req).await?.json::<Vec<Lease>>().await?;
Ok(response)
}
pub async fn get_lease(client: &mut Client, lease_id: &str) -> Result<Lease, Report> {
let url = client.base_url.clone();
let url = url.join(&format!("{}/dhcp-server/lease/{}", super::BASE, lease_id))?;
let req = Request::new(Method::GET, url);
let response = client.execute(req).await?.json::<Lease>().await?;
Ok(response)
}

@ -0,0 +1,4 @@
pub mod dhcp_server;
mod types;
const BASE: &str = "rest/ip";

@ -0,0 +1,82 @@
use serde_derive::Deserialize;
use serde_derive::Serialize;
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DhcpServer {
#[serde(rename = ".id")]
pub id: String,
#[serde(rename = "address-pool")]
pub address_pool: String,
pub authoritative: String,
pub disabled: String,
pub dynamic: String,
pub interface: String,
pub invalid: String,
#[serde(rename = "lease-script")]
pub lease_script: String,
#[serde(rename = "lease-time")]
pub lease_time: String,
pub name: String,
#[serde(rename = "use-radius")]
pub use_radius: String,
}
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Lease {
#[serde(rename = ".id")]
pub id: String,
pub address: String,
#[serde(rename = "address-lists")]
pub address_lists: String,
pub blocked: String,
#[serde(rename = "client-id")]
pub client_id: Option<String>,
#[serde(rename = "dhcp-option")]
pub dhcp_option: String,
pub disabled: String,
pub dynamic: String,
#[serde(rename = "host-name")]
pub host_name: String,
#[serde(rename = "last-seen")]
pub last_seen: String,
#[serde(rename = "mac-address")]
pub mac_address: String,
pub radius: String,
pub server: String,
pub status: String,
#[serde(rename = "active-address")]
pub active_address: Option<String>,
#[serde(rename = "active-client-id")]
pub active_client_id: Option<String>,
#[serde(rename = "active-mac-address")]
pub active_mac_address: Option<String>,
#[serde(rename = "active-server")]
pub active_server: Option<String>,
#[serde(rename = "expires-after")]
pub expires_after: Option<String>,
}
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Network {
#[serde(rename = ".id")]
pub id: String,
pub address: String,
#[serde(rename = "caps-manager")]
pub caps_manager: String,
pub comment: String,
#[serde(rename = "dhcp-option")]
pub dhcp_option: String,
#[serde(rename = "dns-server")]
pub dns_server: String,
pub domain: Option<String>,
pub dynamic: String,
pub gateway: String,
pub netmask: String,
#[serde(rename = "ntp-server")]
pub ntp_server: String,
#[serde(rename = "wins-server")]
pub wins_server: String,
}

@ -1,5 +1,6 @@
mod client;
pub mod interface;
pub mod ip;
pub mod system;
pub use client::*;

@ -0,0 +1,114 @@
use color_eyre::Report;
use mikrotik::Client;
use reqwest::Url;
use std::sync::Once;
use tracing_subscriber::EnvFilter;
static INIT: Once = Once::new();
fn setup() -> Result<(), Report> {
INIT.call_once(|| {
if std::env::var("RUST_LIB_BACKTRACE").is_err() {
std::env::set_var("RUST_LIB_BACKTRACE", "1")
}
color_eyre::install();
if std::env::var("RUST_LOG").is_err() {
std::env::set_var("RUST_LOG", "info");
}
tracing_subscriber::fmt::fmt()
.with_env_filter(EnvFilter::from_default_env())
.init();
});
Ok(())
}
#[tokio::test]
async fn list_dhcp_servers() -> Result<(), Report> {
setup()?;
let base = Url::parse("https://10.0.10.1")?;
let mut client = Client::new(base, "admin".to_string(), "ifd783far".to_string(), true)?;
let response = mikrotik::ip::dhcp_server::list(&mut client).await?;
println!("{:?}", response);
Ok(())
}
#[tokio::test]
async fn get_dhcp_server() -> Result<(), Report> {
setup()?;
let base = Url::parse("https://10.0.10.1")?;
let mut client = Client::new(base, "admin".to_string(), "ifd783far".to_string(), true)?;
let response = mikrotik::ip::dhcp_server::get(&mut client, "vlan-150").await?;
println!("{:?}", response);
Ok(())
}
#[tokio::test]
async fn list_network() -> Result<(), Report> {
setup()?;
let base = Url::parse("https://10.0.10.1")?;
let mut client = Client::new(base, "admin".to_string(), "ifd783far".to_string(), true)?;
let response = mikrotik::ip::dhcp_server::list_network(&mut client).await?;
println!("{:?}", response);
Ok(())
}
#[tokio::test]
async fn get_network() -> Result<(), Report> {
setup()?;
let base = Url::parse("https://10.0.10.1")?;
let mut client = Client::new(base, "admin".to_string(), "ifd783far".to_string(), true)?;
let response = mikrotik::ip::dhcp_server::list_network(&mut client).await?;
let response = mikrotik::ip::dhcp_server::get_network(&mut client, &response[0].id).await?;
println!("{:?}", response);
Ok(())
}
#[tokio::test]
async fn list_leases() -> Result<(), Report> {
setup()?;
let base = Url::parse("https://10.0.10.1")?;
let mut client = Client::new(base, "admin".to_string(), "ifd783far".to_string(), true)?;
let response = mikrotik::ip::dhcp_server::list_leases(&mut client).await?;
println!("{:?}", response);
Ok(())
}
#[tokio::test]
async fn get_lease() -> Result<(), Report> {
setup()?;
let base = Url::parse("https://10.0.10.1")?;
let mut client = Client::new(base, "admin".to_string(), "ifd783far".to_string(), true)?;
let response = mikrotik::ip::dhcp_server::list_leases(&mut client).await?;
let response = mikrotik::ip::dhcp_server::get_lease(&mut client, &response[0].id).await?;
println!("{:?}", response);
Ok(())
}
Loading…
Cancel
Save