Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions rust/Cargo.lock

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

2 changes: 1 addition & 1 deletion rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ members = [
"suseconnect-agama/suseconnect-agama-sys",
"xtask",
"zypp-agama",
"zypp-agama/zypp-agama-sys",
"zypp-agama/zypp-agama-sys", "agama-iscsi",
]
resolver = "2"

Expand Down
21 changes: 21 additions & 0 deletions rust/agama-iscsi/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[package]
name = "agama-iscsi"
version = "0.1.0"
rust-version.workspace = true
edition.workspace = true

[dependencies]
agama-utils = { path = "../agama-utils" }
agama-storage = { path = "../agama-storage" }
thiserror = "2.0.16"
async-trait = "0.1.89"
zbus = "5.7.1"
tracing = "0.1.41"
tokio = { version = "1.47.1", features = ["macros", "rt-multi-thread", "sync"] }
tokio-stream = "0.1.16"
serde = { version = "1.0.228" }
serde_json = "1.0.140"

[dev-dependencies]
test-context = "0.4.1"
tokio-test = "0.4.4"
100 changes: 100 additions & 0 deletions rust/agama-iscsi/src/client.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// Copyright (c) [2026] SUSE LLC
//
// All Rights Reserved.
//
// This program is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the Free
// Software Foundation; either version 2 of the License, or (at your option)
// any later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, contact SUSE LLC.
//
// To contact SUSE LLC about this file by physical or electronic mail, you may
// find current contact information at www.suse.com.

//! Implements a client to access Agama's D-Bus API related to Bootloader management.

use crate::dbus::ISCSIProxy;
use agama_utils::api::iscsi::Config;
use agama_utils::api::iscsi::DiscoverConfig;
use async_trait::async_trait;
use serde_json::Value;
use zbus::Connection;

#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error(transparent)]
DBus(#[from] zbus::Error),
#[error(transparent)]
Json(#[from] serde_json::Error),
}

pub enum DiscoverResult {
Success,
Failure,
}

#[async_trait]
pub trait ISCSIClient {
async fn discover(&self, config: DiscoverConfig) -> Result<DiscoverResult, Error>;
async fn get_system(&self) -> Result<Option<Value>, Error>;
async fn get_config(&self) -> Result<Option<Config>, Error>;
async fn set_config(&self, config: Option<Config>) -> Result<(), Error>;
}

#[derive(Clone)]
pub struct Client<'a> {
proxy: ISCSIProxy<'a>,
}

impl<'a> Client<'a> {
pub async fn new(connection: Connection) -> Result<Client<'a>, Error> {
let proxy = ISCSIProxy::new(&connection).await?;
Ok(Self { proxy })
}
}

