diff options
author | Didier Wenzek <didier.wenzek@acidalie.com> | 2021-08-02 10:18:54 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-08-02 11:18:54 +0200 |
commit | b430eb2fba3f1d5a0bac480f4f299f551374e23e (patch) | |
tree | 30c26c77ca9626b42a153b18f142072a2548360f /sm | |
parent | 3e606a8e2e56c64e63321648c5f55e11bd00abb7 (diff) |
[CIT-500] Add helper function to sm_json (#362)
* [CIT-500] Add helper to extract updates from a software update request
* [CIT-500] Add the request-specific topic names
* [CIT-500] Cargo fmt
Co-authored-by: Wenzek <diw@softwareag.com>
Diffstat (limited to 'sm')
-rw-r--r-- | sm/json_sm/src/lib.rs | 110 | ||||
-rw-r--r-- | sm/json_sm/src/messages.rs | 56 |
2 files changed, 166 insertions, 0 deletions
diff --git a/sm/json_sm/src/lib.rs b/sm/json_sm/src/lib.rs index 89396a9e..be1ad57a 100644 --- a/sm/json_sm/src/lib.rs +++ b/sm/json_sm/src/lib.rs @@ -14,6 +14,28 @@ mod tests { use super::*; #[test] + fn topic_names() { + // There are two topics for each kind of requests, + // one for the requests, the other for the responses + assert_eq!( + SoftwareListRequest::topic_name(), + "tedge/commands/req/software/list" + ); + assert_eq!( + SoftwareListResponse::topic_name(), + "tedge/commands/res/software/list" + ); + assert_eq!( + SoftwareUpdateRequest::topic_name(), + "tedge/commands/req/software/update" + ); + assert_eq!( + SoftwareUpdateResponse::topic_name(), + "tedge/commands/res/software/update" + ); + } + + #[test] fn creating_a_software_list_request() { let request = SoftwareListRequest::new(1); @@ -272,6 +294,94 @@ mod tests { } #[test] + fn using_a_software_update_request() { + let json_request = r#"{ + "id": 123, + "updateList": [ + { + "type": "debian", + "modules": [ + { + "name": "nodered", + "version": "1.0.0", + "action": "install" + }, + { + "name": "collectd", + "version": "5.7", + "url": "https://collectd.org/download/collectd-tarballs/collectd-5.12.0.tar.bz2", + "action": "install" + } + ] + }, + { + "type": "docker", + "modules": [ + { + "name": "nginx", + "version": "1.21.0", + "action": "install" + }, + { + "name": "mongodb", + "version": "4.4.6", + "action": "remove" + } + ] + } + ] + }"#; + let request = + SoftwareUpdateRequest::from_json(json_request).expect("Failed to deserialize"); + + assert_eq!(request.id, 123); + + assert_eq!( + request.modules_types(), + vec!["debian".to_string(), "docker".to_string(),] + ); + + assert_eq!( + request.updates_for("debian"), + vec![ + SoftwareModuleUpdate::install(SoftwareModule { + module_type: "debian".to_string(), + name: "nodered".to_string(), + version: Some("1.0.0".to_string()), + url: None, + }), + SoftwareModuleUpdate::install(SoftwareModule { + module_type: "debian".to_string(), + name: "collectd".to_string(), + version: Some("5.7".to_string()), + url: Some( + "https://collectd.org/download/collectd-tarballs/collectd-5.12.0.tar.bz2" + .to_string(), + ), + }), + ] + ); + + assert_eq!( + request.updates_for("docker"), + vec![ + SoftwareModuleUpdate::install(SoftwareModule { + module_type: "docker".to_string(), + name: "nginx".to_string(), + version: Some("1.21.0".to_string()), + url: None, + }), + SoftwareModuleUpdate::remove(SoftwareModule { + module_type: "docker".to_string(), + name: "mongodb".to_string(), + version: Some("4.4.6".to_string()), + url: None, + }), + ] + ); + } + + #[test] fn creating_a_software_update_response() { let request = SoftwareUpdateRequest::new(123); let response = SoftwareUpdateResponse::new(&request); diff --git a/sm/json_sm/src/messages.rs b/sm/json_sm/src/messages.rs index 7be3c89a..7b0d90b1 100644 --- a/sm/json_sm/src/messages.rs +++ b/sm/json_sm/src/messages.rs @@ -37,6 +37,10 @@ impl SoftwareListRequest { pub fn new(id: usize) -> SoftwareListRequest { SoftwareListRequest { id } } + + pub fn topic_name() -> &'static str { + "tedge/commands/req/software/list" + } } /// Message payload definition for SoftwareUpdate request. @@ -58,6 +62,10 @@ impl SoftwareUpdateRequest { } } + pub fn topic_name() -> &'static str { + "tedge/commands/req/software/update" + } + pub fn add_updates(&mut self, plugin_type: &str, updates: Vec<SoftwareModuleUpdate>) { self.update_list.push(SoftwareRequestResponseSoftwareList { plugin_type: plugin_type.to_string(), @@ -67,6 +75,46 @@ impl SoftwareUpdateRequest { .collect::<Vec<SoftwareModuleItem>>(), }) } + + pub fn modules_types(&self) -> Vec<SoftwareType> { + let mut modules_types = vec![]; + + for updates_per_type in self.update_list.iter() { + modules_types.push(updates_per_type.plugin_type.clone()) + } + + modules_types + } + + pub fn updates_for(&self, module_type: &str) -> Vec<SoftwareModuleUpdate> { + let mut updates = vec![]; + + if let Some(items) = self + .update_list + .iter() + .find(|&items| items.plugin_type == module_type) + { + for item in items.modules.iter() { + let module = SoftwareModule { + module_type: module_type.to_string(), + name: item.name.clone(), + version: item.version.clone(), + url: item.url.clone(), + }; + match item.action { + None => {} + Some(SoftwareModuleAction::Install) => { + updates.push(SoftwareModuleUpdate::install(module)); + } + Some(SoftwareModuleAction::Remove) => { + updates.push(SoftwareModuleUpdate::remove(module)); + } + } + } + } + + updates + } } /// Sub list of modules grouped by plugin type. @@ -104,6 +152,10 @@ impl SoftwareListResponse { } } + pub fn topic_name() -> &'static str { + "tedge/commands/res/software/list" + } + pub fn add_modules(&mut self, plugin_type: &str, modules: Vec<SoftwareModule>) { self.response.add_modules( plugin_type.to_string(), @@ -156,6 +208,10 @@ impl SoftwareUpdateResponse { } } + pub fn topic_name() -> &'static str { + "tedge/commands/res/software/update" + } + pub fn add_modules(&mut self, plugin_type: &str, modules: Vec<SoftwareModule>) { self.response.add_modules( plugin_type.to_string(), |