summaryrefslogtreecommitdiffstats
path: root/crates
diff options
context:
space:
mode:
authorLukasz Woznicki <lukasz.woznicki@softwareag.com>2022-05-13 12:56:31 +0100
committerLukasz Woznicki <lukasz.woznicki@softwareag.com>2022-05-13 13:44:46 +0100
commit22e874ef463b94c7bc8d0fa892d0ce3a55cbc3c7 (patch)
tree56e69be887da542231c0ddd4e0a152eaed4203a6 /crates
parent73c38bc07f3e0cef2c230c0a47f73b9e36e70d72 (diff)
Add smartrest templates sets to possible tedge config options
Signed-off-by: Lukasz Woznicki <lukasz.woznicki@softwareag.com>
Diffstat (limited to 'crates')
-rw-r--r--crates/common/tedge_config/src/models/mod.rs4
-rw-r--r--crates/common/tedge_config/src/models/templates_set.rs101
-rw-r--r--crates/common/tedge_config/src/settings.rs19
-rw-r--r--crates/common/tedge_config/src/tedge_config.rs26
-rw-r--r--crates/common/tedge_config/src/tedge_config_dto.rs3
5 files changed, 152 insertions, 1 deletions
diff --git a/crates/common/tedge_config/src/models/mod.rs b/crates/common/tedge_config/src/models/mod.rs
index 4044168d..37871da7 100644
--- a/crates/common/tedge_config/src/models/mod.rs
+++ b/crates/common/tedge_config/src/models/mod.rs
@@ -3,4 +3,6 @@ pub mod file_path;
pub mod flag;
pub mod ipaddress;
pub mod port;
-pub use self::{connect_url::*, file_path::*, flag::*, ipaddress::*, port::*};
+pub mod templates_set;
+
+pub use self::{connect_url::*, file_path::*, flag::*, ipaddress::*, port::*, templates_set::*};
diff --git a/crates/common/tedge_config/src/models/templates_set.rs b/crates/common/tedge_config/src/models/templates_set.rs
new file mode 100644
index 00000000..db47fc8f
--- /dev/null
+++ b/crates/common/tedge_config/src/models/templates_set.rs
@@ -0,0 +1,101 @@
+use std::convert::TryInto;
+
+/// Represents a set of smartrest templates.
+///
+/// New type to add conversion methods and deduplicate provided templates.
+#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, Eq, PartialEq)]
+#[serde(transparent)]
+pub struct TemplatesSet(pub Vec<String>);
+
+#[derive(thiserror::Error, Debug)]
+#[error("FilePath to String conversion failed: {0:?}")]
+pub struct TemplatesSetToStringConversionFailure(String);
+
+impl TryFrom<Vec<String>> for TemplatesSet {
+ type Error = TemplatesSetToStringConversionFailure;
+
+ fn try_from(value: Vec<String>) -> Result<Self, Self::Error> {
+ let set = value
+ .iter()
+ .flat_map(|s| {
+ // Smartrest templates should be deserialized as:
+ // c8y/s/uc/template-1 (in from localhost), s/uc/template-1
+ // c8y/s/dc/template-1 (out to localhost), s/dc/template-1
+ [
+ format!(r#"c8y/s/uc/{s} out 2 c8y/ """#),
+ format!(r#"c8y/s/dc/{s} in 2 c8y/ """#),
+ ]
+ .into_iter()
+ })
+ .collect::<Vec<String>>();
+ Ok(TemplatesSet(set))
+ }
+}
+
+impl TryFrom<Vec<&str>> for TemplatesSet {
+ type Error = TemplatesSetToStringConversionFailure;
+
+ fn try_from(value: Vec<&str>) -> Result<Self, Self::Error> {
+ let set = value
+ .iter()
+ .flat_map(|s| {
+ // Smartrest templates should be deserialized as:
+ // c8y/s/uc/template-1 (in from localhost), s/uc/template-1
+ // c8y/s/dc/template-1 (out to localhost), s/dc/template-1
+ [
+ format!(r#"c8y/s/uc/{s} out 2 c8y/ """#),
+ format!(r#"c8y/s/dc/{s} in 2 c8y/ """#),
+ ]
+ .into_iter()
+ })
+ .collect::<Vec<String>>();
+ Ok(TemplatesSet(set))
+ }
+}
+
+impl TryInto<Vec<String>> for TemplatesSet {
+ type Error = TemplatesSetToStringConversionFailure;
+
+ fn try_into(self) -> Result<Vec<String>, TemplatesSetToStringConversionFailure> {
+ Ok(self.0)
+ }
+}
+
+impl From<TemplatesSet> for String {
+ fn from(val: TemplatesSet) -> Self {
+ val.to_string()
+ }
+}
+
+impl From<String> for TemplatesSet {
+ fn from(val: String) -> Self {
+ let strings = val.split(',').map(|ss| ss.into()).collect();
+ TemplatesSet(strings)
+ }
+}
+
+impl std::fmt::Display for TemplatesSet {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ write!(f, "{:?}", self.0)
+ }
+}
+
+#[cfg(test)]
+mod test {
+ use super::TemplatesSet;
+
+ #[test]
+ fn conversion_from_strings() {
+ let strings = vec!["template-1", "template-2"];
+ let expected = vec![
+ r#"c8y/s/uc/template-1 out 2 c8y/ """#,
+ r#"c8y/s/dc/template-1 in 2 c8y/ """#,
+ r#"c8y/s/uc/template-2 out 2 c8y/ """#,
+ r#"c8y/s/dc/template-2 in 2 c8y/ """#,
+ ];
+
+ let res = TemplatesSet::try_from(strings).unwrap();
+
+ assert_eq!(res.0, expected);
+ }
+}
diff --git a/crates/common/tedge_config/src/settings.rs b/crates/common/tedge_config/src/settings.rs
index fb018408..bca85b1a 100644
--- a/crates/common/tedge_config/src/settings.rs
+++ b/crates/common/tedge_config/src/settings.rs
@@ -102,6 +102,25 @@ impl ConfigSetting for C8yRootCertPathSetting {
}
///
+/// Smartrest templates to subsribe to.
+///
+/// Example: template1,template2
+///
+#[derive(Debug)]
+pub struct C8ySmartRestTemplates;
+
+impl ConfigSetting for C8ySmartRestTemplates {
+ const KEY: &'static str = "c8y.smartrest.templates";
+
+ const DESCRIPTION: &'static str = concat!(
+ "Set of SmartRest templates for the device ",
+ "Example: template1,template2"
+ );
+
+ type Value = TemplatesSet;
+}
+
+///
/// Tenant endpoint URL of Azure IoT tenant.
///
/// Example: MyAzure.azure-devices.net
diff --git a/crates/common/tedge_config/src/tedge_config.rs b/crates/common/tedge_config/src/tedge_config.rs
index f37ea0e3..9952b2d1 100644
--- a/crates/common/tedge_config/src/tedge_config.rs
+++ b/crates/common/tedge_config/src/tedge_config.rs
@@ -138,6 +138,32 @@ impl ConfigSettingAccessor<C8yUrlSetting> for TEdgeConfig {
}
}
+impl ConfigSettingAccessor<C8ySmartRestTemplates> for TEdgeConfig {
+ fn query(&self, _setting: C8ySmartRestTemplates) -> ConfigSettingResult<TemplatesSet> {
+ self.data
+ .c8y
+ .smartrest_templates
+ .clone()
+ .ok_or(ConfigSettingError::ConfigNotSet {
+ key: C8ySmartRestTemplates::KEY,
+ })
+ }
+
+ fn update(
+ &mut self,
+ _setting: C8ySmartRestTemplates,
+ value: TemplatesSet,
+ ) -> ConfigSettingResult<()> {
+ self.data.c8y.smartrest_templates = Some(value);
+ Ok(())
+ }
+
+ fn unset(&mut self, _setting: C8ySmartRestTemplates) -> ConfigSettingResult<()> {
+ self.data.c8y.smartrest_templates = None;
+ Ok(())
+ }
+}
+
impl ConfigSettingAccessor<DeviceCertPathSetting> for TEdgeConfig {
fn query(&self, _setting: DeviceCertPathSetting) -> ConfigSettingResult<FilePath> {
Ok(self
diff --git a/crates/common/tedge_config/src/tedge_config_dto.rs b/crates/common/tedge_config/src/tedge_config_dto.rs
index 28cbfcbd..75b576b6 100644
--- a/crates/common/tedge_config/src/tedge_config_dto.rs
+++ b/crates/common/tedge_config/src/tedge_config_dto.rs
@@ -72,6 +72,9 @@ pub(crate) struct CumulocityConfigDto {
/// Boolean whether Azure mapper adds timestamp or not.
pub(crate) mapper_timestamp: Option<bool>,
+
+ /// Set of c8y templates used for subscriptions.
+ pub(crate) smartrest_templates: Option<TemplatesSet>,
}
#[derive(Debug, Default, Deserialize, Serialize)]