Skip to content

Commit 5337bd0

Browse files
committed
Add specific patch document for config
1 parent 6a33cec commit 5337bd0

File tree

4 files changed

+38
-21
lines changed

4 files changed

+38
-21
lines changed

rust/agama-server/src/server/types.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@
2020

2121
//! This module defines some ancillary types for the HTTP API.
2222
23-
use std::collections::HashMap;
24-
23+
use agama_lib::install_settings::InstallSettings;
2524
use agama_utils::issue;
26-
use serde::Serialize;
25+
use serde::{Deserialize, Serialize};
26+
use std::collections::HashMap;
2727

28-
#[derive(Serialize, utoipa::ToSchema)]
28+
#[derive(Deserialize, Serialize, utoipa::ToSchema)]
2929
/// Holds the installation issues for each scope.
3030
pub struct IssuesMap {
3131
/// iSCSI issues.
@@ -60,3 +60,10 @@ impl From<HashMap<String, Vec<issue::Issue>>> for IssuesMap {
6060
}
6161
}
6262
}
63+
64+
#[derive(Deserialize, Serialize, utoipa::ToSchema)]
65+
/// Patch for the config.
66+
pub struct ConfigPatch {
67+
/// Update for the current config.
68+
pub update: Option<InstallSettings>,
69+
}

rust/agama-server/src/server/web.rs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
//! This module implements Agama's HTTP API.
2222
23+
use crate::server::types::{ConfigPatch, IssuesMap};
2324
use agama_lib::{error::ServiceError, http, install_settings::InstallSettings};
2425
use agama_manager::{self as manager, message, SystemInfo};
2526
use agama_utils::actor::Handler;
@@ -34,8 +35,6 @@ use hyper::StatusCode;
3435
use serde::Serialize;
3536
use serde_json::json;
3637

37-
use super::types::IssuesMap;
38-
3938
#[derive(thiserror::Error, Debug)]
4039
pub enum Error {
4140
#[error(transparent)]
@@ -52,13 +51,6 @@ impl IntoResponse for Error {
5251
}
5352
}
5453

55-
fn to_option_response<T: Serialize>(value: Option<T>) -> Response {
56-
match value {
57-
Some(inner) => Json(inner).into_response(),
58-
None => StatusCode::NOT_FOUND.into_response(),
59-
}
60-
}
61-
6254
#[derive(Clone)]
6355
pub struct ServerState {
6456
manager: Handler<manager::Service>,
@@ -197,12 +189,14 @@ async fn put_config(
197189
)]
198190
async fn patch_config(
199191
State(state): State<ServerState>,
200-
Json(config): Json<InstallSettings>,
192+
Json(patch): Json<ConfigPatch>,
201193
) -> ServerResult<()> {
202-
state
203-
.manager
204-
.call(message::UpdateConfig::new(config))
205-
.await?;
194+
if let Some(config) = patch.update {
195+
state
196+
.manager
197+
.call(message::UpdateConfig::new(config))
198+
.await?;
199+
}
206200
Ok(())
207201
}
208202

@@ -256,3 +250,10 @@ async fn run_action(
256250
state.manager.call(message::RunAction::new(action)).await?;
257251
Ok(())
258252
}
253+
254+
fn to_option_response<T: Serialize>(value: Option<T>) -> Response {
255+
match value {
256+
Some(inner) => Json(inner).into_response(),
257+
None => StatusCode::NOT_FOUND.into_response(),
258+
}
259+
}

rust/agama-server/src/web/docs/config.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,8 @@ impl ApiDocBuilder for ConfigApiDocBuilder {
159159
.schema_from::<agama_lib::users::RootUserSettings>()
160160
.schema_from::<agama_lib::users::UserPassword>()
161161
.schema_from::<agama_lib::users::UserSettings>()
162+
.schema_from::<crate::server::types::ConfigPatch>()
163+
.schema_from::<crate::server::types::IssuesMap>()
162164
.schema_from::<crate::software::web::SoftwareProposal>()
163165
.schema_from::<agama_manager::message::Action>()
164166
.schema_from::<agama_manager::message::Status>()

rust/agama-server/tests/server_service.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,13 +150,15 @@ async fn test_put_config() -> Result<(), Box<dyn Error>> {
150150
#[test]
151151
#[cfg(not(ci))]
152152
async fn test_patch_config() -> Result<(), Box<dyn Error>> {
153+
use agama_server::server::types::ConfigPatch;
154+
153155
let localization = Config {
154156
locale: Some("es_ES.UTF-8".to_string()),
155157
keymap: Some("es".to_string()),
156158
timezone: Some("Atlantic/Canary".to_string()),
157159
};
158160

159-
let mut config = InstallSettings {
161+
let config = InstallSettings {
160162
localization: Some(localization),
161163
..Default::default()
162164
};
@@ -177,13 +179,18 @@ async fn test_patch_config() -> Result<(), Box<dyn Error>> {
177179
keymap: Some("en".to_string()),
178180
timezone: None,
179181
};
180-
config.localization = Some(localization);
182+
let patch = ConfigPatch {
183+
update: Some(InstallSettings {
184+
localization: Some(localization),
185+
..Default::default()
186+
}),
187+
};
181188

182189
let request = Request::builder()
183190
.uri("/config")
184191
.header("Content-Type", "application/json")
185192
.method(Method::PATCH)
186-
.body(serde_json::to_string(&config)?)
193+
.body(serde_json::to_string(&patch)?)
187194
.unwrap();
188195

189196
let response = server_service.clone().oneshot(request).await.unwrap();

0 commit comments

Comments
 (0)