Skip to content

feat: support user topics when handling custom operation#3189

Merged
Ruadhri17 merged 3 commits intothin-edge:mainfrom
Ruadhri17:custom-operation-topic
Oct 29, 2024
Merged

feat: support user topics when handling custom operation#3189
Ruadhri17 merged 3 commits intothin-edge:mainfrom
Ruadhri17:custom-operation-topic

Conversation

@Ruadhri17
Copy link
Copy Markdown
Contributor

@Ruadhri17 Ruadhri17 commented Oct 15, 2024

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):

[exec]
topic = "${.bridge.topic_prefix}/custom/topic/one"
on_fragment = "com_cumulocity_model_WebCamDevice"
command = "/usr/bin/do_something.sh ${.payload.com_cumulocity_model_WebCamDevice.parameters.duration}"

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:

  • Robot framework test

Types of changes

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Improvement (general improvements like code refactoring that doesn't explicitly fix a bug or add any new functionality)
  • Documentation Update (if none of the other choices apply)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

Paste Link to the issue


Checklist

  • I have read the CONTRIBUTING doc
  • I have signed the CLA (in all commits with git commit -s)
  • I ran cargo fmt as mentioned in CODING_GUIDELINES
  • I used cargo clippy as mentioned in CODING_GUIDELINES
  • I have added tests that prove my fix is effective or that my feature works
  • I have added necessary documentation (if appropriate)

Further comments

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Oct 15, 2024

Robot Results

✅ Passed ❌ Failed ⏭️ Skipped Total Pass % ⏱️ Duration
525 0 2 525 100 1h31m19.722918s

@codecov
Copy link
Copy Markdown

codecov bot commented Oct 16, 2024

@Ruadhri17 Ruadhri17 marked this pull request as ready for review October 16, 2024 09:02
@Ruadhri17 Ruadhri17 marked this pull request as draft October 16, 2024 09:53
@Ruadhri17 Ruadhri17 force-pushed the custom-operation-topic branch from 1fcdbbd to e902e6e Compare October 16, 2024 18:21
@Ruadhri17 Ruadhri17 force-pushed the custom-operation-topic branch from e902e6e to fadb1d1 Compare October 16, 2024 18:26
@Ruadhri17 Ruadhri17 force-pushed the custom-operation-topic branch from fadb1d1 to 27bc06d Compare October 16, 2024 19:10
@Ruadhri17 Ruadhri17 temporarily deployed to Test Pull Request October 16, 2024 20:20 — with GitHub Actions Inactive
@Ruadhri17 Ruadhri17 force-pushed the custom-operation-topic branch from 121f19c to 29a8439 Compare October 16, 2024 20:30
@Ruadhri17 Ruadhri17 temporarily deployed to Test Pull Request October 16, 2024 20:30 — with GitHub Actions Inactive
@Ruadhri17 Ruadhri17 marked this pull request as ready for review October 16, 2024 20:30
@Ruadhri17 Ruadhri17 requested review from a team and gligorisaev as code owners October 16, 2024 20:30
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The added test step is doing what it was intended for

Copy link
Copy Markdown
Contributor

@gligorisaev gligorisaev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resolved by c1311aa

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

${.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}).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Though doesn't the the property name topic suggest otherwise?

Copy link
Copy Markdown
Contributor

@reubenmiller reubenmiller Oct 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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.

Copy link
Copy Markdown
Member

@rina23q rina23q left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As per this comment, this function doesn't have to do anything with workflow's GenericCommandState for the topic. Instead, you need to parse prefix here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resolved by c1311aa

Comment on lines +301 to +302
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>();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic here can be much simpler. You just need to replace ${.bridge.topic_prefix} by C8yMapperConfig::c8y_prefix.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resolved by c1311aa

(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 =>
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better to inject any ${.bridge...} config when the operations are loaded. The bridge config will not change after the mapper starts.

Suggested change
if record.inject_values_into_template(topic) == topic_name =>
if topic == topic_name =>

Comment on lines +485 to +487
struct TestBridgeConfig {
c8y_prefix: TopicPrefix,
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why another struct TestBridgeConfig that is exactly a copy of BridgeConfig?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BridgeConfig is part of C8yMapperConfig so cannot provide it to operations but one is needed to run tests

Copy link
Copy Markdown
Contributor

@didier-wenzek didier-wenzek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved.

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>
@Ruadhri17 Ruadhri17 force-pushed the custom-operation-topic branch from 4c8e045 to 0ca2b20 Compare October 29, 2024 16:34
@Ruadhri17 Ruadhri17 temporarily deployed to Test Pull Request October 29, 2024 16:34 — with GitHub Actions Inactive
@Ruadhri17 Ruadhri17 enabled auto-merge October 29, 2024 16:35
@Ruadhri17 Ruadhri17 added this pull request to the merge queue Oct 29, 2024
Merged via the queue into thin-edge:main with commit 77c7a01 Oct 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

theme:c8y Theme: Cumulocity related topics

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants