8000 Add restructurings to fix events persistence in v2 · localstack/localstack@7e07d72 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7e07d72

Browse files
committed
Add restructurings to fix events persistence in v2
1 parent 3be7ae0 commit 7e07d72

File tree

4 files changed

+51
-39
lines changed

4 files changed

+51
-39
lines changed

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

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import json
22
from datetime import datetime, timezone
3-
from typing import Optional
3+
from typing import Optional, Self
44

55
from localstack.aws.api.events import (
66
Action,
@@ -23,27 +23,32 @@ class EventBusService:
2323
event_source_name: str | None
2424
tags: TagList | None
2525
policy: str | None
26-
rules: RuleDict | None
2726
event_bus: EventBus
2827

29-
def __init__(
30-
self,
28+
def __init__(self, event_bus: EventBus):
29+
self.event_bus = event_bus
30+
31+
@classmethod
32+
def create_event_bus_service(
33+
cls,
3134
name: EventBusName,
3235
region: str,
3336
account_id: str,
3437
event_source_name: Optional[str] = None,
3538
tags: Optional[TagList] = None,
3639
policy: Optional[str] = None,
3740
rules: Optional[RuleDict] = None,
38-
):
39-
self.event_bus = EventBus(
40-
name,
41-
region,
42-
account_id,
43-
event_source_name,
44-
tags,
45-
policy,
46-
rules,
41+
) -> Self:
42+
return cls(
43+
EventBus(
44+
name,
45+
region,
46+
account_id,
47+
event_source_name,
48+
tags,
49+
policy,
50+
rules,
51+
)
4752
)
4853

4954
@property

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,4 +237,4 @@ class EventsStore(BaseStore):
237237
TAGS: TaggingService = CrossRegionAttribute(default=TaggingService)
238238

239239

240-
events_store = AccountRegionBundle("events", EventsStore)
240+
events_stores = AccountRegionBundle("events", EventsStore)

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@
132132
RuleDict,
133133
TargetDict,
134134
ValidationException,
135-
events_store,
135+
events_stores,
136136
)
137137
from localstack.services.events.models import (
138138
InvalidEventPatternException as InternalInvalidEventPatternException,
@@ -1521,7 +1521,7 @@ def untag_resource(
15211521
def get_store(self, region: str, account_id: str) -> EventsStore:
15221522
"""Returns the events store for the account and region.
15231523
On first call, creates the default event bus for the account region."""
1524-
store = events_store[account_id][region]
1524+
store = events_stores[account_id][region]
15251525
# create default event bus for account region on first call
15261526
default_event_bus_name = "default"
15271527
if default_event_bus_name not in store.event_buses:
@@ -1577,7 +1577,7 @@ def create_event_bus_service(
15771577
event_source_name: Optional[EventSourceName],
15781578
tags: Optional[TagList],
15791579
) -> EventBusService:
1580-
event_bus_service = EventBusService(
1580+
event_bus_service = EventBusService.create_event_bus_service(
15811581
name,
15821582
region,
15831583
account_id,
@@ -1601,7 +1601,7 @@ def create_rule_service(
16011601
event_bus_name: Optional[EventBusName],
16021602
targets: Optional[TargetDict],
16031603
) -> RuleService:
1604-
rule_service = RuleService(
1604+
rule_service = RuleService.create_rule_service(
16051605
name,
16061606
region,
16071607
account_id,

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

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,16 @@ class RuleService:
4242
managed_by: ManagedBy
4343
rule: Rule
4444

45-
def __init__(
46-
self,
45+
def __init__(self, rule: Rule):
46+
self.rule = rule
47+
if rule.schedule_expression:
48+
self.schedule_cron = self._get_schedule_cron(rule.schedule_expression)
49+
else:
50+
self.schedule_cron = None
51+
52+
@classmethod
53+
def create_rule_service(
54+
cls,
4755
name: RuleName,
4856
region: Optional[str] = None,
4957
account_id: Optional[str] = None,
@@ -57,25 +65,23 @@ def __init__(
5765
targets: Optional[TargetDict] = None,
5866
managed_by: Optional[ManagedBy] = None,
5967
):
60-
self._validate_input(event_pattern, schedule_expression, event_bus_name)
61-
if schedule_expression:
62-
self.schedule_cron = self._get_schedule_cron(schedule_expression)
63-
else:
64-
self.schedule_cron = None
68+
cls._validate_input(event_pattern, schedule_expression, event_bus_name)
6569
# required to keep data and functionality separate for persistence
66-
self.rule = Rule(
67-
name,
68-
region,
69-
account_id,
70-
schedule_expression,
71-
event_pattern,
72-
state,
73-
description,
74-
role_arn,
75-
tags,
76-
event_bus_name,
77-
targets,
78-
managed_by,
70+
return cls(
71+
Rule(
72+
name,
73+
region,
74+
account_id,
75+
schedule_expression,
76+
event_pattern,
77+
state,
78+
description,
79+
role_arn,
80+
tags,
81+
event_bus_name,
82+
targets,
83+
managed_by,
84+
)
7985
)
8086

8187
@property
@@ -178,8 +184,9 @@ def validate_targets_input(self, targets: TargetList) -> PutTargetsResultEntryLi
178184

179185
return validation_errors
180186

187+
@classmethod
181188
def _validate_input(
182-
self,
189+
cls,
183190
event_pattern: Optional[EventPattern],
184191
schedule_expression: Optional[ScheduleExpression],
185192
event_bus_name: Optional[EventBusName] = "default",

0 commit comments

Comments
 (0)
0