New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/eventbridge v2 add input transformer #10789
Feature/eventbridge v2 add input transformer #10789
Conversation
1d731cb
to
fe49153
Compare
LocalStack Community integration with Pro 2 files ±0 2 suites ±0 1h 36m 50s ⏱️ +51s Results for commit ab05337. ± Comparison against base commit e69d60f. This pull request removes 6 and adds 12 tests. Note that renamed tests count towards both.
♻️ This comment has been updated with latest results. |
0f42ca8
to
c80c43f
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM overall 👍
Looking forward to reusing this input transformer in Pipes as well in the future 🚀
I commented regarding a couple of edge cases, which would be nice to fix or validate.
predefined_template_replacements = self._get_predefined_template_replacements(event) | ||
template_replacements.update(predefined_template_replacements) | ||
|
||
is_json_format = input_template.strip().startswith(("{")) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it make sense to test this assumption? For example: {<aws.events.rule-name> works?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
extended test in: ab05337
localstack/services/events/target.py
Outdated
"""Extracts predefined values from the event.""" | ||
predefined_template_replacements = {} | ||
predefined_template_replacements["aws.events.rule-arn"] = self.rule_arn | ||
predefined_template_replacements["aws.events.rule-name"] = self.rule_arn.split("/")[-1] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The split won't work if the name contains a slash /
, which is a valid character according to this regex: (arn:aws[\w-]*:events:[a-z]{2}-[a-z]+-[\w-]+:[0-9]{12}:event-bus\/)?[/\.\-_A-Za-z0-9]+
https://docs.aws.amazon.com/eventbridge/latest/APIReference/API_PutRule.html#eventbridge-PutRule-request-EventBusName
See regex example: https://regex101.com/r/aNQ4KF/1
Maybe parse_arn
would be more reliable here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pars_arn did not work, since it only returns the resource that contains bus and rule name and as you pointed out both bus and rule name can have /. I added rule_name
as a TargetSender instance variable.
Fixed in: 1b0366d
def replace_placeholder(match): | ||
key = match.group(1) | ||
value = replacements.get(key, match.group(0)) # handle non defined placeholders | ||
return json.dumps(value) if is_json else value |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How are non-string values such as the discussed integer scenario work now?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't deal with this one edge case for for the predefined placeholder <aws.events.events> where the event body contains "time" which is returned as an int, not a string. I figure that this is such an edge case that we can deal with it once we have the provider at parity.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair point 👍
Let's keep track of it somewhere for the future.
for placeholder, transformer_path in transformer_path_map.items(): | ||
if placeholder in PREDEFINED_PLACEHOLDERS: | ||
continue | ||
value = extract_jsonpath(event, transformer_path) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this fail with an invalid jsonpath?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the boto client validation of input template that checks that only placeholders that are using a json path that is present in the defined event should handle errors long before extract_jsonpath
will be called, therefore it should be save.
c80c43f
to
4daa1ed
Compare
38f7df8
to
2fcae48
Compare
Motivation
This PR adds input transformers https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-transform-target-input.html to the v2 provider. Input transformers use either predefined placeholders or custom placeholders defined in input template or a combination of both, that are used to re-write the returned event.
Predefined placeholders can return either a string or a dict ( dicts are only allowed in json input maps)
The event that is returned is the input map that gets populated with the placeholders described before with the variables from the original event and rule. The input map can be either a plain string or a json string that results in a return dict.
Changes
Add input transformer to transform incoming event based on input path and input map.
Add predefined placeholders.
Add tests for input path string and json.
Add test for predefined placeholders.
Add test for combining input path and input transfomrer.