Skip to content

Commit

Permalink
Some network serialize updates
Browse files Browse the repository at this point in the history
  • Loading branch information
teclator committed Mar 1, 2024
1 parent 6035ef7 commit 349f6ee
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
27 changes: 26 additions & 1 deletion rust/agama-server/src/network/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::network::error::NetworkStateError;
use agama_lib::network::types::{BondMode, DeviceType, SSID};
use cidr::IpInet;
use serde::Serialize;
use serde_with::{serde_as, DisplayFromStr};
use serde_with::{serde_as, skip_serializing_none, DisplayFromStr};
use std::{
collections::HashMap,
default::Default,
Expand Down Expand Up @@ -373,11 +373,13 @@ mod tests {
#[derive(Debug, Clone, Serialize)]
pub struct Device {
pub name: String,
#[serde(rename = "type")]
pub type_: DeviceType,
}

/// Represents an availble network connection.
#[serde_as]
#[skip_serializing_none]
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct Connection {
pub id: String,
Expand Down Expand Up @@ -566,23 +568,31 @@ pub enum Status {
Removed,
}

#[skip_serializing_none]
#[derive(Default, Debug, PartialEq, Clone, Serialize)]
pub struct IpConfig {
pub method4: Ipv4Method,
pub method6: Ipv6Method,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub addresses: Vec<IpInet>,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub nameservers: Vec<IpAddr>,
pub gateway4: Option<IpAddr>,
pub gateway6: Option<IpAddr>,
pub routes4: Option<Vec<IpRoute>>,
pub routes6: Option<Vec<IpRoute>>,
}

#[skip_serializing_none]
#[derive(Debug, Default, PartialEq, Clone, Serialize)]
pub struct MatchConfig {
#[serde(skip_serializing_if = "Vec::is_empty")]
pub driver: Vec<String>,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub interface: Vec<String>,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub path: Vec<String>,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub kernel: Vec<String>,
}

Expand Down Expand Up @@ -675,7 +685,9 @@ impl From<UnknownIpMethod> for zbus::fdo::Error {
#[derive(Debug, PartialEq, Clone, Serialize)]
pub struct IpRoute {
pub destination: IpInet,
#[serde(skip_serializing_if = "Option::is_none")]
pub next_hop: Option<IpAddr>,
#[serde(skip_serializing_if = "Option::is_none")]
pub metric: Option<u32>,
}

Expand Down Expand Up @@ -744,11 +756,16 @@ pub struct WirelessConfig {
pub mode: WirelessMode,
#[serde_as(as = "DisplayFromStr")]
pub ssid: SSID,
#[serde(skip_serializing_if = "Option::is_none")]
pub password: Option<String>,
pub security: SecurityProtocol,
#[serde(skip_serializing_if = "Option::is_none")]
pub band: Option<WirelessBand>,
#[serde(skip_serializing_if = "Option::is_none")]
pub channel: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub bssid: Option<macaddr::MacAddr6>,
#[serde(skip_serializing_if = "Option::is_none")]
pub wep_security: Option<WEPSecurity>,
pub hidden: bool,
}
Expand Down Expand Up @@ -852,6 +869,7 @@ impl TryFrom<&str> for SecurityProtocol {
pub struct WEPSecurity {
pub auth_alg: WEPAuthAlg,
pub wep_key_type: WEPKeyType,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub keys: Vec<String>,
pub wep_key_index: u32,
}
Expand Down Expand Up @@ -993,15 +1011,22 @@ impl TryFrom<ConnectionConfig> for BondConfig {
#[derive(Debug, Default, PartialEq, Clone, Serialize)]
pub struct BridgeConfig {
pub stp: bool,
#[serde(skip_serializing_if = "Option::is_none")]
pub priority: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub forward_delay: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub hello_time: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub max_age: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub ageing_time: Option<u32>,
}

#[derive(Debug, Default, PartialEq, Clone, Serialize)]
pub struct BridgePortConfig {
#[serde(skip_serializing_if = "Option::is_none")]
pub priority: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub path_cost: Option<u32>,
}
4 changes: 2 additions & 2 deletions rust/agama-server/src/network/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ impl<T: Adapter> NetworkSystem<T> {
tx.send(conn.cloned()).unwrap();
}
Action::GetConnections(tx) => {
tx.send(self.state.connections.clone());
tx.send(self.state.connections.clone()).unwrap();
}
Action::GetConnectionPath(uuid, tx) => {
let tree = self.tree.lock().await;
Expand All @@ -105,7 +105,7 @@ impl<T: Adapter> NetworkSystem<T> {
tx.send(path).unwrap();
}
Action::GetDevices(tx) => {
tx.send(self.state.devices.clone());
tx.send(self.state.devices.clone()).unwrap();
}
Action::GetDevicesPaths(tx) => {
let tree = self.tree.lock().await;
Expand Down
4 changes: 2 additions & 2 deletions rust/agama-server/src/network/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ pub async fn network_service(events: EventsSender) -> Router {
.with_state(state)
}

#[utoipa::path(get, path = "/devices", responses(
#[utoipa::path(get, path = "/network/devices", responses(
(status = 200, description = "List of devices", body = Vec<Device>)
))]
async fn devices(State(state): State<NetworkState>) -> Json<Vec<Device>> {
Expand All @@ -87,7 +87,7 @@ async fn devices(State(state): State<NetworkState>) -> Json<Vec<Device>> {
Json(devices)
}

#[utoipa::path(get, path = "/connections", responses(
#[utoipa::path(get, path = "/network/connections", responses(
(status = 200, description = "List of known connections", body = Vec<Connection>)
))]
async fn connections(State(state): State<NetworkState>) -> Json<Vec<Connection>> {
Expand Down

0 comments on commit 349f6ee

Please sign in to comment.