Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose the software service through the HTTP/JSON API #1069

Merged
merged 24 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
e75dcf8
rust: add a software service
imobachgs Feb 29, 2024
d424b68
rust: add support to select a product via HTTP
imobachgs Feb 29, 2024
57cac7f
rust: emit product changes
imobachgs Feb 29, 2024
fc3e12a
rust: move software config to a separate resource
imobachgs Mar 1, 2024
2b5be30
rust: share the SoftwareProductProxy
imobachgs Mar 1, 2024
5edea12
rust: use the software client in the web API
imobachgs Mar 1, 2024
9c65f85
rust: add patterns support to the HTTP API
imobachgs Mar 1, 2024
0cbb436
rust: emit patterns selection changes
imobachgs Mar 1, 2024
35ffb0e
rust: emit stream events in a single place
imobachgs Mar 2, 2024
6340803
doc: update the OpenAPI documentation
imobachgs Mar 4, 2024
0e0b0e3
rust: refactor the CLI arguments handling
imobachgs Mar 4, 2024
6e9d476
rust: fix service tests
imobachgs Mar 4, 2024
602a6f8
rust: remove unneeded lifetime annotations
imobachgs Mar 4, 2024
4417e43
software web functions return an result
imobachgs Mar 4, 2024
4679589
rust: add an endpoint /software/proposal
imobachgs Mar 4, 2024
4896fb2
rust: add an endpoint to start the software probing
imobachgs Mar 4, 2024
6d6ee2d
rust: PatternsChanged includes the list of patterns
imobachgs Mar 4, 2024
ab55a05
rust: document used_disk_space
imobachgs Mar 4, 2024
c1b2853
rust: merge PatternStatus and SelectedBy
imobachgs Mar 5, 2024
0a57873
rust: handle errors when creating the service router
imobachgs Mar 5, 2024
9551eae
rust: fix service tests
imobachgs Mar 5, 2024
6a6cf06
rust: add missing elements to the OpenAPI documentation
imobachgs Mar 5, 2024
7cbe2b7
rust: improve SoftwareError description
imobachgs Mar 5, 2024
4c0cb3b
rust: handle invalid pattern selection reasons
imobachgs Mar 5, 2024
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
2 changes: 1 addition & 1 deletion rust/agama-lib/src/software.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ pub mod proxies;
mod settings;
mod store;

pub use client::{Pattern, SelectedBy, SoftwareClient};
pub use client::{Pattern, SelectedBy, SoftwareClient, UnknownSelectedBy};
pub use settings::SoftwareSettings;
pub use store::SoftwareStore;
36 changes: 27 additions & 9 deletions rust/agama-lib/src/software/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub struct Pattern {
}

/// Represents the reason why a pattern is selected.
#[derive(Clone, Copy, Debug, Serialize)]
#[derive(Clone, Copy, Debug, PartialEq, Serialize)]
pub enum SelectedBy {
/// The pattern was selected by the user.
User = 0,
Expand All @@ -32,12 +32,18 @@ pub enum SelectedBy {
None = 2,
}

impl From<u8> for SelectedBy {
fn from(value: u8) -> Self {
#[derive(Debug, thiserror::Error)]
#[error("Unknown selected by value: '{0}'")]
pub struct UnknownSelectedBy(u8);

impl TryFrom<u8> for SelectedBy {
type Error = UnknownSelectedBy;

fn try_from(value: u8) -> Result<Self, Self::Error> {
match value {
0 => Self::User,
1 => Self::Auto,
_ => Self::None,
0 => Ok(Self::User),
1 => Ok(Self::Auto),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

now, I am confused, why is not there 2 => Ok(Self::None)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because it is not expected to get a 2 from the API (it does not exist).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, ok.

_ => Err(UnknownSelectedBy(value)),
}
}
}
Expand Down Expand Up @@ -83,8 +89,14 @@ impl<'a> SoftwareClient<'a> {
.selected_patterns()
.await?
.into_iter()
.filter(|(_id, reason)| *reason == SelectedBy::User as u8)
.map(|(id, _reason)| id)
.filter_map(|(id, reason)| match SelectedBy::try_from(reason) {
Ok(reason) if reason == SelectedBy::User => Some(id),
Ok(_reason) => None,
Err(e) => {
log::warn!("Ignoring pattern {}. Error: {}", &id, e);
None
}
})
.collect();
Ok(patterns)
}
Expand All @@ -94,7 +106,13 @@ impl<'a> SoftwareClient<'a> {
let patterns = self.software_proxy.selected_patterns().await?;
let patterns = patterns
.into_iter()
.map(|(id, reason)| (id, reason.into()))
.filter_map(|(id, reason)| match SelectedBy::try_from(reason) {
Ok(reason) => Some((id, reason)),
Err(e) => {
log::warn!("Ignoring pattern {}. Error: {}", &id, e);
None
}
})
.collect();
Ok(patterns)
}
Expand Down
28 changes: 23 additions & 5 deletions rust/agama-server/src/software/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use agama_lib::{
product::{Product, ProductClient},
software::{
proxies::{Software1Proxy, SoftwareProductProxy},
Pattern, SelectedBy, SoftwareClient,
Pattern, SelectedBy, SoftwareClient, UnknownSelectedBy,
},
};
use axum::{
Expand Down Expand Up @@ -90,16 +90,34 @@ async fn patterns_changed_stream(
.await
.then(|change| async move {
if let Ok(patterns) = change.get().await {
let patterns: HashMap<String, SelectedBy> =
patterns.into_iter().map(|(k, v)| (k, v.into())).collect();
return Some(Event::PatternsChanged(patterns));
return match reason_to_selected_by(patterns) {
Ok(patterns) => Some(patterns),
Err(error) => {
log::warn!("Ignoring the list of changed patterns. Error: {}", error);
None
}
};
}
None
})
.filter_map(|e| e);
.filter_map(|e| e.map(Event::PatternsChanged));
Ok(stream)
}

// Returns a hash replacing the selection "reason" from D-Bus with a SelectedBy variant.
fn reason_to_selected_by(
patterns: HashMap<String, u8>,
) -> Result<HashMap<String, SelectedBy>, UnknownSelectedBy> {
let mut selected: HashMap<String, SelectedBy> = HashMap::new();
for (id, reason) in patterns {
match SelectedBy::try_from(reason) {
Ok(selected_by) => selected.insert(id, selected_by),
Err(e) => return Err(e),
};
}
Ok(selected)
}

/// Sets up and returns the axum service for the software module.
pub async fn software_service(dbus: zbus::Connection) -> Result<Router, ServiceError> {
let product = ProductClient::new(dbus.clone()).await?;
Expand Down
Loading