-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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 path #10733
Changes from all commits
1d50936
279f03a
782930e
f33c015
0984224
07951a2
da96b7c
bc5d39a
b9d2dd3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,14 @@ | ||
from dataclasses import dataclass, field | ||
from typing import Optional | ||
from typing import Optional, TypeAlias, TypedDict | ||
|
||
from localstack.aws.api.core import ServiceException | ||
from localstack.aws.api.events import ( | ||
Arn, | ||
CreatedBy, | ||
EventBusName, | ||
EventPattern, | ||
EventResourceList, | ||
EventSourceName, | ||
ManagedBy, | ||
RoleArn, | ||
RuleDescription, | ||
|
@@ -102,3 +104,18 @@ class InvalidEventPatternException(Exception): | |
def __init__(self, reason=None, message=None) -> None: | ||
self.reason = reason | ||
self.message = message or f"Event pattern is not valid. Reason: {reason}" | ||
|
||
|
||
class FormattedEvent(TypedDict): | ||
version: str | ||
id: str | ||
detail_type: Optional[str] # key "detail-type" is automatically interpreted as detail_type | ||
source: Optional[EventSourceName] | ||
account: str | ||
time: str | ||
region: str | ||
resources: Optional[EventResourceList] | ||
detail: dict[str, str | dict] | ||
|
||
|
||
TransformedEvent: TypeAlias = FormattedEvent | dict | str | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can understand why we end up such a flexible type definition given the many possibilities using input templates. For readers, this type definition is quite confusing. Consider adding a comment for now. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -194,23 +194,12 @@ def _delete_log_group(): | |
|
||
|
||
@pytest.fixture | ||
def put_events_with_filter_to_sqs(aws_client, sqs_get_queue_arn, clean_up): | ||
def create_sqs_events_target(aws_client, sqs_get_queue_arn): | ||
queue_urls = [] | ||
event_bus_names = [] | ||
rule_names = [] | ||
target_ids = [] | ||
|
||
def _put_events_with_filter_to_sqs( | ||
pattern: dict, | ||
entries_asserts: list[Tuple[list[dict], bool]], | ||
input_path: str = None, | ||
input_transformer: dict[dict, str] = None, | ||
): | ||
queue_name = f"queue-{short_uid()}" | ||
rule_name = f"rule-{short_uid()}" | ||
target_id = f"target-{short_uid()}" | ||
bus_name = f"bus-{short_uid()}" | ||
|
||
def _create_sqs_events_target(queue_name: str | None = None) -> tuple[str, str]: | ||
if not queue_name: | ||
queue_name = f"tests-queue-{short_uid()}" | ||
sqs_client = aws_client.sqs | ||
queue_url = sqs_client.create_queue(QueueName=queue_name)["QueueUrl"] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This on purpose does not use the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok, fair point 👍 Why is no cleanup required here? Wouldn't the factory remove the need for an explicit cleanup after the yield here? It might have to do with the proper cleanup order 🤔 |
||
queue_urls.append(queue_url) | ||
|
@@ -231,6 +220,34 @@ def _put_events_with_filter_to_sqs( | |
sqs_client.set_queue_attributes( | ||
QueueUrl=queue_url, Attributes={"Policy": json.dumps(policy)} | ||
) | ||
return queue_url, queue_arn | ||
|
||
yield _create_sqs_events_target | ||
|
||
for queue_url in queue_urls: | ||
try: | ||
aws_client.sqs.delete_queue(QueueUrl=queue_url) | ||
except Exception as e: | ||
LOG.debug("error cleaning up queue %s: %s", queue_url, e) | ||
|
||
|
||
@pytest.fixture | ||
def put_events_with_filter_to_sqs(aws_client, create_sqs_events_target, clean_up): | ||
event_bus_names = [] | ||
rule_names = [] | ||
target_ids = [] | ||
|
||
def _put_events_with_filter_to_sqs( | ||
pattern: dict, | ||
entries_asserts: list[Tuple[list[dict], bool]], | ||
input_path: str = None, | ||
input_transformer: dict[dict, str] = None, | ||
): | ||
rule_name = f"test-rule-{short_uid()}" | ||
target_id = f"test-target-{short_uid()}" | ||
bus_name = f"test-bus-{short_uid()}" | ||
|
||
queue_url, queue_arn = create_sqs_events_target() | ||
|
||
events_client = aws_client.events | ||
events_client.create_event_bus(Name=bus_name) | ||
|
@@ -262,7 +279,7 @@ def _put_events_with_filter_to_sqs( | |
entry["EventBusName"] = bus_name | ||
message = _put_entries_assert_results_sqs( | ||
events_client, | ||
sqs_client, | ||
aws_client.sqs, | ||
queue_url, | ||
entries=entries, | ||
should_match=entry_asserts[1], | ||
|
@@ -274,14 +291,11 @@ def _put_events_with_filter_to_sqs( | |
|
||
yield _put_events_with_filter_to_sqs | ||
|
||
for queue_url, event_bus_name, rule_name, target_id in zip( | ||
queue_urls, event_bus_names, rule_names, target_ids | ||
): | ||
for event_bus_name, rule_name, target_id in zip(event_bus_names, rule_names, target_ids): | ||
clean_up( | ||
bus_name=event_bus_name, | ||
rule_name=rule_name, | ||
target_ids=target_id, | ||
queue_url=queue_url, | ||
) | ||
|
||
|
||
|
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.
@maxhoheiser This type definition allows both
detail-type
anddetail_type
, but we should restrict it to the hyphen-only variant.Python supports an alternative syntax for defining TypedDicts, which is recommended in such cases:
All credits go to @dfangl for this suggestion 💯