diff options
author | Albin Suresh <albin.suresh@softwareag.com> | 2022-04-12 17:36:53 +0530 |
---|---|---|
committer | Albin Suresh <albin.suresh@softwareag.com> | 2022-04-12 18:02:39 +0530 |
commit | 19aea1a260196593c82ac84954b7f844a2fd209b (patch) | |
tree | 79f85e4414a840605ef706fcad52263bace2cb49 /crates | |
parent | 6ff7da30dc2d1979b237ac9b014f741b65bfef88 (diff) |
Issue #1030 Support c8y_UploadConfigFile operation
Diffstat (limited to 'crates')
-rw-r--r-- | crates/core/c8y_api/src/http_proxy.rs | 54 | ||||
-rw-r--r-- | crates/core/c8y_smartrest/src/smartrest_deserializer.rs | 22 | ||||
-rw-r--r-- | crates/core/c8y_smartrest/src/smartrest_serializer.rs | 2 | ||||
-rw-r--r-- | crates/core/tedge_mapper/src/c8y/tests.rs | 10 |
4 files changed, 86 insertions, 2 deletions
diff --git a/crates/core/c8y_api/src/http_proxy.rs b/crates/core/c8y_api/src/http_proxy.rs index 9e75faf3..53fe8611 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, time::Duration}; +use std::{collections::HashMap, path::Path, time::Duration}; use tedge_config::{ C8yUrlSetting, ConfigSettingAccessor, ConfigSettingAccessorStringExt, DeviceIdSetting, MqttBindAddressSetting, MqttPortSetting, TEdgeConfig, @@ -43,6 +43,12 @@ pub trait C8YHttpProxy: Send + Sync { &mut self, log_content: &str, ) -> Result<String, SMCumulocityMapperError>; + + async fn upload_config_file( + &mut self, + config_path: &Path, + config_content: &str, + ) -> Result<String, SMCumulocityMapperError>; } /// Define a C8y endpoint @@ -266,6 +272,25 @@ impl JwtAuthHttpProxy { ) } + fn create_event( + &self, + event_type: String, + event_text: Option<String>, + event_time: Option<OffsetDateTime>, + ) -> C8yCreateEvent { + let c8y_managed_object = C8yManagedObject { + id: self.end_point.c8y_internal_id.clone(), + }; + + C8yCreateEvent::new( + Some(c8y_managed_object), + event_type.clone(), + event_time.map_or(OffsetDateTime::now_utc(), |time| time), + event_text.map_or(event_type, |text| text), + HashMap::new(), + ) + } + async fn send_event_internal( &mut self, c8y_event: C8yCreateEvent, @@ -375,6 +400,33 @@ impl C8YHttpProxy for JwtAuthHttpProxy { let _response = self.http_con.execute(request).await?; Ok(binary_upload_event_url) } + + async fn upload_config_file( + &mut self, + config_path: &Path, + 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 event_response_id = self.send_event_internal(config_file_event).await?; + let binary_upload_event_url = self + .end_point + .get_url_for_event_binary_upload(&event_response_id); + + let request = self + .http_con + .post(&binary_upload_event_url) + .header("Accept", "application/json") + .header("Content-Type", "text/plain") + .body(config_content.to_string()) + .bearer_auth(token.token()) + .timeout(Duration::from_millis(10000)) + .build()?; + + let _response = self.http_con.execute(request).await?; + Ok(binary_upload_event_url) + } } #[cfg(test)] diff --git a/crates/core/c8y_smartrest/src/smartrest_deserializer.rs b/crates/core/c8y_smartrest/src/smartrest_deserializer.rs index 244d77bc..c65f49b2 100644 --- a/crates/core/c8y_smartrest/src/smartrest_deserializer.rs +++ b/crates/core/c8y_smartrest/src/smartrest_deserializer.rs @@ -242,6 +242,28 @@ impl SmartRestRestartRequest { } } +#[derive(Debug, Deserialize, Serialize, PartialEq)] +pub struct SmartRestConfigUploadRequest { + pub message_id: String, + pub device: String, + pub config_type: String, +} + +impl SmartRestConfigUploadRequest { + pub fn from_smartrest(smartrest: &str) -> Result<Self, SmartRestDeserializerError> { + let mut rdr = ReaderBuilder::new() + .has_headers(false) + .flexible(true) + .from_reader(smartrest.as_bytes()); + + rdr.deserialize() + .next() + .ok_or_else(|| panic!("empty request")) + .unwrap() // does already panic before this, so this unwrap is only required for type lineup + .map_err(SmartRestDeserializerError::from) + } +} + type JwtToken = String; #[derive(Debug, Deserialize, PartialEq)] diff --git a/crates/core/c8y_smartrest/src/smartrest_serializer.rs b/crates/core/c8y_smartrest/src/smartrest_serializer.rs index 7f548d74..b3ec3e44 100644 --- a/crates/core/c8y_smartrest/src/smartrest_serializer.rs +++ b/crates/core/c8y_smartrest/src/smartrest_serializer.rs @@ -10,6 +10,7 @@ pub enum CumulocitySupportedOperations { C8ySoftwareUpdate, C8yLogFileRequest, C8yRestartRequest, + C8yUploadConfigFile, } impl From<CumulocitySupportedOperations> for &'static str { @@ -18,6 +19,7 @@ impl From<CumulocitySupportedOperations> for &'static str { CumulocitySupportedOperations::C8ySoftwareUpdate => "c8y_SoftwareUpdate", CumulocitySupportedOperations::C8yLogFileRequest => "c8y_LogfileRequest", CumulocitySupportedOperations::C8yRestartRequest => "c8y_Restart", + CumulocitySupportedOperations::C8yUploadConfigFile => "c8y_UploadConfigFile", } } } diff --git a/crates/core/tedge_mapper/src/c8y/tests.rs b/crates/core/tedge_mapper/src/c8y/tests.rs index bafe2b84..849c7563 100644 --- a/crates/core/tedge_mapper/src/c8y/tests.rs +++ b/crates/core/tedge_mapper/src/c8y/tests.rs @@ -18,7 +18,7 @@ use mqtt_channel::{Message, Topic}; use mqtt_tests::test_mqtt_server::MqttProcessHandler; use serde_json::json; use serial_test::serial; -use std::time::Duration; +use std::{path::Path, time::Duration}; use test_case::test_case; use tokio::task::JoinHandle; @@ -828,6 +828,14 @@ impl C8YHttpProxy for FakeC8YHttpProxy { ) -> Result<String, SMCumulocityMapperError> { Ok("123".into()) } + + async fn upload_config_file( + &mut self, + _config_path: &Path, + _config_content: &str, + ) -> Result<String, SMCumulocityMapperError> { + Ok("fake/upload/url".into()) + } } async fn start_c8y_mapper(mqtt_port: u16) -> Result<JoinHandle<()>, anyhow::Error> { |