#[async_trait]
impl<'a> ISCSIClient for Client<'a> {
async fn discover(&self, config: DiscoverConfig) -> Result<DiscoverResult, Error> {
let result = self
.proxy
.discover(serde_json::to_string(&config)?.as_str())
.await?;
match result {
0 => Ok(DiscoverResult::Success),
_ => Ok(DiscoverResult::Failure),
}
}

async fn get_system(&self) -> Result<Option<Value>, Error> {
let serialized_system = self.proxy.get_system().await?;
let system: Value = serde_json::from_str(serialized_system.as_str())?;
match system {
Value::Null => Ok(None),
_ => Ok(Some(system)),
}
}

async fn get_config(&self) -> Result<Option<Config>, Error> {
let serialized_config = self.proxy.get_config().await?;
let value: Value = serde_json::from_str(serialized_config.as_str())?;
match value {
Value::Null => Ok(None),
_ => Ok(Some(Config(value))),
}
}

async fn set_config(&self, config: Option<Config>) -> Result<(), Error> {
self.proxy
.set_config(serde_json::to_string(&config)?.as_str())
.await?;
Ok(())
}
}
52 changes: 52 additions & 0 deletions rust/agama-iscsi/src/dbus.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//! # D-Bus interface proxy for: `org.opensuse.Agama.Storage1.ISCSI`
//!
//! This code was generated by `zbus-xmlgen` `5.2.0` from D-Bus introspection data.
//! Source: `iscsi.xml`.
//!
//! You may prefer to adapt it, instead of using it verbatim.
//!
//! More information can be found in the [Writing a client proxy] section of the zbus
//! documentation.
//!
//! This type implements the [D-Bus standard interfaces], (`org.freedesktop.DBus.*`) for which the
//! following zbus API can be used:
//!
//! * [`zbus::fdo::PropertiesProxy`]
//! * [`zbus::fdo::IntrospectableProxy`]
//!
//! Consequently `zbus-xmlgen` did not generate code for the above interfaces.
//!
//! [Writing a client proxy]: https://z-galaxy.github.io/zbus/client.html
//! [D-Bus standard interfaces]: https://dbus.freedesktop.org/doc/dbus-specification.html#standard-interfaces,
use zbus::proxy;
#[proxy(
default_service = "org.opensuse.Agama.Storage1",
default_path = "/org/opensuse/Agama/Storage1/ISCSI",
interface = "org.opensuse.Agama.Storage1.ISCSI",
assume_defaults = true
)]
pub trait ISCSI {
/// Discover method
fn discover(&self, serialized_options: &str) -> zbus::Result<u32>;

/// GetConfig method
fn get_config(&self) -> zbus::Result<String>;

/// GetSystem method
fn get_system(&self) -> zbus::Result<String>;

/// SetConfig method
fn set_config(&self, serialized_config: &str) -> zbus::Result<()>;

/// ProgressChanged signal
#[zbus(signal)]
fn progress_changed(&self, progress: &str) -> zbus::Result<()>;

/// ProgressFinished signal
#[zbus(signal)]
fn progress_finished(&self) -> zbus::Result<()>;

/// SystemChanged signal
#[zbus(signal)]
fn system_changed(&self, system: &str) -> zbus::Result<()>;
}
117 changes: 117 additions & 0 deletions rust/agama-iscsi/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
// Copyright (c) [2026] SUSE LLC
//
// All Rights Reserved.
//
// This program is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the Free
// Software Foundation; either version 2 of the License, or (at your option)
// any later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, contact SUSE LLC.
//
// To contact SUSE LLC about this file by physical or electronic mail, you may
// find current contact information at www.suse.com.

pub mod service;
pub use service::{Service, Starter};

pub mod client;
pub mod message;
pub mod test_utils;

mod dbus;
mod monitor;

use agama_storage as storage;

#[cfg(test)]
mod tests {
use super::*;
use crate::test_utils::TestClient;
use agama_utils::{
actor::Handler,
api::{iscsi::Config, iscsi::DiscoverConfig, Event},
issue, progress, test,
};
use test_context::{test_context, AsyncTestContext};
use tokio::sync::broadcast;

struct Context {
handler: Handler<Service>,
client: TestClient,
}

impl AsyncTestContext for Context {
async fn setup() -> Context {
let (events, _) = broadcast::channel::<Event>(16);
let connection = test::dbus::connection().await.unwrap();
let progress = progress::Service::starter(events.clone()).start();
let issues = issue::Service::starter(events.clone()).start();
let storage = storage::test_utils::start_service(
events.clone(),
issues.clone(),
progress.clone(),
connection.clone(),
)
.await;
let client = TestClient::new();
let handler = Service::starter(storage, events, progress, connection)
.with_client(client.clone())
.start()
.await
.expect("Could not start the iSCSI service");

Context { handler, client }
}
}

#[test_context(Context)]
#[tokio::test]
async fn test_discover(ctx: &mut Context) -> Result<(), service::Error> {
let message = message::Discover::new(DiscoverConfig::new("192.168.100.10", 3260));
ctx.handler.call(message).await?;

let state = ctx.client.state().await;
assert!(state.discovered);

Ok(())
}

#[test_context(Context)]
#[tokio::test]
async fn test_get_system(ctx: &mut Context) -> Result<(), service::Error> {
let system = ctx.handler.call(message::GetSystem).await?;
assert!(system.is_some());

Ok(())
}

#[test_context(Context)]
#[tokio::test]
async fn test_set_config(ctx: &mut Context) -> Result<(), service::Error> {
let config: Config = serde_json::from_str(
r#"
{
"initiator": "ini.test",
"targets": [
{ "name": "target.test" }
]
}
"#,
)
.unwrap();
let message = message::SetConfig::new(Some(config));
ctx.handler.call(message).await?;

let config = ctx.handler.call(message::GetConfig).await?;
assert!(config.is_some());

Ok(())
}
}
Loading
Loading