Skip to content

Commit

Permalink
Add up and down status to network model
Browse files Browse the repository at this point in the history
  • Loading branch information
jcronenberg committed Dec 15, 2023
1 parent 627f657 commit e298166
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
15 changes: 14 additions & 1 deletion rust/agama-dbus-server/src/network/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,18 @@ impl Connection {
self.base().status == Status::Removed
}

pub fn is_up(&self) -> bool {
self.base().status == Status::Up
}

pub fn set_up(&mut self) {
self.base_mut().status = Status::Up
}

pub fn set_down(&mut self) {
self.base_mut().status = Status::Down
}

/// Determines whether it is a loopback interface.
pub fn is_loopback(&self) -> bool {
matches!(self, Connection::Loopback(_))
Expand Down Expand Up @@ -540,7 +552,8 @@ impl From<InvalidMacAddress> for zbus::fdo::Error {
#[derive(Debug, Default, Clone, Copy, PartialEq)]
pub enum Status {
#[default]
Present,
Up,
Down,
Removed,
}

Expand Down
24 changes: 23 additions & 1 deletion rust/agama-dbus-server/src/network/nm/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,11 @@ impl<'a> NetworkManagerClient<'a> {
proxy.add_connection(new_conn).await?
};

self.activate_connection(path).await?;
if conn.is_up() {
self.activate_connection(path).await?;
} else {
self.deactivate_connection(path).await?;
}
Ok(())
}

Expand All @@ -160,6 +164,24 @@ impl<'a> NetworkManagerClient<'a> {
Ok(())
}

/// Deactivates a NetworkManager connection.
///
/// * `path`: D-Bus patch of the connection.
async fn deactivate_connection(&self, path: OwnedObjectPath) -> Result<(), ServiceError> {
let proxy = NetworkManagerProxy::new(&self.connection).await?;
match proxy.deactivate_connection(&path.as_ref()).await {
Err(e) => {
// Ignore ConnectionNotActive error since this just means the state is already correct
if e.to_string().contains("ConnectionNotActive") {
Ok(())
} else {
Err(ServiceError::DBus(e))
}
}
_ => Ok(()),
}
}

async fn get_connection_proxy(&self, uuid: Uuid) -> Result<ConnectionProxy, ServiceError> {
let proxy = SettingsProxy::new(&self.connection).await?;
let uuid_s = uuid.to_string();
Expand Down

0 comments on commit e298166

Please sign in to comment.