Skip to content

Commit 2ea9e8f

Browse files
committed
Set storage config
1 parent e4c32e6 commit 2ea9e8f

File tree

9 files changed

+82
-60
lines changed

9 files changed

+82
-60
lines changed

rust/agama-manager/src/service.rs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,8 @@ impl MessageHandler<message::GetExtendedConfig> for Service {
153153
Ok(Config {
154154
l10n: Some(l10n),
155155
questions: Some(questions),
156-
storage,
156+
storage: storage.as_ref().and_then(|c| c.storage.clone()),
157+
legacy_autoyast_storage: storage.and_then(|c| c.legacy_autoyast_storage),
157158
})
158159
}
159160
}
@@ -172,17 +173,21 @@ impl MessageHandler<message::GetConfig> for Service {
172173
impl MessageHandler<message::SetConfig> for Service {
173174
/// Sets the config.
174175
async fn handle(&mut self, message: message::SetConfig) -> Result<(), Error> {
176+
let config = message.config;
177+
175178
self.l10n
176-
.call(l10n::message::SetConfig::new(message.config.l10n.clone()))
179+
.call(l10n::message::SetConfig::new(config.l10n.clone()))
177180
.await?;
178181

179182
self.questions
180-
.call(question::message::SetConfig::new(
181-
message.config.questions.clone(),
182-
))
183+
.call(question::message::SetConfig::new(config.questions.clone()))
184+
.await?;
185+
186+
self.storage
187+
.call(storage::message::SetConfig::new((&config).try_into().ok()))
183188
.await?;
184189

185-
self.config = message.config;
190+
self.config = config;
186191
Ok(())
187192
}
188193
}
@@ -208,6 +213,12 @@ impl MessageHandler<message::UpdateConfig> for Service {
208213
.await?;
209214
}
210215

216+
if let Some(storage) = (&config).try_into().ok() {
217+
self.storage
218+
.call(storage::message::SetConfig::with(storage))
219+
.await?;
220+
}
221+
211222
self.config = config;
212223
Ok(())
213224
}

rust/agama-storage/src/client.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
//! Implements a client to access Agama's storage service.
2222
23-
use agama_utils::api::storage::Config;
23+
use crate::config::Config;
2424
use serde_json::{value::RawValue, Value};
2525
use zbus::{names::BusName, zvariant::OwnedObjectPath, Connection, Message};
2626

