diff options
author | Rina Fujino <18257209+rina23q@users.noreply.github.com> | 2022-05-09 14:33:05 +0200 |
---|---|---|
committer | Rina Fujino <18257209+rina23q@users.noreply.github.com> | 2022-05-17 14:21:50 +0200 |
commit | 60373061fc66f2362eaa6b52c78df4f40f62119d (patch) | |
tree | 2ff4320c993f5b08ab14b531dc21d6245d3d1d3c | |
parent | 29473a8b9912be60bfdad22684d97301c2e80960 (diff) |
Add type support on the plugin configuration
Signed-off-by: Rina Fujino <18257209+rina23q@users.noreply.github.com>
-rw-r--r-- | crates/core/c8y_api/src/http_proxy.rs | 8 | ||||
-rw-r--r-- | crates/core/c8y_smartrest/src/smartrest_deserializer.rs | 2 | ||||
-rw-r--r-- | plugins/c8y_configuration_plugin/src/config.rs | 302 | ||||
-rw-r--r-- | plugins/c8y_configuration_plugin/src/download.rs | 45 | ||||
-rw-r--r-- | plugins/c8y_configuration_plugin/src/error.rs | 6 | ||||
-rw-r--r-- | plugins/c8y_configuration_plugin/src/main.rs | 3 | ||||
-rw-r--r-- | plugins/c8y_configuration_plugin/src/upload.rs | 10 |
7 files changed, 252 insertions, 124 deletions
diff --git a/crates/core/c8y_api/src/http_proxy.rs b/crates/core/c8y_api/src/http_proxy.rs index ac0423da..fa9d5a4d 100644 --- a/crates/core/c8y_api/src/http_proxy.rs +++ b/crates/core/c8y_api/src/http_proxy.rs @@ -8,7 +8,7 @@ use c8y_smartrest::{error::SMCumulocityMapperError, smartrest_deserializer::Smar use mockall::automock; use mqtt_channel::{Connection, PubChannel, StreamExt, Topic, TopicFilter}; use reqwest::Url; -use std::{collections::HashMap, path::Path, time::Duration}; +use std::{collections::HashMap, time::Duration}; use tedge_config::{ C8yUrlSetting, ConfigSettingAccessor, ConfigSettingAccessorStringExt, DeviceIdSetting, MqttBindAddressSetting, MqttPortSetting, TEdgeConfig, @@ -46,7 +46,7 @@ pub trait C8YHttpProxy: Send + Sync { async fn upload_config_file( &mut self, - config_path: &Path, + config_type: &str, config_content: &str, ) -> Result<String, SMCumulocityMapperError>; } @@ -403,12 +403,12 @@ impl C8YHttpProxy for JwtAuthHttpProxy { async fn upload_config_file( &mut self, - config_path: &Path, + config_type: &str, config_content: &str, ) -> Result<String, SMCumulocityMapperError> { let token = self.get_jwt_token().await?; - let config_file_event = self.create_event(config_path.display().to_string(), None, None); + let config_file_event = self.create_event(config_type.to_string(), None, None); let event_response_id = self.send_event_internal(config_file_event).await?; let binary_upload_event_url = self .end_point diff --git a/crates/core/c8y_smartrest/src/smartrest_deserializer.rs b/crates/core/c8y_smartrest/src/smartrest_deserializer.rs index 5d28fac0..b5aa5b46 100644 --- a/crates/core/c8y_smartrest/src/smartrest_deserializer.rs +++ b/crates/core/c8y_smartrest/src/smartrest_deserializer.rs @@ -244,7 +244,7 @@ pub struct SmartRestConfigUploadRequest { impl SmartRestRequestGeneric for SmartRestConfigUploadRequest {} -#[derive(Debug, Deserialize, Serialize, PartialEq)] +#[derive(Debug, Deserialize, Serialize, PartialEq, Clone)] pub struct SmartRestConfigDownloadRequest { pub message_id: String, pub device: String, diff --git a/plugins/c8y_configuration_plugin/src/config.rs b/plugins/c8y_configuration_plugin/src/config.rs index f6c76d20..dafa9c96 100644 --- a/plugins/c8y_configuration_plugin/src/config.rs +++ b/plugins/c8y_configuration_plugin/src/config.rs @@ -1,52 +1,70 @@ +use crate::error::ConfigManagementError; use c8y_smartrest::topic::C8yTopic; use mqtt_channel::Message; use serde::Deserialize; +use std::borrow::Borrow; use std::collections::HashSet; use std::fs; use std::hash::{Hash, Hasher}; -use std::path::PathBuf; +use std::path::{Path, PathBuf}; use tracing::{info, warn}; -#[derive(Deserialize, Debug, Eq, PartialEq, Default)] +const DEFAULT_PLUGIN_CONFIG_TYPE: &str = "c8y-configuration-plugin"; + +#[derive(Deserialize, Debug, Default)] #[serde(deny_unknown_fields)] +struct RawPluginConfig { + pub files: Vec<RawFileEntry>, +} + +#[derive(Deserialize, Debug, Default, PartialEq)] +#[serde(deny_unknown_fields)] +pub struct RawFileEntry { + pub path: String, + #[serde(rename = "type")] + config_type: Option<String>, +} + +#[derive(Debug, Eq, PartialEq, Default, Clone)] pub struct PluginConfig { pub files: HashSet<FileEntry>, } -#[derive(Deserialize, Debug, Eq, Default, Clone)] -#[serde(deny_unknown_fields)] +#[derive(Debug, Eq, Default, Clone)] pub struct FileEntry { - path: String, + pub path: String, + config_type: String, } impl Hash for FileEntry { fn hash<H: Hasher>(&self, state: &mut H) { - self.path.hash(state); + self.config_type.hash(state); } } impl PartialEq for FileEntry { fn eq(&self, other: &Self) -> bool { - self.path == other.path + self.config_type == other.config_type } } -impl FileEntry { - pub(crate) fn new(path: String) -> Self { - Self { path } +impl Borrow<String> for FileEntry { + fn borrow(&self) -> &String { + &self.config_type } } -impl PluginConfig { - pub fn new(config_file_path: PathBuf) -> Self { - let config_file_path_str = config_file_path.as_path().display().to_string(); - Self::read_config(config_file_path).add_file(config_file_path_str) +impl FileEntry { + pub fn new(path: String, config_type: String) -> Self { + Self { path, config_type } } +} - fn read_config(path: PathBuf) -> Self { - let path_str = path.as_path().display().to_string(); +impl RawPluginConfig { + fn new(config_file_path: PathBuf) -> Self { + let path_str = config_file_path.as_path().display().to_string(); info!("Reading the config file from {}", path_str); - match fs::read_to_string(path) { + match fs::read_to_string(config_file_path) { Ok(contents) => match toml::from_str(contents.as_str()) { Ok(config) => config, _ => { @@ -63,11 +81,38 @@ impl PluginConfig { } } } +} + +impl PluginConfig { + pub fn new(config_file_path: PathBuf) -> Self { + let plugin_config = Self::new_with_config_file_entry(&config_file_path); + let raw_config = RawPluginConfig::new(config_file_path); + plugin_config.add_entries_from_raw_config(raw_config) + } + + fn new_with_config_file_entry(config_file_path: &Path) -> Self { + let c8y_configuration_plugin = FileEntry::new( + config_file_path.display().to_string(), + DEFAULT_PLUGIN_CONFIG_TYPE.into(), + ); + Self { + files: HashSet::from([c8y_configuration_plugin]), + } + } - fn add_file(&self, file: String) -> Self { - let mut files = self.files.clone(); - let _ = files.insert(FileEntry::new(file)); - Self { files } + fn add_entries_from_raw_config(mut self, raw_config: RawPluginConfig) -> Self { + let original_plugin_config = self.clone(); + for raw_entry in raw_config.files { + let config_type = raw_entry + .config_type + .unwrap_or_else(|| raw_entry.path.clone()); + let entry = FileEntry::new(raw_entry.path, config_type.clone()); + if !self.files.insert(entry) { + warn!("The config file has the duplicated type '{}'.", config_type); + return original_plugin_config; + } + } + self } pub fn to_supported_config_types_message(&self) -> Result<Message, anyhow::Error> { @@ -75,16 +120,27 @@ impl PluginConfig { Ok(Message::new(&topic, self.to_smartrest_payload())) } - pub fn get_all_file_paths(&self) -> Vec<String> { + pub fn get_all_file_types(&self) -> Vec<String> { self.files .iter() - .map(|x| x.path.to_string()) + .map(|x| x.config_type.to_string()) .collect::<Vec<_>>() } + pub fn get_path_from_type(&self, config_type: &str) -> Result<String, ConfigManagementError> { + let file_entry = self + .files + .get(&config_type.to_string()) + .ok_or(ConfigManagementError::InvalidRequestedConfigType { + config_type: config_type.to_owned(), + })? + .to_owned(); + Ok(file_entry.path) + } + // 119,typeA,typeB,... fn to_smartrest_payload(&self) -> String { - let mut config_types = self.get_all_file_paths(); + let mut config_types = self.get_all_file_types(); // Sort because hashset doesn't guarantee the order let () = config_types.sort(); let supported_config_types = config_types.join(","); @@ -102,71 +158,141 @@ mod tests { const PLUGIN_CONFIG_FILE: &str = "c8y-configuration-plugin.toml"; #[test] - fn deserialize_plugin_config() { - let config: PluginConfig = toml::from_str( + fn deserialize_raw_plugin_config_array_of_tables() { + let config: RawPluginConfig = toml::from_str( r#" [[files]] - path = "/etc/tedge/tedge.toml" + path = "/etc/tedge/tedge.toml" + type = "tedge.toml" [[files]] - path = "/etc/tedge/mosquitto-conf/c8y-bridge.conf" + type = "tedge.toml" + path = "/etc/tedge/tedge.toml" [[files]] - path = "/etc/tedge/mosquitto-conf/tedge-mosquitto.conf" + path = "/etc/tedge/mosquitto-conf/c8y-bridge.conf" [[files]] - path = "/etc/mosquitto/mosquitto.conf" + path = "/etc/tedge/mosquitto-conf/tedge-mosquitto.conf" + type = '"double quotation"' + [[files]] + path = "/etc/mosquitto/mosquitto.conf" + type = "'single quotation'" "#, ) .unwrap(); assert_eq!( config.files, - HashSet::from([ - FileEntry::new("/etc/tedge/tedge.toml".to_string()), - FileEntry::new("/etc/tedge/mosquitto-conf/c8y-bridge.conf".to_string(),), - FileEntry::new("/etc/tedge/mosquitto-conf/tedge-mosquitto.conf".to_string(),), - FileEntry::new("/etc/mosquitto/mosquitto.conf".to_string()) - ]) + vec![ + RawFileEntry::new( + "/etc/tedge/tedge.toml".to_string(), + Some("tedge.toml".to_string()) + ), + RawFileEntry::new( + "/etc/tedge/tedge.toml".to_string(), + Some("tedge.toml".to_string()) + ), + RawFileEntry::new( + "/etc/tedge/mosquitto-conf/c8y-bridge.conf".to_string(), + None + ), + RawFileEntry::new( + "/etc/tedge/mosquitto-conf/tedge-mosquitto.conf".to_string(), + Some("\"double quotation\"".to_string()) + ), + RawFileEntry::new( + "/etc/mosquitto/mosquitto.conf".to_string(), + Some("'single quotation'".to_string()) + ) + ] + ); + } + + #[test] + fn deserialize_raw_plugin_config() { + let config: RawPluginConfig = toml::from_str( + r#" + files = [ + { path = "/etc/tedge/tedge.toml", type = "tedge.toml" }, + { type = "tedge.toml", path = "/etc/tedge/tedge.toml" }, + { path = "/etc/tedge/mosquitto-conf/c8y-bridge.conf" }, + { path = "/etc/tedge/mosquitto-conf/tedge-mosquitto.conf", type = '"double quotation"' }, + { path = "/etc/mosquitto/mosquitto.conf", type = "'single quotation'" }, + ] + "#, + ) + .unwrap(); + + assert_eq!( + config.files, + vec![ + RawFileEntry::new( + "/etc/tedge/tedge.toml".to_string(), + Some("tedge.toml".to_string()) + ), + RawFileEntry::new( + "/etc/tedge/tedge.toml".to_string(), + Some("tedge.toml".to_string()) + ), + RawFileEntry::new( + "/etc/tedge/mosquitto-conf/c8y-bridge.conf".to_string(), + None + ), + RawFileEntry::new( + "/etc/tedge/mosquitto-conf/tedge-mosquitto.conf".to_string(), + Some("\"double quotation\"".to_string()) + ), + RawFileEntry::new( + "/etc/mosquitto/mosquitto.conf".to_string(), + Some("'single quotation'".to_string()) + ) + ] ); } #[test_case( r#" [[files]] - path = "/etc/tedge/tedge.toml" - [[files]] - path = "/etc/tedge/mosquitto-conf/c8y-bridge.conf" + path = "/etc/tedge/tedge.toml" + type = "tedge" [[files]] - path = "/etc/tedge/mosquitto-conf/tedge-mosquitto.conf" - [[files]] - path = "/etc/mosquitto/mosquitto.conf" + path = "/etc/tedge/mosquitto-conf/c8y-bridge.conf" "#, PluginConfig { files: HashSet::from([ - FileEntry::new("/etc/tedge/tedge.toml".to_string()), - FileEntry::new("/etc/tedge/mosquitto-conf/c8y-bridge.conf".to_string(),), - FileEntry::new("/etc/tedge/mosquitto-conf/tedge-mosquitto.conf".to_string(),), - FileEntry::new("/etc/mosquitto/mosquitto.conf".to_string()) + FileEntry::new("/etc/tedge/tedge.toml".to_string(), "tedge".to_string()), + FileEntry::new("/etc/tedge/mosquitto-conf/c8y-bridge.conf".to_string(), "/etc/tedge/mosquitto-conf/c8y-bridge.conf".to_string()), ]) }; "standard case" )] #[test_case( r#" [[files]] - path = "/etc/tedge/tedge.toml" - [[files]] - path = "/etc/tedge/mosquitto-conf/c8y-bridge.conf" + path = "/etc/tedge/tedge.toml" + type = "tedge" [[files]] - path = "/etc/tedge/tedge.toml" - [[files]] - path = "/etc/tedge/mosquitto-conf/c8y-bridge.conf" + path = "/etc/tedge/tedge.toml" + type = "tedge2" "#, PluginConfig { files: HashSet::from([ - FileEntry::new("/etc/tedge/tedge.toml".to_string()), - FileEntry::new("/etc/tedge/mosquitto-conf/c8y-bridge.conf".to_string(),), + FileEntry::new("/etc/tedge/tedge.toml".to_string(), "tedge".to_string()), + FileEntry::new("/etc/tedge/tedge.toml".to_string(), "tedge2".to_string()), ]) }; "file path duplication" )] #[test_case( + r#" + [[files]] + path = "/etc/tedge/tedge.toml" + type = "tedge" + [[files]] + path = "/etc/tedge/tedge2.toml" + type = "tedge" + "#, + PluginConfig { + files: HashSet::new() + }; "file type duplication" + )] + #[test_case( r#"files = []"#, PluginConfig { files: HashSet::new() @@ -183,13 +309,8 @@ mod tests { #[test_case( r#" [[files]] - path = "/etc/tedge/tedge.toml" - [[files]] - path = "/etc/tedge/mosquitto-conf/c8y-bridge.conf" - [[files]] - path = "/etc/tedge/tedge.toml" - [[files]] - path = "/etc/tedge/mosquitto-conf/c8y-bridge.conf" + path = "/etc/tedge/tedge.toml" + type = "tedge" [[unsupported_key]] "#, PluginConfig { @@ -197,22 +318,41 @@ mod tests { } ;"unexpected field" )] - fn read_plugin_config_file(file_content: &str, raw_config: PluginConfig) -> anyhow::Result<()> { + fn read_plugin_config_file( + file_content: &str, + expected_config: PluginConfig, + ) -> anyhow::Result<()> { let (_dir, config_root_path) = create_temp_plugin_config(file_content)?; let tmp_path_to_plugin_config = config_root_path.join(PLUGIN_CONFIG_FILE); let tmp_path_to_plugin_config_str = tmp_path_to_plugin_config.as_path().display().to_string(); let config = PluginConfig::new(tmp_path_to_plugin_config.clone()); - - // The expected output should contain /tmp/<random>/c8y_configuration_plugin.toml - let expected_config = raw_config.add_file(tmp_path_to_plugin_config_str); + let expected_config = expected_config.add_file_entry( + tmp_path_to_plugin_config_str, + DEFAULT_PLUGIN_CONFIG_TYPE.into(), + ); assert_eq!(config, expected_config); Ok(()) } + impl RawFileEntry { + pub fn new(path: String, config_type: Option<String>) -> Self { + Self { path, config_type } + } + } + + // Use this to add a temporary file path of the plugin configuration file + impl PluginConfig { + fn add_file_entry(&self, path: String, config_type: String) -> Self { + let mut files = self.files.clone(); + let _ = files.insert(FileEntry::new(path, config_type)); + Self { files } + } + } + // Need to return TempDir, otherwise the dir will be deleted when this function ends. fn create_temp_plugin_config(content: &str) -> std::io::Result<(TempDir, PathBuf)> { let temp_dir = TempDir::new()?; @@ -224,29 +364,12 @@ mod tests { } #[test] - fn add_file_to_plugin_config() { - let config = PluginConfig::default().add_file("/test/path/file".into()); - assert_eq!( - config.files, - HashSet::from([FileEntry::new("/test/path/file".to_string())]) - ) - } - - #[test] - fn add_file_to_plugin_config_with_duplication() { - let config = PluginConfig::default() - .add_file("/test/path/file".into()) - .add_file("/test/path/file".into()); - assert_eq!( - config.files, - HashSet::from([FileEntry::new("/test/path/file".to_string())]) - ) - } - - #[test] fn get_smartrest_single_type() { let plugin_config = PluginConfig { - files: HashSet::from([FileEntry::new("typeA".to_string())]), + files: HashSet::from([FileEntry::new( + "/path/to/file".to_string(), + "typeA".to_string(), + )]), }; let output = plugin_config.to_smartrest_payload(); assert_eq!(output, "119,typeA"); @@ -256,13 +379,12 @@ mod tests { fn get_smartrest_multiple_types() { let plugin_config = PluginConfig { files: HashSet::from([ - FileEntry::new("typeA".to_string()), - FileEntry::new("typeB".to_string()), - FileEntry::new("typeC".to_string()), + FileEntry::new("path1".to_string(), "typeA".to_string()), + FileEntry::new("path2".to_string(), "typeB".to_string()), + FileEntry::new("path3".to_string(), "typeC".to_string()), ]), }; let output = plugin_config.to_smartrest_payload(); assert_eq!(output, ("119,typeA,typeB,typeC")); - dbg!(output); } } diff --git a/plugins/c8y_configuration_plugin/src/download.rs b/plugins/c8y_configuration_plugin/src/download.rs index 9d39989c..9565cf26 100644 --- a/plugins/c8y_configuration_plugin/src/download.rs +++ b/plugins/c8y_configuration_plugin/src/download.rs @@ -15,6 +15,7 @@ use serde_json::json; use std::fs; use std::os::unix::fs::PermissionsExt; use std::path::{Path, PathBuf}; +use tracing::info; const CONFIG_CHANGE_TOPIC: &str = "tedge/configuration_change"; @@ -28,19 +29,22 @@ pub async fn handle_config_download_request( let executing_message = DownloadConfigFileStatusMessage::executing()?; let () = mqtt_client.published.send(executing_message).await?; - // Add validation if the config_type exists in - let changed_file = smartrest_request.config_type.clone(); + let changed_config_type = smartrest_request.config_type.clone(); match download_config_file(plugin_config, smartrest_request, tmp_dir, http_client).await { Ok(_) => { + info!("The configuration download for '{changed_config_type}' is successful."); + let successful_message = DownloadConfigFileStatusMessage::successful(None)?; let () = mqtt_client.published.send(successful_message).await?; - let notification_message = get_file_change_notification_message(changed_file); + let notification_message = get_file_change_notification_message(changed_config_type); let () = mqtt_client.published.send(notification_message).await?; Ok(()) } Err(err) => { + error!("The configuration download for '{changed_config_type}' is failed.",); + let failed_message = DownloadConfigFileStatusMessage::failed(err.to_string())?; let () = mqtt_client.published.send(failed_message).await?; Err(err) @@ -93,15 +97,8 @@ impl ConfigDownloadRequest { plugin_config: &PluginConfig, tmp_dir: PathBuf, ) -> Result<Self, ConfigManagementError> { - // Check if the requested config type is in the plugin config list - let all_file_paths = plugin_config.get_all_file_paths(); - if !all_file_paths.contains(&request.config_type) { - return Err(ConfigManagementError::InvalidRequestedConfigType { - path: request.config_type, - }); - } - - let destination_path = PathBuf::from(request.config_type); + let destination_path_string = plugin_config.get_path_from_type(&request.config_type)?; + let destination_path = PathBuf::from(destination_path_string); let file_name = Self::get_filename(destination_path.clone())?; Ok(Self { @@ -219,14 +216,15 @@ mod tests { #[test] fn create_config_download_request() -> Result<(), anyhow::Error> { - let payload = "524,rina0005,https://test.cumulocity.com/inventory/binaries/70208,/etc/tedge/tedge.toml"; + let payload = "524,rina0005,https://test.cumulocity.com/inventory/binaries/70208,tedge"; let smartrest_request = SmartRestConfigDownloadRequest::from_smartrest(payload)?; let plugin_config = PluginConfig { files: HashSet::from([ - FileEntry::new("/etc/tedge/tedge.toml".to_string()), - FileEntry::new("/etc/tedge/mosquitto-conf/c8y-bridge.conf".to_string()), - FileEntry::new("/etc/tedge/mosquitto-conf/tedge-mosquitto.conf".to_string()), - FileEntry::new("/etc/mosquitto/mosquitto.conf".to_string()), + FileEntry::new("/etc/tedge/tedge.toml".to_string(), "tedge".to_string()), + FileEntry::new( + "/etc/tedge/mosquitto-conf/c8y-bridge.conf".to_string(), + "c8y-bridge".to_string(), + ), ]), }; let config_download_request = ConfigDownloadRequest::try_new( @@ -251,15 +249,14 @@ mod tests { #[test] fn requested_config_does_not_match_config_plugin() -> Result<(), anyhow::Error> { - let payload = "524,rina0005,https://test.cumulocity.com/inventory/binaries/70208,/etc/tedge/not_in_config.toml"; + let payload = + "524,rina0005,https://test.cumulocity.com/inventory/binaries/70208,not_in_config.toml"; let smartrest_request = SmartRestConfigDownloadRequest::from_smartrest(payload)?; let plugin_config = PluginConfig { - files: HashSet::from([ - FileEntry::new("/etc/tedge/tedge.toml".to_string()), - FileEntry::new("/etc/tedge/mosquitto-conf/c8y-bridge.conf".to_string()), - FileEntry::new("/etc/tedge/mosquitto-conf/tedge-mosquitto.conf".to_string()), - FileEntry::new("/etc/mosquitto/mosquitto.conf".to_string()), - ]), + files: HashSet::from([FileEntry::new( + "/etc/tedge/tedge.toml".to_string(), + "tedge".to_string(), + )]), }; let config_download_request = ConfigDownloadRequest::try_new( smartrest_request, diff --git a/plugins/c8y_configuration_plugin/src/error.rs b/plugins/c8y_configuration_plugin/src/error.rs index 3c6a91ff..efcb0913 100644 --- a/plugins/c8y_configuration_plugin/src/error.rs +++ b/plugins/c8y_configuration_plugin/src/error.rs @@ -17,8 +17,10 @@ pub enum ConfigManagementError { #[error("Failed to copy a file from {src:?} to {dest:?}")] FileCopyFailed { src: PathBuf, dest: PathBuf }, - #[error("The requested config_type {path} doesn't match the plugin config.")] - InvalidRequestedConfigType { path: String }, + #[error( + "The requested config_type {config_type} is not defined in the plugin configuration file." + )] + InvalidRequestedConfigType { config_type: String }, #[error(transparent)] FromTEdgeConfig(#[from] tedge_config::TEdgeConfigError), diff --git a/plugins/c8y_configuration_plugin/src/main.rs b/plugins/c8y_configuration_plugin/src/main.rs index edf8bd9a..7bf1c40c 100644 --- a/plugins/c8y_configuration_plugin/src/main.rs +++ b/plugins/c8y_configuration_plugin/src/main.rs @@ -133,6 +133,7 @@ async fn main() -> Result<(), anyhow::Error> { // handle the config file upload request handle_config_upload_request( + &plugin_config, config_upload_request, &mut mqtt_client, &mut http_client, @@ -146,7 +147,7 @@ async fn main() -> Result<(), anyhow::Error> { }; if let Err(err) = result { - error!("Handling of operation: '{}' failed with {}", payload, err); + error!("Handling of operation: '{payload}' failed with {err}"); } } } diff --git a/plugins/c8y_configuration_plugin/src/upload.rs b/plugins/c8y_configuration_plugin/src/upload.rs index f316c256..5717dd6a 100644 --- a/plugins/c8y_configuration_plugin/src/upload.rs +++ b/plugins/c8y_configuration_plugin/src/upload.rs @@ -1,3 +1,4 @@ +use crate::PluginConfig; use anyhow::Result; use c8y_api::http_proxy::{C8YHttpProxy, JwtAuthHttpProxy}; use c8y_smartrest::error::SmartRestSerializerError; @@ -42,6 +43,7 @@ impl TryIntoOperationStatusMessage for UploadConfigFileStatusMessage { } pub async fn handle_config_upload_request( + plugin_config: &PluginConfig, config_upload_request: SmartRestConfigUploadRequest, mqtt_client: &mut Connection, http_client: &mut JwtAuthHttpProxy, @@ -50,11 +52,14 @@ pub async fn handle_config_upload_request( let msg = UploadConfigFileStatusMessage::executing()?; let () = mqtt_client.published.send(msg).await?; + let config_file_path = plugin_config.get_path_from_type(&config_upload_request.config_type)?; let upload_result = upload_config_file( - Path::new(config_upload_request.config_type.as_str()), + Path::new(config_file_path.as_str()), + &config_upload_request.config_type, http_client, ) .await; + match upload_result { Ok(upload_event_url) => { let successful_message = @@ -72,6 +77,7 @@ pub async fn handle_config_upload_request( async fn upload_config_file( config_file_path: &Path, + config_type: &str, http_client: &mut JwtAuthHttpProxy, ) -> Result<String> { // read the config file contents @@ -79,7 +85,7 @@ async fn upload_config_file( // upload config file let upload_event_url = http_client - .upload_config_file(config_file_path, &config_content) + .upload_config_file(config_type, &config_content) .await?; Ok(upload_event_url) |