Skip to content

Commit

Permalink
feat(storage): add HTTP API to set the config model
Browse files Browse the repository at this point in the history
  • Loading branch information
joseivanlopez committed Dec 2, 2024
1 parent 2dff0f6 commit 7bc3c88
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 9 deletions.
8 changes: 8 additions & 0 deletions rust/agama-lib/src/storage/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,14 @@ impl<'a> StorageClient<'a> {
Ok(settings)
}

/// Set the storage config according to the JSON schema
pub async fn set_config_model(&self, model: Box<RawValue>) -> Result<u32, ServiceError> {
Ok(self
.storage_proxy
.set_config_model(serde_json::to_string(&model).unwrap().as_str())
.await?)
}

/// Get the storage config model according to the JSON schema
pub async fn get_config_model(&self) -> Result<Box<RawValue>, ServiceError> {
let serialized_config_model = self.storage_proxy.get_config_model().await?;
Expand Down
3 changes: 3 additions & 0 deletions rust/agama-lib/src/storage/proxies/storage1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ pub trait Storage1 {
/// Get the current storage config according to the JSON schema
fn get_config(&self) -> zbus::Result<String>;

/// Set the storage config model according to the JSON schema
fn set_config_model(&self, model: &str) -> zbus::Result<u32>;

/// Get the storage config model according to the JSON schema
fn get_config_model(&self) -> zbus::Result<String>;

Expand Down
46 changes: 37 additions & 9 deletions rust/agama-server/src/storage/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ pub async fn storage_service(dbus: zbus::Connection) -> Result<Router, ServiceEr
let state = StorageState { client };
let router = Router::new()
.route("/config", put(set_config).get(get_config))
.route("/config_model", get(get_config_model))
.route("/config_model", put(set_config_model).get(get_config_model))
.route("/probe", post(probe))
.route("/devices/dirty", get(devices_dirty))
.route("/devices/system", get(system_devices))
Expand Down Expand Up @@ -157,6 +157,32 @@ async fn get_config(State(state): State<StorageState<'_>>) -> Result<Json<Storag
Ok(Json(settings))
}

/// Sets the storage configuration.
///
/// * `state`: service state.
/// * `config`: storage configuration.
#[utoipa::path(
put,
path = "/config",
context_path = "/api/storage",
operation_id = "set_storage_config",
responses(
(status = 200, description = "Set the storage configuration"),
(status = 400, description = "The D-Bus service could not perform the action")
)
)]
async fn set_config(
State(state): State<StorageState<'_>>,
Json(settings): Json<StorageSettings>,
) -> Result<Json<()>, Error> {
let _status: u32 = state
.client
.set_config(settings)
.await
.map_err(Error::Service)?;
Ok(Json(()))
}

/// Returns the storage config model.
///
/// * `state` : service state.
Expand All @@ -181,27 +207,29 @@ async fn get_config_model(
Ok(Json(config_model))
}

/// Sets the storage configuration.

/// Sets the storage config model.
///
/// * `state`: service state.
/// * `config`: storage configuration.
/// * `config_model`: storage config model.
#[utoipa::path(
put,
path = "/config",
request_body = String,
path = "/config_model",
context_path = "/api/storage",
operation_id = "set_storage_config",
operation_id = "set_storage_config_model",
responses(
(status = 200, description = "Set the storage configuration"),
(status = 200, description = "Set the storage config model"),
(status = 400, description = "The D-Bus service could not perform the action")
)
)]
async fn set_config(
async fn set_config_model(
State(state): State<StorageState<'_>>,
Json(settings): Json<StorageSettings>,
Json(model): Json<Box<RawValue>>,
) -> Result<Json<()>, Error> {
let _status: u32 = state
.client
.set_config(settings)
.set_config_model(model)
.await
.map_err(Error::Service)?;
Ok(Json(()))
Expand Down

0 comments on commit 7bc3c88

Please sign in to comment.