feat: support user topics when handling custom operation#3189
feat: support user topics when handling custom operation#3189Ruadhri17 merged 3 commits intothin-edge:mainfrom
Conversation
Robot Results
|
8ad970a to
1fcdbbd
Compare
Codecov ReportAttention: Patch coverage is Additional details and impacted files📢 Thoughts on this report? Let us know! |
1fcdbbd to
e902e6e
Compare
e902e6e to
fadb1d1
Compare
fadb1d1 to
27bc06d
Compare
121f19c to
29a8439
Compare
There was a problem hiding this comment.
The added test step is doing what it was intended for
gligorisaev
left a comment
There was a problem hiding this comment.
Checked the whole test, The added test step is doing what it was intended for
| @@ -0,0 +1,4 @@ | |||
| [exec] | |||
| topic = "${.topic.root_prefix}/custom/topic/one" | |||
There was a problem hiding this comment.
What does ${.topic.root_prefix} refer to?
It would be more common to be able to expand the mapper prefix (e.g. c8y or whether the cloud prefix etc.), and to avoid confusion with the topic.root_topic value (which is currently the local thin-edge.io topic prefix, e.g. te).
There was a problem hiding this comment.
Sorry I just read the other code, and could answer the questions.
We should rename the variable as follows:
${.topic.root_prefix} => ${.bridge.topic_prefix}
The .bridge.topic_prefix then aligns with the tedge.toml value c8y.bridge.topic_prefix, where the leading c8y. prefix is not required as the mapper already knows what context it is (as the mapper is started with tedge-mapper c8y)
There was a problem hiding this comment.
${.bridge.topic_prefix} sounds really ad-hoc and not extensible in the future.
I would be explicit with ${.config.c8y.bridge.topic_prefix} and more general with the ability to inject any config setting. Surely more useful for the script rather than the topic on which commands are expected, I agree. However, making the context explicit avoid consistency and extensibility issues.
Injecting any config value might be a bit too involved for this PR. Hence, it's okay to start with a simple string replacement (as currently implemented) but with an explicit config context (i.e. ${.config.c8y.bridge.topic_prefix}).
There was a problem hiding this comment.
Though the ${.bridge.topic_prefix} value is actually the prefix is used by the cloud "profile" used by the mapper. And since the custom operation templates are under the context of the mapper's cloud already, the value does not need to "c8y" context helper.
There was a problem hiding this comment.
the value does not need to "c8y" context helper.
Yes. My concern is that this looks like as a path templating system while it is not, just variable substitution using ${.bridge.topic_prefix} as a variable name.
There was a problem hiding this comment.
Though doesn't the the property name topic suggest otherwise?
There was a problem hiding this comment.
@didier-wenzek Sorry I misunderstood your point. I've opened a new discussion about the code in question. https://github.com/thin-edge/thin-edge.io/pull/3189/files#r1813004355
There was a problem hiding this comment.
@reubenmiller And I misunderstood yours ;-)
After an offline discussion, I agree that it makes sense to use the bridge config and not the tedge config as root of the substitution in ${.bridge.topic_prefix}.
For the implementation, what is done is working and can be taken as fist step - even if not extensible.
rina23q
left a comment
There was a problem hiding this comment.
Still need to work and investigate the test failure, why 504&506 are sent twice.
| pub fn filter_by_topics( | ||
| &self, | ||
| topics: Vec<String>, | ||
| state: &GenericCommandState, |
| for topic in topics { | ||
| // if topic contains a template for the root prefix, replace that template with mapper prefix. | ||
| // else if topic contains a template not for the root prefix, reject that topic when appending the topic filter | ||
| // else if no template is present, simply add that topic to the topic filter (assuming that provided pattern is valid) | ||
| let topic = topic | ||
| .split_inclusive("}") | ||
| .flat_map(|s| match s.find("${") { | ||
| None => vec![s], | ||
| Some(i) => { | ||
| let (prefix, template) = s.split_at(i); | ||
| vec![prefix, template] | ||
| } | ||
| }) | ||
| .map(|s| { | ||
| if let Some(template) = GenericCommandState::extract_path(s) { | ||
| if template.eq(".topic.root_prefix") { | ||
| prefix.as_str() | ||
| } else { | ||
| warn!("The custom topic '{topic}' is invalid and ignored."); | ||
| s | ||
| } | ||
| } else { | ||
| s | ||
| } | ||
| }) | ||
| .collect::<String>(); | ||
|
|
There was a problem hiding this comment.
The logic here can be much simpler. You just need to replace ${.bridge.topic_prefix} by C8yMapperConfig::c8y_prefix.
7942628 to
1640988
Compare
1640988 to
33ebe9a
Compare
33ebe9a to
f8ee8f8
Compare
| (None, Some(on_fragment)) => vec.push((on_fragment, op.clone())), | ||
| (Some(topic), Some(on_fragment)) if topic == topic_name => { | ||
| (Some(ref topic), Some(on_fragment)) | ||
| if record.inject_values_into_template(topic) == topic_name => |
There was a problem hiding this comment.
It would be better to inject any ${.bridge...} config when the operations are loaded. The bridge config will not change after the mapper starts.
| if record.inject_values_into_template(topic) == topic_name => | |
| if topic == topic_name => |
| struct TestBridgeConfig { | ||
| c8y_prefix: TopicPrefix, | ||
| } |
There was a problem hiding this comment.
Why another struct TestBridgeConfig that is exactly a copy of BridgeConfig?
There was a problem hiding this comment.
BridgeConfig is part of C8yMapperConfig so cannot provide it to operations but one is needed to run tests
Signed-off-by: Krzysztof Piotrowski <K.R.Piotrowski17@gmail.com>
Signed-off-by: Krzysztof Piotrowski <K.R.Piotrowski17@gmail.com>
Signed-off-by: Krzysztof Piotrowski <K.R.Piotrowski17@gmail.com>
4c8e045 to
0ca2b20
Compare
Proposed changes
This PR adds support for user topics when handling custom operation. Custom topic can be provided in custom operation in following way (templates supported):
Mapper replace template with mapper prefix (if needed) and subscribe to the topic. If template is different than
${.bridge.topic_prefix}then this topic will be skipped.TODO:
Types of changes
Paste Link to the issue
Checklist
cargo fmtas mentioned in CODING_GUIDELINEScargo clippyas mentioned in CODING_GUIDELINESFurther comments