Skip to content
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

Merged
merged 23 commits into from May 14, 2024

Conversation

maxhoheiser
Copy link
Member

@maxhoheiser maxhoheiser commented May 8, 2024

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.

@maxhoheiser maxhoheiser self-assigned this May 8, 2024
@maxhoheiser maxhoheiser requested a review from joe4dev May 8, 2024 06:59
@maxhoheiser maxhoheiser added aws:events Amazon EventBridge semver: minor Non-breaking changes which can be included in minor releases, but not in patch releases labels May 8, 2024
@maxhoheiser maxhoheiser force-pushed the feature/eventbridge_v2-add-input-transformer branch 2 times, most recently from 1d731cb to fe49153 Compare May 8, 2024 07:01
Copy link

github-actions bot commented May 8, 2024

LocalStack Community integration with Pro

    2 files  ±0      2 suites  ±0   1h 36m 50s ⏱️ +51s
2 941 tests +6  2 640 ✅ +1  301 💤 +5  0 ❌ ±0 
2 943 runs  +6  2 640 ✅ +1  303 💤 +5  0 ❌ ±0 

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.
tests.aws.services.events.test_events_inputs.TestEventsInputPath ‑ test_put_events_with_input_path
tests.aws.services.events.test_events_inputs.TestEventsInputPath ‑ test_put_events_with_input_path_max_level_depth
tests.aws.services.events.test_events_inputs.TestEventsInputPath ‑ test_put_events_with_input_path_multiple_targets
tests.aws.services.events.test_events_inputs.TestEventsInputPath ‑ test_put_events_with_input_path_nested[event_detail0]
tests.aws.services.events.test_events_inputs.TestEventsInputPath ‑ test_put_events_with_input_path_nested[event_detail1]
tests.aws.services.events.test_events_inputs.TestEventsInputTransformers ‑ test_put_events_with_input_transformation_to_sqs
tests.aws.services.events.test_events_inputs ‑ test_put_event_input_path_and_input_transfomer
tests.aws.services.events.test_events_inputs.TestInputPath ‑ test_put_events_with_input_path
tests.aws.services.events.test_events_inputs.TestInputPath ‑ test_put_events_with_input_path_max_level_depth
tests.aws.services.events.test_events_inputs.TestInputPath ‑ test_put_events_with_input_path_multiple_targets
tests.aws.services.events.test_events_inputs.TestInputPath ‑ test_put_events_with_input_path_nested[event_detail0]
tests.aws.services.events.test_events_inputs.TestInputPath ‑ test_put_events_with_input_path_nested[event_detail1]
tests.aws.services.events.test_events_inputs.TestInputTransformer ‑ test_input_transformer_predefined_variables["Message containing all pre defined variables <aws.events.rule-arn> <aws.events.rule-name> <aws.events.event.ingestion-time>"]
tests.aws.services.events.test_events_inputs.TestInputTransformer ‑ test_input_transformer_predefined_variables[{"originalEvent": <aws.events.event>, "originalEventJson": <aws.events.event.json>}]
tests.aws.services.events.test_events_inputs.TestInputTransformer ‑ test_put_events_with_input_transformer_input_template_json
tests.aws.services.events.test_events_inputs.TestInputTransformer ‑ test_put_events_with_input_transformer_input_template_string["Event of <detail-type> type, at time <timestamp>, info extracted from detail <command>"]
…

♻️ This comment has been updated with latest results.

@maxhoheiser maxhoheiser force-pushed the feature/eventbridge_v2-add-input-transformer branch 4 times, most recently from 0f42ca8 to c80c43f Compare May 14, 2024 11:45
Copy link
Member

@joe4dev joe4dev left a 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.

localstack/services/events/target.py Show resolved Hide resolved
predefined_template_replacements = self._get_predefined_template_replacements(event)
template_replacements.update(predefined_template_replacements)

is_json_format = input_template.strip().startswith(("{"))
Copy link
Member

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?

Copy link
Member Author

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 Show resolved Hide resolved
"""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]
Copy link
Member

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?

Copy link
Member Author

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

localstack/services/events/target.py Outdated Show resolved Hide resolved
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
Copy link
Member

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?

Copy link
Member Author

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.

Copy link
Member

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)
Copy link
Member

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?

Copy link
Member Author

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.

localstack/services/events/target.py Show resolved Hide resolved
@maxhoheiser maxhoheiser force-pushed the feature/eventbridge_v2-add-input-transformer branch from c80c43f to 4daa1ed Compare May 14, 2024 16:20
@maxhoheiser maxhoheiser force-pushed the feature/eventbridge_v2-add-input-transformer branch from 38f7df8 to 2fcae48 Compare May 14, 2024 16:45
@maxhoheiser maxhoheiser marked this pull request as ready for review May 14, 2024 17:58
@maxhoheiser maxhoheiser merged commit 3c4c463 into master May 14, 2024
30 checks passed
@maxhoheiser maxhoheiser deleted the feature/eventbridge_v2-add-input-transformer branch May 14, 2024 17:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
aws:events Amazon EventBridge semver: minor Non-breaking changes which can be included in minor releases, but not in patch releases
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants