Skip to content

Commit

Permalink
Add autoconnect property for network connections (#1715)
Browse files Browse the repository at this point in the history
## Problem

Missing autoconnect property for network connections.

## Solution

Add autoconnect property.


## Testing

- *Extended unit test*
- *Tested manually*
```bash
sudo agama auth login && AGAMA_TOKEN=`sudo agama auth show`
curl http://localhost/api/network/connections \
    -H "Content-Type: application/json" \
    -H "Authorization: Bearer $AGAMA_TOKEN" \
    -d '{ "id": "testcon", "method4": "auto", "method6": "auto", "ignoreAutoDns": false, "status": "down", "autoconnect": false }'
curl -X POST http://localhost/api/network/system/apply \
    -H "Content-Type: application/json" \
    -H "Authorization: Bearer $AGAMA_TOKEN"
nmcli con show testcon
# See autoconnect set to "no".
```
  • Loading branch information
imobachgs authored Nov 6, 2024
2 parents 016a4d9 + 0b27d8b commit 4e58df2
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 0 deletions.
26 changes: 26 additions & 0 deletions rust/agama-lib/src/network/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,50 +165,76 @@ pub struct NetworkDevice {
pub state: DeviceState,
}

/// Represents the configuration details for a network connection
#[derive(Clone, Debug, Default, Serialize, Deserialize, utoipa::ToSchema)]
#[serde(rename_all = "camelCase")]
pub struct NetworkConnection {
/// Unique identifier for the network connection
pub id: String,
/// IPv4 method used for the network connection
#[serde(skip_serializing_if = "Option::is_none")]
pub method4: Option<String>,
/// Gateway IP address for the IPv4 connection
#[serde(skip_serializing_if = "Option::is_none")]
pub gateway4: Option<IpAddr>,
/// IPv6 method used for the network connection
#[serde(skip_serializing_if = "Option::is_none")]
pub method6: Option<String>,
/// Gateway IP address for the IPv6 connection
#[serde(skip_serializing_if = "Option::is_none")]
pub gateway6: Option<IpAddr>,
/// List of assigned IP addresses
#[serde(skip_serializing_if = "Vec::is_empty", default)]
pub addresses: Vec<IpInet>,
/// List of DNS server IP addresses
#[serde(skip_serializing_if = "Vec::is_empty", default)]
pub nameservers: Vec<IpAddr>,
/// List of search domains for DNS resolution
#[serde(skip_serializing_if = "Vec::is_empty", default)]
pub dns_searchlist: Vec<String>,
/// Specifies whether to ignore automatically assigned DNS settings
#[serde(skip_serializing_if = "Option::is_none")]
pub ignore_auto_dns: Option<bool>,
/// Wireless settings for the connection
#[serde(skip_serializing_if = "Option::is_none")]
pub wireless: Option<WirelessSettings>,
/// Network interface associated with the connection
#[serde(skip_serializing_if = "Option::is_none")]
pub interface: Option<String>,
/// Match settings for the network connection
#[serde(skip_serializing_if = "Option::is_none")]
pub match_settings: Option<MatchSettings>,
/// Identifier for the parent connection, if this connection is part of a bond
#[serde(skip_serializing_if = "Option::is_none")]
pub parent: Option<String>,
/// Bonding settings if part of a bond
#[serde(skip_serializing_if = "Option::is_none")]
pub bond: Option<BondSettings>,
/// MAC address of the connection's interface
#[serde(rename = "mac-address", skip_serializing_if = "Option::is_none")]
pub mac_address: Option<String>,
/// Current status of the network connection
#[serde(skip_serializing_if = "Option::is_none")]
pub status: Option<Status>,
/// Maximum Transmission Unit (MTU) for the connection
#[serde(skip_serializing_if = "is_zero", default)]
pub mtu: u32,
/// IEEE 802.1X settings
#[serde(rename = "ieee-8021x", skip_serializing_if = "Option::is_none")]
pub ieee_8021x: Option<IEEE8021XSettings>,
/// Specifies if the connection should automatically connect
#[serde(default = "default_true")]
pub autoconnect: bool,
}

fn is_zero<T: PartialEq + From<u16>>(u: &T) -> bool {
*u == T::from(0)
}

fn default_true() -> bool {
true
}

impl NetworkConnection {
/// Device type expected for the network connection.
///
Expand Down
5 changes: 5 additions & 0 deletions rust/agama-server/src/network/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,7 @@ pub struct Connection {
pub match_config: MatchConfig,
pub config: ConnectionConfig,
pub ieee_8021x_config: Option<IEEE8021XConfig>,
pub autoconnect: bool,
}

impl Connection {
Expand Down Expand Up @@ -584,6 +585,7 @@ impl Default for Connection {
match_config: Default::default(),
config: Default::default(),
ieee_8021x_config: Default::default(),
autoconnect: true,
}
}
}
Expand Down Expand Up @@ -634,6 +636,7 @@ impl TryFrom<NetworkConnection> for Connection {
connection.ip_config.gateway6 = conn.gateway6;
connection.interface = conn.interface;
connection.mtu = conn.mtu;
connection.autoconnect = conn.autoconnect;

Ok(connection)
}
Expand All @@ -660,6 +663,7 @@ impl TryFrom<Connection> for NetworkConnection {
let ieee_8021x: Option<IEEE8021XSettings> = conn
.ieee_8021x_config
.and_then(|x| IEEE8021XSettings::try_from(x).ok());
let autoconnect = conn.autoconnect;

let mut connection = NetworkConnection {
id,
Expand All @@ -676,6 +680,7 @@ impl TryFrom<Connection> for NetworkConnection {
addresses,
mtu,
ieee_8021x,
autoconnect,
..Default::default()
};

Expand Down
16 changes: 16 additions & 0 deletions rust/agama-server/src/network/nm/dbus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ pub fn connection_to_dbus<'a>(
let mut connection_dbus = HashMap::from([
("id", conn.id.as_str().into()),
("type", ETHERNET_KEY.into()),
("autoconnect", conn.autoconnect.into()),
]);

if let Some(interface) = &conn.interface {
Expand Down Expand Up @@ -652,6 +653,10 @@ fn base_connection_from_dbus(conn: &OwnedNestedHash) -> Result<Connection, NmErr
..Default::default()
};

if let Some(autoconnect) = get_optional_property(connection, "autoconnect")? {
base_connection.autoconnect = autoconnect;
}

if let Some(match_config) = conn.get("match") {
base_connection.match_config = match_config_from_dbus(match_config)?;
}
Expand Down Expand Up @@ -1201,6 +1206,7 @@ mod test {
let connection_section = HashMap::from([
("id".to_string(), Value::new("eth0").try_to_owned()?),
("uuid".to_string(), Value::new(uuid).try_to_owned()?),
("autoconnect".to_string(), Value::new(false).try_to_owned()?),
]);

let address_v4_data = vec![HashMap::from([
Expand Down Expand Up @@ -1342,6 +1348,8 @@ mod test {
metric: Some(100)
}]
);
assert_eq!(connection.autoconnect, false);

Ok(())
}

Expand Down Expand Up @@ -2049,6 +2057,7 @@ mod test {
ip_config,
mac_address,
mtu: 1500_u32,
autoconnect: false,
..Default::default()
}
}
Expand All @@ -2058,6 +2067,13 @@ mod test {
let id: &str = connection_dbus.get("id").unwrap().downcast_ref().unwrap();
assert_eq!(id, "agama");

let autoconnect: bool = connection_dbus
.get("autoconnect")
.unwrap()
.downcast_ref()
.unwrap();
assert_eq!(autoconnect, false);

let ethernet_connection = conn_dbus.get(ETHERNET_KEY).unwrap();
let mac_address: &str = ethernet_connection
.get("assigned-mac-address")
Expand Down
6 changes: 6 additions & 0 deletions rust/package/agama.changes
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
-------------------------------------------------------------------
Wed Oct 30 15:27:11 UTC 2024 - Jorik Cronenberg <jorik.cronenberg@suse.com>

- Add autoconnect property for network connections
(gh#agama-project/agama#1715)

-------------------------------------------------------------------
Mon Oct 28 09:24:48 UTC 2024 - Imobach Gonzalez Sosa <igonzalezsosa@suse.com>

Expand Down

0 comments on commit 4e58df2

Please sign in to comment.