Skip to content

Commit 89777a8

Browse files
authored
Merge pull request #3864 from albinsuresh/refactor/typed-device-topic-id-in-tedge-config
refactor: typed device topic id in tedge config
2 parents f5297bb + 4691213 commit 89777a8

File tree

13 files changed

+24
-56
lines changed

13 files changed

+24
-56
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/common/tedge_config/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ rustls = { workspace = true }
3030
serde = { workspace = true, features = ["rc"] }
3131
strum = { workspace = true }
3232
strum_macros = { workspace = true }
33+
tedge_api = { workspace = true }
3334
tedge_config_macros = { workspace = true }
3435
tedge_utils = { workspace = true, features = ["timestamp"] }
3536
thiserror = { workspace = true }

crates/common/tedge_config/src/tedge_toml/tedge_config.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ use std::num::NonZeroU16;
4646
use std::path::PathBuf;
4747
use std::sync::Arc;
4848
use std::sync::OnceLock;
49+
use tedge_api::mqtt_topics::EntityTopicId;
4950
pub use tedge_config_macros::ConfigNotSet;
5051
pub use tedge_config_macros::MultiError;
5152
pub use tedge_config_macros::ProfileName;
@@ -673,10 +674,11 @@ define_tedge_config! {
673674
topic_root: String,
674675

675676
/// The device MQTT topic identifier
676-
#[tedge_config(default(value = "device/main//"))]
677+
#[tedge_config(default(function = "EntityTopicId::default_main_device"))]
677678
#[tedge_config(example = "device/main//")]
678679
#[tedge_config(example = "device/child_001//")]
679-
device_topic_id: String,
680+
#[doku(as = "String")]
681+
device_topic_id: EntityTopicId,
680682

681683
bind: {
682684
/// The address mosquitto binds to for internal use

crates/common/tedge_config/src/tedge_toml/tedge_config/append_remove.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ impl_append_remove_for_single_value!(
3838
AbsolutePath,
3939
String,
4040
ConnectUrl,
41+
EntityTopicId,
4142
HostPort<HTTPS_PORT>,
4243
HostPort<MQTT_TLS_PORT>,
4344
HostPort<MQTT_SVC_TLS_PORT>,

crates/core/tedge/src/cli/connect/command.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ use std::hash::Hash;
3737
use std::net::ToSocketAddrs;
3838
use std::sync::Arc;
3939
use std::time::Duration;
40-
use tedge_api::mqtt_topics::EntityTopicId;
4140
use tedge_api::mqtt_topics::MqttSchema;
4241
use tedge_api::mqtt_topics::TopicIdError;
4342
use tedge_api::service_health_topic;
@@ -783,7 +782,7 @@ pub(crate) fn bridge_health_topic(
783782
};
784783

785784
let mqtt_schema = MqttSchema::with_root(tedge_config.mqtt.topic_root.clone());
786-
let device_topic_id = tedge_config.mqtt.device_topic_id.parse::<EntityTopicId>()?;
785+
let device_topic_id = tedge_config.mqtt.device_topic_id.clone();
787786
Ok(service_health_topic(
788787
&mqtt_schema,
789788
&device_topic_id,

crates/core/tedge_agent/src/agent.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,12 @@ impl AgentConfig {
120120
.mqtt_topic_root
121121
.unwrap_or(tedge_config.mqtt.topic_root.clone().into());
122122

123-
let mqtt_device_topic_id: EntityTopicId = cliopts
124-
.mqtt_device_topic_id
125-
.unwrap_or(tedge_config.mqtt.device_topic_id.clone().into())
126-
.parse()
127-
.context("Could not parse the device MQTT topic")?;
123+
let mqtt_device_topic_id: EntityTopicId = match cliopts.mqtt_device_topic_id {
124+
Some(topic_id) => topic_id
125+
.parse()
126+
.context("Could not parse the device MQTT topic")?,
127+
None => tedge_config.mqtt.device_topic_id.clone(),
128+
};
128129
let service_topic_id = mqtt_device_topic_id.to_default_service_topic_id("tedge-agent")
129130
.with_context(|| format!("Device topic id {} currently needs default scheme, e.g: 'device/DEVICE_NAME//'", mqtt_device_topic_id))?;
130131

crates/core/tedge_agent/src/software_manager/config.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,7 @@ impl SoftwareManagerConfig {
2929
.or_none()
3030
.cloned();
3131

32-
let device = tedge_config
33-
.mqtt
34-
.device_topic_id
35-
.parse()
36-
.unwrap_or(EntityTopicId::default_main_device());
32+
let device = tedge_config.mqtt.device_topic_id.clone();
3733

3834
Ok(SoftwareManagerConfig {
3935
device,

crates/core/tedge_mapper/src/aws/mapper.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,10 @@ use async_trait::async_trait;
66
use aws_mapper_ext::converter::AwsConverter;
77
use clock::WallClock;
88
use mqtt_channel::TopicFilter;
9-
use std::str::FromStr;
109
use tedge_actors::ConvertingActor;
1110
use tedge_actors::MessageSink;
1211
use tedge_actors::MessageSource;
1312
use tedge_actors::NoConfig;
14-
use tedge_api::mqtt_topics::EntityTopicId;
1513
use tedge_api::mqtt_topics::MqttSchema;
1614
use tedge_api::service_health_topic;
1715
use tedge_config::models::TopicPrefix;
@@ -44,7 +42,7 @@ impl TEdgeComponent for AwsMapper {
4442
let mqtt_schema = MqttSchema::with_root(tedge_config.mqtt.topic_root.clone());
4543
if tedge_config.mqtt.bridge.built_in {
4644
let device_id = aws_config.device.id()?;
47-
let device_topic_id = EntityTopicId::from_str(&tedge_config.mqtt.device_topic_id)?;
45+
let device_topic_id = tedge_config.mqtt.device_topic_id.clone();
4846

4947
let rules = built_in_bridge_rules(device_id, prefix)?;
5048

crates/core/tedge_mapper/src/az/mapper.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,10 @@ use az_mapper_ext::converter::AzureConverter;
77
use clock::WallClock;
88
use mqtt_channel::TopicFilter;
99
use std::borrow::Cow;
10-
use std::str::FromStr;
1110
use tedge_actors::ConvertingActor;
1211
use tedge_actors::MessageSink;
1312
use tedge_actors::MessageSource;
1413
use tedge_actors::NoConfig;
15-
use tedge_api::mqtt_topics::EntityTopicId;
1614
use tedge_api::mqtt_topics::MqttSchema;
1715
use tedge_api::service_health_topic;
1816
use tedge_config::models::TopicPrefix;
@@ -44,7 +42,7 @@ impl TEdgeComponent for AzureMapper {
4442
let mqtt_schema = MqttSchema::with_root(tedge_config.mqtt.topic_root.clone());
4543

4644
if tedge_config.mqtt.bridge.built_in {
47-
let device_topic_id = EntityTopicId::from_str(&tedge_config.mqtt.device_topic_id)?;
45+
let device_topic_id = tedge_config.mqtt.device_topic_id.clone();
4846

4947
let remote_clientid = az_config.device.id()?;
5048
let rules = built_in_bridge_rules(remote_clientid, prefix)?;

crates/core/tedge_mapper/src/c8y/mapper.rs

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -188,12 +188,7 @@ pub fn service_monitor_client_config(
188188

189189
// there is one mapper instance per cloud per thin-edge instance, perhaps we should use some
190190
// predefined topic id instead of trying to derive it from current device?
191-
let entity_topic_id: EntityTopicId = tedge_config
192-
.mqtt
193-
.device_topic_id
194-
.clone()
195-
.parse()
196-
.context("Invalid device_topic_id")?;
191+
let entity_topic_id: EntityTopicId = tedge_config.mqtt.device_topic_id.clone();
197192
let prefix = &c8y_config.bridge.topic_prefix;
198193

199194
let mapper_service_topic_id = entity_topic_id
@@ -356,12 +351,7 @@ fn core_mqtt_bridge_config(
356351

357352
// there is one mapper instance per cloud per thin-edge instance, perhaps we should use some
358353
// predefined topic id instead of trying to derive it from current device?
359-
let entity_topic_id: EntityTopicId = tedge_config
360-
.mqtt
361-
.device_topic_id
362-
.clone()
363-
.parse()
364-
.context("Invalid device_topic_id")?;
354+
let entity_topic_id: EntityTopicId = tedge_config.mqtt.device_topic_id.clone();
365355

366356
let mapper_service_topic_id = entity_topic_id
367357
.default_service_for_device(c8y_mapper_name)
@@ -464,12 +454,7 @@ fn mqtt_service_bridge_config(
464454
"tedge-mapper-bridge-{}",
465455
c8y_config.mqtt_service.topic_prefix.as_str()
466456
);
467-
let device_topic_id: EntityTopicId = tedge_config
468-
.mqtt
469-
.device_topic_id
470-
.clone()
471-
.parse()
472-
.context("Invalid device_topic_id")?;
457+
let device_topic_id: EntityTopicId = tedge_config.mqtt.device_topic_id.clone();
473458

474459
let bridge_health_topic =
475460
service_health_topic(&mqtt_schema, &device_topic_id, &bridge_service_name);

0 commit comments

Comments
 (0)