@@ -78,9 +78,7 @@ impl Client {
7878

7979
pub async fn get_config(&self) -> Result<Option<Config>, Error> {
8080
let message = self.call("GetConfig", &()).await?;
81-
let value: String = message.body().deserialize()?;
82-
let config = serde_json::from_str(value.as_str())?;
83-
Ok(config)
81+
self.json_from(message)
8482
}
8583

8684
pub async fn get_config_model(&self) -> Result<Option<Box<RawValue>>, Error> {
@@ -104,8 +102,10 @@ impl Client {
104102
Ok(())
105103
}
106104

107-
pub async fn set_config(&self, config: Box<RawValue>) -> Result<(), Error> {
108-
self.call("SetConfig", &(config.to_string())).await?;
105+
pub async fn set_config(&self, config: Option<Config>) -> Result<(), Error> {
106+
let config = config.filter(|c| c.is_some());
107+
let json = serde_json::to_string(&config)?;
108+
self.call("SetConfig", &(json)).await?;
109109
Ok(())
110110
}
111111

@@ -140,17 +140,21 @@ impl Client {
140140
.map_err(|e| e.into())
141141
}
142142

143-
fn json_from(&self, message: Message) -> Result<Option<Box<RawValue>>, Error> {
143+
fn json_from<T: serde::de::DeserializeOwned>(
144+
&self,
145+
message: Message,
146+
) -> Result<Option<T>, Error> {
144147
let value: String = message.body().deserialize()?;
145148
if self.is_null(value.as_str()) {
146149
return Ok(None);
147150
}
148-
let json = RawValue::from_string(value)?;
151+
let json = serde_json::from_str(value.as_str())?;
149152
Ok(Some(json))
150153
}
151154

152155
fn is_null(&self, value: &str) -> bool {
153-
match serde_json::from_str::<Value>(value) {
156+
let value = serde_json::from_str::<Value>(value);
157+
match value {
154158
Ok(Value::Null) => true,
155159
Ok(_) => false,
156160
Err(_) => false,

rust/agama-utils/src/api/storage/config.rs renamed to rust/agama-storage/src/config.rs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,36 @@
1818
// To contact SUSE LLC about this file by physical or electronic mail, you may
1919
// find current contact information at www.suse.com.
2020

21+
use agama_utils::api;
2122
use serde::{Deserialize, Serialize};
2223
use serde_json::value::RawValue;
2324

24-
#[derive(Clone, Debug, Default, Serialize, Deserialize, utoipa::ToSchema)]
25+
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
2526
#[serde(rename_all = "camelCase")]
2627
pub struct Config {
27-
#[serde(default)]
2828
#[serde(skip_serializing_if = "Option::is_none")]
29-
#[schema(value_type = Object)]
3029
pub storage: Option<Box<RawValue>>,
31-
#[serde(default)]
3230
#[serde(skip_serializing_if = "Option::is_none")]
33-
#[schema(value_type = Object)]
3431
pub legacy_autoyast_storage: Option<Box<RawValue>>,
3532
}
33+
34+
impl Config {
35+
pub fn is_some(&self) -> bool {
36+
self.storage.is_some() || self.legacy_autoyast_storage.is_some()
37+
}
38+
}
39+
40+
impl TryFrom<&api::Config> for Config {
41+
type Error = ();
42+
43+
fn try_from(config: &api::Config) -> Result<Self, Self::Error> {
44+
if config.storage.is_none() && config.legacy_autoyast_storage.is_none() {
45+
Err(())
46+
} else {
47+
Ok(Config {
48+
storage: config.storage.clone(),
49+
legacy_autoyast_storage: config.legacy_autoyast_storage.clone(),
50+
})
51+
}
52+
}
53+
}

rust/agama-storage/src/lib.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,15 @@
1818
// To contact SUSE LLC about this file by physical or electronic mail, you may
1919
// find current contact information at www.suse.com.
2020

21+
pub mod start;
22+
pub use start::start;
23+
2124
pub mod service;
2225
pub use service::Service;
2326

27+
mod config;
28+
pub use config::Config;
29+
2430
mod client;
2531
pub mod message;
2632
mod monitor;
27-
28-
pub mod start;
29-
pub use start::start;

rust/agama-storage/src/message.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
// To contact SUSE LLC about this file by physical or electronic mail, you may
1919
// find current contact information at www.suse.com.
2020

21-
use agama_utils::{actor::Message, api::storage::Config};
21+
use crate::config::Config;
22+
use agama_utils::actor::Message;
2223
use serde_json::value::RawValue;
2324

2425
#[derive(Clone)]
@@ -94,13 +95,19 @@ impl Message for SetProduct {
9495

9596
#[derive(Clone)]
9697
pub struct SetConfig {
97-
pub config: Box<RawValue>,
98+
pub config: Option<Config>,
9899
}
99100

100101
impl SetConfig {
101-
pub fn new(config: Box<RawValue>) -> Self {
102+
pub fn new(config: Option<Config>) -> Self {
102103
Self { config }
103104
}
105+
106+
pub fn with(config: Config) -> Self {
107+
Self {
108+
config: Some(config),
109+
}
110+
}
104111
}
105112

106113
impl Message for SetConfig {

rust/agama-storage/src/service.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@
1818
// To contact SUSE LLC about this file by physical or electronic mail, you may
1919
// find current contact information at www.suse.com.
2020

21-
use crate::client::{self, Client};
22-
use crate::message;
23-
use agama_utils::{
24-
actor::{self, Actor, MessageHandler},
25-
api::storage::Config,
21+
use crate::{
22+
client::{self, Client},
23+
config::Config,
24+
message,
2625
};
26+
use agama_utils::actor::{self, Actor, MessageHandler};
2727
use async_trait::async_trait;
2828
use serde_json::value::RawValue;
2929

rust/agama-utils/src/api.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,3 @@ pub use action::Action;
5050

5151
pub mod l10n;
5252
pub mod question;
53-
pub mod storage;

rust/agama-utils/src/api/config.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@
1818
// To contact SUSE LLC about this file by physical or electronic mail, you may
1919
// find current contact information at www.suse.com.
2020

21-
use crate::api::{l10n, question, storage};
21+
use crate::api::{l10n, question};
2222
use serde::{Deserialize, Serialize};
23+
use serde_json::value::RawValue;
2324

2425
#[derive(Clone, Debug, Default, Deserialize, Serialize, utoipa::ToSchema)]
2526
#[serde(rename_all = "camelCase")]
@@ -29,8 +30,12 @@ pub struct Config {
2930
pub l10n: Option<l10n::Config>,
3031
#[serde(skip_serializing_if = "Option::is_none")]
3132
pub questions: Option<question::Config>,
32-
#[serde(flatten)]
33-
pub storage: Option<storage::Config>,
33+
#[serde(skip_serializing_if = "Option::is_none")]
34+
#[schema(value_type = Object)]
35+
pub storage: Option<Box<RawValue>>,
36+
#[serde(skip_serializing_if = "Option::is_none")]
37+
#[schema(value_type = Object)]
38+
pub legacy_autoyast_storage: Option<Box<RawValue>>,
3439
}
3540

3641
/// Patch for the config.

rust/agama-utils/src/api/storage.rs

Lines changed: 0 additions & 25 deletions
This file was deleted.

0 commit comments

Comments
 (0)