Skip to content

Commit

Permalink
Move mac_address functions to BaseConnection
Browse files Browse the repository at this point in the history
  • Loading branch information
jcronenberg committed Nov 30, 2023
1 parent b9556c5 commit e3b3305
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 25 deletions.
4 changes: 2 additions & 2 deletions rust/agama-dbus-server/src/network/dbus/interfaces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,13 +242,13 @@ impl Connection {
/// Custom mac-address
#[dbus_interface(property)]
pub async fn mac_address(&self) -> String {
self.get_connection().await.mac_address_string()
self.get_connection().await.base().mac_address_string()
}

#[dbus_interface(property)]
pub async fn set_mac_address(&mut self, mac_address: &str) -> zbus::fdo::Result<()> {
let mut connection = self.get_connection().await;
connection.set_mac_address(mac_address)?;
connection.base_mut().set_mac_address(mac_address)?;
self.update_connection(connection).await
}
}
Expand Down
34 changes: 18 additions & 16 deletions rust/agama-dbus-server/src/network/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,22 +307,6 @@ impl Connection {
matches!(self, Connection::Loopback(_))
}

pub fn mac_address_string(&self) -> String {
match &self.base().mac_address {
Some(mac) => mac.to_string(),
None => "".to_string(),
}
}

pub fn set_mac_address(&mut self, mac_address: &str) -> Result<(), InvalidMacAddress> {
self.base_mut().mac_address = if mac_address.is_empty() {
None
} else {
Some(MacAddress::from_str(mac_address)?)
};
Ok(())
}

pub fn is_ethernet(&self) -> bool {
matches!(self, Connection::Loopback(_)) || matches!(self, Connection::Ethernet(_))
}
Expand All @@ -339,6 +323,24 @@ pub struct BaseConnection {
pub match_config: MatchConfig,
}

impl BaseConnection {
pub fn mac_address_string(&self) -> String {
match &self.mac_address {
Some(mac) => mac.to_string(),
None => "".to_string(),
}
}

pub fn set_mac_address(&mut self, mac_address: &str) -> Result<(), InvalidMacAddress> {
self.mac_address = if mac_address.is_empty() {
None
} else {
Some(MacAddress::from_str(mac_address)?)
};
Ok(())
}
}

impl PartialEq for BaseConnection {
fn eq(&self, other: &Self) -> bool {
self.id == other.id && self.uuid == other.uuid && self.ip_config == other.ip_config
Expand Down
11 changes: 4 additions & 7 deletions rust/agama-dbus-server/src/network/nm/dbus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub fn connection_to_dbus(conn: &Connection) -> NestedHash {
if conn.is_ethernet() {
let ethernet_config = HashMap::from([(
"assigned-mac-address",
Value::new(conn.mac_address_string()),
Value::new(conn.base().mac_address_string()),
)]);
result.insert(ETHERNET_KEY, ethernet_config);
} else if let Connection::Wireless(wireless) = conn {
Expand Down Expand Up @@ -226,10 +226,7 @@ fn wireless_config_to_dbus(conn: &WirelessConnection) -> NestedHash {
("ssid", Value::new(config.ssid.to_vec())),
(
"assigned-mac-address",
Value::new(match &conn.base.mac_address {
Some(mac) => mac.to_string(),
None => "".to_string(),
}),
Value::new(conn.base.mac_address_string()),
),
]);

Expand Down Expand Up @@ -626,7 +623,7 @@ mod test {
let match_config = connection.match_config();
assert_eq!(match_config.kernel, vec!["pci-0000:00:19.0"]);

assert_eq!(connection.mac_address_string(), "12:34:56:78:9A:BC");
assert_eq!(connection.base().mac_address_string(), "12:34:56:78:9A:BC");

assert_eq!(
ip_config.addresses,
Expand Down Expand Up @@ -870,7 +867,7 @@ mod test {

let mut updated = Connection::Ethernet(EthernetConnection::default());
updated.set_interface("");
assert!(updated.set_mac_address("").is_ok());
assert!(updated.base_mut().set_mac_address("").is_ok());
let updated = connection_to_dbus(&updated);

let merged = merge_dbus_connections(&original, &updated);
Expand Down

0 comments on commit e3b3305

Please sign in to comment.