Skip to content

Commit

Permalink
return some leases
Browse files Browse the repository at this point in the history
  • Loading branch information
leshow committed Oct 13, 2024
1 parent dd45079 commit 3b9f942
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 7 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions external-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@ config = { path = "../libs/config" }
# libs
anyhow = { workspace = true }
axum = "0.7.5"
chrono = "0.4.38"
hex = "0.4.3"
tokio = { workspace = true }
tracing-futures = { workspace = true }
tracing = { workspace = true }
parking_lot = "0.12"
serde = { workspace = true }
serde_json = { workspace = true }
prometheus = { workspace = true }
ipnet = { workspace = true }


[dev-dependencies]
Expand Down
101 changes: 94 additions & 7 deletions external-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ impl<S: Storage> ExternalApi<S> {
.route("/ping", routing::get(handlers::ping))
.route("/metrics", routing::get(handlers::metrics))
.route("/metrics-text", routing::get(handlers::metrics_text))
.route("/leases", routing::get(handlers::leases::<S>))
.route("/v1/leases", routing::get(handlers::leases::<S>))
.route("/config", routing::get(handlers::config))
.layer(Extension(state))
.layer(Extension(ip_mgr))
Expand Down Expand Up @@ -152,7 +152,7 @@ impl<S: Storage> ExternalApi<S> {

mod handlers {

use std::sync::Arc;
use std::{collections::HashMap, sync::Arc, time::UNIX_EPOCH};

use anyhow::Context;
use axum::{
Expand All @@ -162,9 +162,11 @@ mod handlers {
http::{Response, StatusCode},
response::IntoResponse,
};
use chrono::{DateTime, Utc};
use config::DhcpConfig;
use dora_core::metrics::{START_TIME, UPTIME};
use ip_manager::{IpManager, Storage};
use ipnet::Ipv4Net;
use prometheus::{Encoder, ProtobufEncoder, TextEncoder};
use tracing::error;

Expand All @@ -181,9 +183,67 @@ mod handlers {
Extension(cfg): Extension<Arc<DhcpConfig>>,
Extension(ip_mgr): Extension<Arc<IpManager<S>>>,
) -> ServerResult<impl IntoResponse> {
let leases = ip_mgr.select_all().await;
todo!();
Ok(())
use crate::models::{LeaseInfo, LeaseNetworks, LeaseState, Leases};
use ip_manager::State as S;

let mut cfg = (*cfg).clone();

Check warning on line 189 in external-api/src/lib.rs

View workflow job for this annotation

GitHub Actions / Check (stable)

variable does not need to be mutable

Check failure on line 189 in external-api/src/lib.rs

View workflow job for this annotation

GitHub Actions / Clippy (stable)

variable does not need to be mutable

Check failure on line 189 in external-api/src/lib.rs

View workflow job for this annotation

GitHub Actions / Clippy (stable)

variable does not need to be mutable

Check warning on line 189 in external-api/src/lib.rs

View workflow job for this annotation

GitHub Actions / Check (stable)

variable does not need to be mutable

Check warning on line 189 in external-api/src/lib.rs

View workflow job for this annotation

GitHub Actions / Check (beta)

variable does not need to be mutable

Check warning on line 189 in external-api/src/lib.rs

View workflow job for this annotation

GitHub Actions / Check (beta)

variable does not need to be mutable

Check warning on line 189 in external-api/src/lib.rs

View workflow job for this annotation

GitHub Actions / Test Suite (stable)

variable does not need to be mutable

Check warning on line 189 in external-api/src/lib.rs

View workflow job for this annotation

GitHub Actions / Test Suite (stable)

variable does not need to be mutable
let networks = ip_mgr
.select_all()
.await?
.into_iter()
.map(|lease| {
let info = lease.as_ref();
let ip = info.ip();
let id = info.id().map(hex::encode);
let secs = info.expires_at().duration_since(UNIX_EPOCH)?.as_secs();
let network = info.network();
let expires_at_epoch = secs;
let expires_at_utc = DateTime::<Utc>::from_timestamp(
info.expires_at().duration_since(UNIX_EPOCH)?.as_secs() as i64,
0,
)
.context("failed to create UTC datetime")?
.to_rfc3339();
let lease_info = LeaseInfo {
ip,
id,
network,
expires_at_epoch,
expires_at_utc,
};

let netv4 = match network {
std::net::IpAddr::V4(ip) => ip,
std::net::IpAddr::V6(_) => {
return Err(anyhow::anyhow!("no dynamic ipv6 at this time"))
}
};
if let Some(net) = cfg.v4().network(netv4) {
let lease = match lease {
S::Reserved(_) => LeaseState::Reserved(lease_info),
S::Leased(_) => LeaseState::Leased(lease_info),
S::Probated(_) => LeaseState::Probated(lease_info),
};
Ok((net, lease))
} else {
Err(anyhow::anyhow!(
"failed to find network in cfg for {lease_info:?}"
))
}
})
.collect::<Result<Vec<_>, _>>()?
.into_iter()
.fold(
HashMap::<Ipv4Net, LeaseNetworks>::new(),
|mut map, (net, lease)| {
let entry = map.entry(net.full_subnet()).or_default();
entry.ips.push(lease);

map
},
);

Ok(axum::Json(Leases { networks }))
}

pub(crate) async fn config(
Expand All @@ -195,7 +255,7 @@ mod handlers {
let cfg = tokio::fs::read_to_string(path)
.await
.with_context(|| format!("failed to find config at {}", path.display()))?;
Ok(serde_json::to_string_pretty(&cfg)?)
Ok(axum::Json(cfg))
}

pub(crate) async fn metrics() -> ServerResult<impl IntoResponse> {
Expand Down Expand Up @@ -241,10 +301,12 @@ mod handlers {

/// Various models for API responses
pub mod models {
use std::{collections::HashMap, fmt, net::IpAddr, sync::Arc};

use axum::response::IntoResponse;
use ipnet::Ipv4Net;
use parking_lot::Mutex;
use serde::{Deserialize, Serialize};
use std::{fmt, sync::Arc};

/// The overall health of the system
pub type State = Arc<Mutex<Health>>;
Expand All @@ -271,6 +333,31 @@ pub mod models {
}
}

#[derive(Serialize, Deserialize, Default, Debug, PartialEq, Clone, Eq)]
pub struct Leases {

Check warning on line 337 in external-api/src/lib.rs

View workflow job for this annotation

GitHub Actions / Check (stable)

missing documentation for a struct

Check failure on line 337 in external-api/src/lib.rs

View workflow job for this annotation

GitHub Actions / Clippy (stable)

missing documentation for a struct

Check failure on line 337 in external-api/src/lib.rs

View workflow job for this annotation

GitHub Actions / Clippy (stable)

missing documentation for a struct

Check warning on line 337 in external-api/src/lib.rs

View workflow job for this annotation

GitHub Actions / Check (stable)

missing documentation for a struct

Check warning on line 337 in external-api/src/lib.rs

View workflow job for this annotation

GitHub Actions / Check (beta)

missing documentation for a struct

Check warning on line 337 in external-api/src/lib.rs

View workflow job for this annotation

GitHub Actions / Check (beta)

missing documentation for a struct

Check warning on line 337 in external-api/src/lib.rs

View workflow job for this annotation

GitHub Actions / Test Suite (stable)

missing documentation for a struct

Check warning on line 337 in external-api/src/lib.rs

View workflow job for this annotation

GitHub Actions / Test Suite (stable)

missing documentation for a struct
pub networks: HashMap<Ipv4Net, LeaseNetworks>,

Check warning on line 338 in external-api/src/lib.rs

View workflow job for this annotation

GitHub Actions / Check (stable)

missing documentation for a struct field

Check failure on line 338 in external-api/src/lib.rs

View workflow job for this annotation

GitHub Actions / Clippy (stable)

missing documentation for a struct field

Check failure on line 338 in external-api/src/lib.rs

View workflow job for this annotation

GitHub Actions / Clippy (stable)

missing documentation for a struct field

Check warning on line 338 in external-api/src/lib.rs

View workflow job for this annotation

GitHub Actions / Check (stable)

missing documentation for a struct field

Check warning on line 338 in external-api/src/lib.rs

View workflow job for this annotation

GitHub Actions / Check (beta)

missing documentation for a struct field

Check warning on line 338 in external-api/src/lib.rs

View workflow job for this annotation

GitHub Actions / Check (beta)

missing documentation for a struct field

Check warning on line 338 in external-api/src/lib.rs

View workflow job for this annotation

GitHub Actions / Test Suite (stable)

missing documentation for a struct field

Check warning on line 338 in external-api/src/lib.rs

View workflow job for this annotation

GitHub Actions / Test Suite (stable)

missing documentation for a struct field
}

#[derive(Serialize, Deserialize, Default, Debug, PartialEq, Clone, Eq)]
pub struct LeaseNetworks {

Check warning on line 342 in external-api/src/lib.rs

View workflow job for this annotation

GitHub Actions / Check (stable)

missing documentation for a struct

Check failure on line 342 in external-api/src/lib.rs

View workflow job for this annotation

GitHub Actions / Clippy (stable)

missing documentation for a struct

Check failure on line 342 in external-api/src/lib.rs

View workflow job for this annotation

GitHub Actions / Clippy (stable)

missing documentation for a struct

Check warning on line 342 in external-api/src/lib.rs

View workflow job for this annotation

GitHub Actions / Check (stable)

missing documentation for a struct

Check warning on line 342 in external-api/src/lib.rs

View workflow job for this annotation

GitHub Actions / Check (beta)

missing documentation for a struct

Check warning on line 342 in external-api/src/lib.rs

View workflow job for this annotation

GitHub Actions / Check (beta)

missing documentation for a struct

Check warning on line 342 in external-api/src/lib.rs

View workflow job for this annotation

GitHub Actions / Test Suite (stable)

missing documentation for a struct

Check warning on line 342 in external-api/src/lib.rs

View workflow job for this annotation

GitHub Actions / Test Suite (stable)

missing documentation for a struct
pub ips: Vec<LeaseState>,

Check warning on line 343 in external-api/src/lib.rs

View workflow job for this annotation

GitHub Actions / Check (stable)

missing documentation for a struct field

Check failure on line 343 in external-api/src/lib.rs

View workflow job for this annotation

GitHub Actions / Clippy (stable)

missing documentation for a struct field

Check failure on line 343 in external-api/src/lib.rs

View workflow job for this annotation

GitHub Actions / Clippy (stable)

missing documentation for a struct field

Check warning on line 343 in external-api/src/lib.rs

View workflow job for this annotation

GitHub Actions / Check (stable)

missing documentation for a struct field

Check warning on line 343 in external-api/src/lib.rs

View workflow job for this annotation

GitHub Actions / Check (beta)

missing documentation for a struct field

Check warning on line 343 in external-api/src/lib.rs

View workflow job for this annotation

GitHub Actions / Check (beta)

missing documentation for a struct field

Check warning on line 343 in external-api/src/lib.rs

View workflow job for this annotation

GitHub Actions / Test Suite (stable)

missing documentation for a struct field

Check warning on line 343 in external-api/src/lib.rs

View workflow job for this annotation

GitHub Actions / Test Suite (stable)

missing documentation for a struct field
}

#[derive(Serialize, Deserialize, Debug, PartialEq, Clone, Eq)]
pub enum LeaseState {

Check warning on line 347 in external-api/src/lib.rs

View workflow job for this annotation

GitHub Actions / Check (stable)

missing documentation for an enum

Check failure on line 347 in external-api/src/lib.rs

View workflow job for this annotation

GitHub Actions / Clippy (stable)

missing documentation for an enum

Check failure on line 347 in external-api/src/lib.rs

View workflow job for this annotation

GitHub Actions / Clippy (stable)

missing documentation for an enum

Check warning on line 347 in external-api/src/lib.rs

View workflow job for this annotation

GitHub Actions / Check (stable)

missing documentation for an enum

Check warning on line 347 in external-api/src/lib.rs

View workflow job for this annotation

GitHub Actions / Check (beta)

missing documentation for an enum

Check warning on line 347 in external-api/src/lib.rs

View workflow job for this annotation

GitHub Actions / Check (beta)

missing documentation for an enum

Check warning on line 347 in external-api/src/lib.rs

View workflow job for this annotation

GitHub Actions / Test Suite (stable)

missing documentation for an enum

Check warning on line 347 in external-api/src/lib.rs

View workflow job for this annotation

GitHub Actions / Test Suite (stable)

missing documentation for an enum
Reserved(LeaseInfo),

Check warning on line 348 in external-api/src/lib.rs

View workflow job for this annotation

GitHub Actions / Check (stable)

missing documentation for a variant

Check failure on line 348 in external-api/src/lib.rs

View workflow job for this annotation

GitHub Actions / Clippy (stable)

missing documentation for a variant

Check failure on line 348 in external-api/src/lib.rs

View workflow job for this annotation

GitHub Actions / Clippy (stable)

missing documentation for a variant

Check warning on line 348 in external-api/src/lib.rs

View workflow job for this annotation

GitHub Actions / Check (stable)

missing documentation for a variant

Check warning on line 348 in external-api/src/lib.rs

View workflow job for this annotation

GitHub Actions / Check (beta)

missing documentation for a variant

Check warning on line 348 in external-api/src/lib.rs

View workflow job for this annotation

GitHub Actions / Check (beta)

missing documentation for a variant

Check warning on line 348 in external-api/src/lib.rs

View workflow job for this annotation

GitHub Actions / Test Suite (stable)

missing documentation for a variant

Check warning on line 348 in external-api/src/lib.rs

View workflow job for this annotation

GitHub Actions / Test Suite (stable)

missing documentation for a variant
Leased(LeaseInfo),

Check warning on line 349 in external-api/src/lib.rs

View workflow job for this annotation

GitHub Actions / Check (stable)

missing documentation for a variant

Check failure on line 349 in external-api/src/lib.rs

View workflow job for this annotation

GitHub Actions / Clippy (stable)

missing documentation for a variant

Check failure on line 349 in external-api/src/lib.rs

View workflow job for this annotation

GitHub Actions / Clippy (stable)

missing documentation for a variant

Check warning on line 349 in external-api/src/lib.rs

View workflow job for this annotation

GitHub Actions / Check (stable)

missing documentation for a variant

Check warning on line 349 in external-api/src/lib.rs

View workflow job for this annotation

GitHub Actions / Check (beta)

missing documentation for a variant

Check warning on line 349 in external-api/src/lib.rs

View workflow job for this annotation

GitHub Actions / Check (beta)

missing documentation for a variant

Check warning on line 349 in external-api/src/lib.rs

View workflow job for this annotation

GitHub Actions / Test Suite (stable)

missing documentation for a variant

Check warning on line 349 in external-api/src/lib.rs

View workflow job for this annotation

GitHub Actions / Test Suite (stable)

missing documentation for a variant
Probated(LeaseInfo),

Check warning on line 350 in external-api/src/lib.rs

View workflow job for this annotation

GitHub Actions / Check (stable)

missing documentation for a variant

Check failure on line 350 in external-api/src/lib.rs

View workflow job for this annotation

GitHub Actions / Clippy (stable)

missing documentation for a variant

Check failure on line 350 in external-api/src/lib.rs

View workflow job for this annotation

GitHub Actions / Clippy (stable)

missing documentation for a variant

Check warning on line 350 in external-api/src/lib.rs

View workflow job for this annotation

GitHub Actions / Check (stable)

missing documentation for a variant

Check warning on line 350 in external-api/src/lib.rs

View workflow job for this annotation

GitHub Actions / Check (beta)

missing documentation for a variant

Check warning on line 350 in external-api/src/lib.rs

View workflow job for this annotation

GitHub Actions / Check (beta)

missing documentation for a variant

Check warning on line 350 in external-api/src/lib.rs

View workflow job for this annotation

GitHub Actions / Test Suite (stable)

missing documentation for a variant

Check warning on line 350 in external-api/src/lib.rs

View workflow job for this annotation

GitHub Actions / Test Suite (stable)

missing documentation for a variant
}
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone, Eq)]
pub struct LeaseInfo {

Check warning on line 353 in external-api/src/lib.rs

View workflow job for this annotation

GitHub Actions / Check (stable)

missing documentation for a struct

Check failure on line 353 in external-api/src/lib.rs

View workflow job for this annotation

GitHub Actions / Clippy (stable)

missing documentation for a struct

Check failure on line 353 in external-api/src/lib.rs

View workflow job for this annotation

GitHub Actions / Clippy (stable)

missing documentation for a struct

Check warning on line 353 in external-api/src/lib.rs

View workflow job for this annotation

GitHub Actions / Check (stable)

missing documentation for a struct

Check warning on line 353 in external-api/src/lib.rs

View workflow job for this annotation

GitHub Actions / Check (beta)

missing documentation for a struct

Check warning on line 353 in external-api/src/lib.rs

View workflow job for this annotation

GitHub Actions / Check (beta)

missing documentation for a struct

Check warning on line 353 in external-api/src/lib.rs

View workflow job for this annotation

GitHub Actions / Test Suite (stable)

missing documentation for a struct

Check warning on line 353 in external-api/src/lib.rs

View workflow job for this annotation

GitHub Actions / Test Suite (stable)

missing documentation for a struct
pub ip: IpAddr,
pub id: Option<String>,
pub network: IpAddr,
pub expires_at_epoch: u64,
pub expires_at_utc: String,
}

pub(crate) fn blank_health() -> State {
Arc::new(Mutex::new(Health::Bad))
}
Expand Down
8 changes: 8 additions & 0 deletions libs/config/src/v4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,11 @@ impl Config {
})
}

/// return hashmap of networks
pub fn networks(&self) -> &HashMap<Ipv4Net, Network> {
&self.networks
}

/// find the interface at the index `iface_index`
pub fn find_interface(&self, iface_index: u32) -> Option<&NetworkInterface> {
self.interfaces.iter().find(|e| e.index == iface_index)
Expand Down Expand Up @@ -356,6 +361,9 @@ impl Network {
pub fn subnet(&self) -> Ipv4Addr {
self.subnet.network()
}
pub fn full_subnet(&self) -> Ipv4Net {
self.subnet
}
pub fn authoritative(&self) -> bool {
self.authoritative
}
Expand Down

0 comments on commit 3b9f942

Please sign in to comment.