8000 addressed the comments; docs refrences and refactoring · localstack/localstack@73184cd · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit 73184cd

Browse files
committed
addressed the comments; docs refrences and refactoring
1 parent 5d5f678 commit 73184cd

File tree

6 files changed

+45
-40
lines changed

6 files changed

+45
-40
lines changed

localstack

Lines changed: 0 additions & 1 deletion
This file was deleted.

localstack-core/localstack/services/events/provider.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@
9292
from localstack.aws.api.events import Rule as ApiTypeRule
9393
from localstack.services.events.archive import ArchiveService, ArchiveServiceDict
9494
from localstack.services.events.event_bus import EventBusService, EventBusServiceDict
95-
from localstack.utils.event_matcher import matches_event as matches_rule
9695
from localstack.services.events.models import (
9796
Archive,
9897
ArchiveDict,
@@ -132,6 +131,7 @@
132131
)
133132
from localstack.services.plugins import ServiceLifecycleHook
134133
from localstack.utils.common import truncate
134+
from localstack.utils.event_matcher import matches_event as matches_rule
135135
from localstack.utils.strings import long_uid
136136
from localstack.utils.time import TIMESTAMP_FORMAT_TZ, timestamp
137137< 10000 /code>

localstack-core/localstack/services/events/v1/provider.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@
4040
from localstack.constants import APPLICATION_AMZ_JSON_1_1
4141
from localstack.http import route
4242
from localstack.services.edge import ROUTER
43-
from localstack.utils.event_matcher import matches_event as matches_rule
44-
4543
from localstack.services.events.models import (
4644
InvalidEventPatternException as InternalInvalidEventPatternException,
4745
)
@@ -55,6 +53,7 @@
5553
from localstack.utils.aws.message_forwarding import send_event_to_target
5654
from localstack.utils.collections import pick_attributes
5755
from localstack.utils.common import TMP_FILES, mkdir, save_file, truncate
56+
from localstack.utils.event_matcher import matches_event as matches_rule
5857
from localstack.utils.json import extract_jsonpath
5958
from localstack.utils.strings import long_uid, short_uid
6059
from localstack.utils.time import TIMESTAMP_FORMAT_TZ, timestamp

localstack-core/localstack/services/lambda_/event_source_mapping/pollers/poller.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,14 @@
55

66
from botocore.client import BaseClient
77

8-
from localstack import config
98
from localstack.aws.api.pipes import PipeStateReason
10-
from localstack.utils.event_matcher import matches_event
11-
129
from localstack.services.lambda_.event_source_mapping.event_processor import EventProcessor
1310
from localstack.services.lambda_.event_source_mapping.noops_event_processor import (
1411
NoOpsEventProcessor,
1512
)
1613
from localstack.services.lambda_.event_source_mapping.pipe_utils import get_internal_client
1714
from localstack.utils.aws.arns import parse_arn
15+
from localstack.utils.event_matcher import matches_event
1816

1917

2018
class PipeStateReasonValues(PipeStateReason):
Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,39 @@
1-
# localstack/utils/event_matcher.py
21
import json
3-
from typing import Any, Dict, Union
2+
from typing import Any
3+
44
from localstack import config
55
from localstack.services.events.event_ruler import matches_rule
66
from localstack.services.events.v1.utils import matches_event as python_matches_event
77

8-
def matches_event(event_pattern: Union[Dict[str, Any], str], event: Union[Dict[str, Any], str]) -> bool:
8+
9+
def matches_event(event_pattern: dict[str, Any] | str, event: dict[str, Any] | str) -> bool:
910
"""
1011
Match events based on configured rule engine.
11-
12+
1213
Note: Different services handle patterns/events differently:
13-
- EventBridge uses strings
14+
- EventBridge uses strings
1415
- ESM and Pipes use dicts
15-
16+
1617
Args:
1718
event_pattern: Event pattern (str for EventBridge, dict for ESM/Pipes)
1819
event: Event to match against pattern (str for EventBridge, dict for ESM/Pipes)
19-
20+
2021
Returns:
2122
bool: True if event matches pattern, False otherwise
22-
23+
2324
Examples:
2425
# EventBridge (string-based):
2526
>>> pattern = '{"source": ["aws.ec2"]}'
2627
>>> event = '{"source": "aws.ec2"}'
27-
28+
2829
# ESM/Pipes (dict-based):
2930
>>> pattern = {"source": ["aws.ec2"]}
3031
>>> event = {"source": "aws.ec2"}
32+
33+
References:
34+
- EventBridge Patterns: https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-event-patterns.html
35+
- EventBridge Pipes: https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-pipes-event-filtering.html
36+
- Event Source Mappings: https://docs.aws.amazon.com/lambda/latest/dg/invocation-eventfiltering.html
3137
"""
3238
if config.EVENT_RULE_ENGINE == "java":
3339
# If inputs are already strings (EventBridge), use directly
@@ -37,8 +43,8 @@ def matches_event(event_pattern: Union[Dict[str, Any], str], event: Union[Dict[s
3743
event_str = event if isinstance(event, str) else json.dumps(event)
3844
pattern_str = event_pattern if isinstance(event_pattern, str) else json.dumps(event_pattern)
3945
return matches_rule(event_str, pattern_str)
40-
46+
4147
# Python implementation needs dicts
4248
event_dict = json.loads(event) if isinstance(event, str) else event
4349
pattern_dict = json.loads(event_pattern) if isinstance(event_pattern, str) else event_pattern
44-
return python_matches_event(pattern_dict, event_dict)
50+
return python_matches_event(pattern_dict, event_dict)
Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
import json
2+
23
import pytest
4+
35
from localstack import config
46
from localstack.utils.event_matcher import matches_event
57

6-
78
EVENT_PATTERN_DICT = {
89
"source": ["aws.ec2"],
9-
"detail-type": ["EC2 Instance State-change Notification"]
10+
"detail-type": ["EC2 Instance State-change Notification"],
1011
}
1112
EVENT_DICT = {
1213
"source": "aws.ec2",
1314
"detail-type": "EC2 Instance State-change Notification",
14-
"detail": {"state": "running"}
15+
"detail": {"state": "running"},
1516
}
1617
EVENT_PATTERN_STR = json.dumps(EVENT_PATTERN_DICT)
1718
EVENT_STR = json.dumps(EVENT_DICT)
@@ -30,41 +31,38 @@ def _set_engine(engine: str):
3031
def test_matches_event_with_java_engine_strings(event_rule_engine):
3132
"""Test Java engine with string inputs (EventBridge case)"""
3233
event_rule_engine("java")
33-
assert matches_event(EVENT_PATTERN_STR, EVENT_STR) == True
34+
assert matches_event(EVENT_PATTERN_STR, EVENT_STR)
3435

3536

3637
def test_matches_event_with_java_engine_dicts(event_rule_engine):
3738
"""Test Java engine with dict inputs (ESM/Pipes case)"""
3839
event_rule_engine("java")
39-
assert matches_event(EVENT_PATTERN_DICT, EVENT_DICT) == True
40+
assert matches_event(EVENT_PATTERN_DICT, EVENT_DICT)
4041

4142

4243
def test_matches_event_with_python_engine_strings(event_rule_engine):
4344
"""Test Python engine with string inputs"""
4445
event_rule_engine("python")
45-
assert matches_event(EVENT_PATTERN_STR, EVENT_STR) == True
46+
assert matches_event(EVENT_PATTERN_STR, EVENT_STR)
4647

4748

4849
def test_matches_event_with_python_engine_dicts(event_rule_engine):
4950
"""Test Python engine with dict inputs"""
5051
event_rule_engine("python")
51-
assert matches_event(EVENT_PATTERN_DICT, EVENT_DICT) == True
52+
assert matches_event(EVENT_PATTERN_DICT, EVENT_DICT)
5253

5354

5455
def test_matches_event_mixed_inputs(event_rule_engine):
5556
"""Test with mixed string/dict inputs"""
5657
event_rule_engine("java")
57-
assert matches_event(EVENT_PATTERN_STR, EVENT_DICT) == True
58-
assert matches_event(EVENT_PATTERN_DICT, EVENT_STR) == True
58+
assert matches_event(EVENT_PATTERN_STR, EVENT_DICT)
59+
assert matches_event(EVENT_PATTERN_DICT, EVENT_STR)
5960

6061

6162
def test_matches_event_non_matching_pattern():
6263
"""Test with non-matching pattern"""
63-
non_matching_pattern = {
64-
"source": ["aws.s3"],
65-
"detail-type": ["S3 Event"]
66-
}
67-
assert matches_event(non_matching_pattern, EVENT_DICT) == False
64+
non_matching_pattern = {"source": ["aws.s3"], "detail-type": ["S3 Event"]}
65+
assert not matches_event(non_matching_pattern, EVENT_DICT)
6866

6967

7068
def test_matches_event_invalid_json():
@@ -76,35 +74,40 @@ def test_matches_event_invalid_json():
7674
def test_matches_event_missing_fields():
7775
"""Test with missing required fields"""
7876
incomplete_event = {"source": "aws.ec2"}
79-
assert matches_event(EVENT_PATTERN_DICT, incomplete_event) == False
77+
assert not matches_event(EVENT_PATTERN_DICT, incomplete_event)
8078

8179

8280
def test_matches_event_pattern_matching():
83-
"""Test various pattern matching scenarios based on AWS examples"""
81+
"""Test various pattern matching scenarios based on AWS examples
82+
83+
Examples taken from:
84+
- EventBridge: https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-event-patterns-content-based-filtering.html
85+
- SNS Filtering: https://docs.aws.amazon.com/sns/latest/dg/sns-subscription-filter-policies.html
86+
"""
8487
test_cases = [
8588
# Exact matching
8689
(
8790
{"source": ["aws.ec2"], "detail-type": ["EC2 Instance State-change Notification"]},
8891
{"source": "aws.ec2", "detail-type": "EC2 Instance State-change Notification"},
89-
True
92+
True,
9093
),
9194
# Prefix matching in detail field
9295
(
9396
{"source": ["aws.ec2"], "detail": {"state": [{"prefix": "run"}]}},
9497
{"source": "aws.ec2", "detail": {"state": "running"}},
95-
True
98+
True,
9699
),
97100
# Multiple possible values
98101
(
99102
{"source": ["aws.ec2"], "detail": {"state": ["pending", "running"]}},
100103
{"source": "aws.ec2", "detail": {"state": "running"}},
101-
True
104+
True,
102105
),
103106
# Anything-but matching
104107
(
105108
{"source": ["aws.ec2"], "detail": {"state": [{"anything-but": "terminated"}]}},
106109
{"source": "aws.ec2", "detail": {"state": "running"}},
107-
True
110+
True,
108111
),
109112
]
110113

@@ -116,6 +119,6 @@ def test_matches_event_case_sensitivity():
116119
"""Test case sensitivity in matching"""
117120
case_different_event = {
118121
"source": "AWS.ec2",
119-
"detail-type": "EC2 Instance State-Change Notification"
122+
"detail-type": "EC2 Instance State-Change Notification",
120123
}
121-
assert matches_event(EVENT_PATTERN_DICT, case_different_event) == False
124+
assert not matches_event(EVENT_PATTERN_DICT, case_different_event)

0 commit comments

Comments
 (0)
0