8000 Add restructurings to fix events persistence in v2 by dfangl · Pull Request #11889 · localstack/localstack · GitHub
[go: up one dir, main page]

Skip to content

Add restructurings to fix events persistence in v2 #11889

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

Merged
merged 1 commit into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 18 additions & 13 deletions localstack-core/localstack/services/events/event_bus.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import json
from datetime import datetime, timezone
from typing import Optional
from typing import Optional, Self

from localstack.aws.api.events import (
Action,
Expand All @@ -23,27 +23,32 @@ class EventBusService:
event_source_name: str | None
tags: TagList | None
policy: str | None
rules: RuleDict | None
event_bus: EventBus

def __init__(
self,
def __init__(self, event_bus: EventBus):
self.event_bus = event_bus

@classmethod
def create_event_bus_service(
cls,
name: EventBusName,
region: str,
account_id: str,
event_source_name: Optional[str] = None,
tags: Optional[TagList] = None,
policy: Optional[str] = None,
rules: Optional[RuleDict] = None,
):
self.event_bus = EventBus(
name,
region,
account_id,
event_source_name,
tags,
policy,
rules,
) -> Self:
return cls(
EventBus(
name,
region,
account_id,
event_source_name,
tags,
policy,
rules,
)
)

@property
Expand Down
2 changes: 1 addition & 1 deletion localstack-core/localstack/services/events/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,4 +237,4 @@ class EventsStore(BaseStore):
TAGS: TaggingService = CrossRegionAttribute(default=TaggingService)


events_store = AccountRegionBundle("events", EventsStore)
events_stores = AccountRegionBundle("events", EventsStore)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comment: this was the main reason why the store wasn't being picked up by the state visitor as the _stores suffix is a requirement for automatic detection.

8 changes: 4 additions & 4 deletions localstack-core/localstack/services/events/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@
RuleDict,
TargetDict,
ValidationException,
events_store,
events_stores,
)
from localstack.services.events.models import (
InvalidEventPatternException as InternalInvalidEventPatternException,
Expand Down Expand Up @@ -1521,7 +1521,7 @@ def untag_resource(
def get_store(self, region: str, account_id: str) -> EventsStore:
"""Returns the events store for the account and region.
On first call, creates the default event bus for the account region."""
store = events_store[account_id][region]
store = events_stores[account_id][region]
# create default event bus for account region on first call
default_event_bus_name = "default"
if default_event_bus_name not in store.event_buses:
Expand Down Expand Up @@ -1577,7 +1577,7 @@ def create_event_bus_service(
event_source_name: Optional[EventSourceName],
tags: Optional[TagList],
) -> EventBusService:
event_bus_service = EventBusService(
event_bus_service = EventBusService.create_event_bus_service(
name,
region,
account_id,
Expand All @@ -1601,7 +1601,7 @@ def create_rule_service(
event_bus_name: Optional[EventBusName],
targets: Optional[TargetDict],
) -> RuleService:
rule_service = RuleService(
rule_service = RuleService.create_rule_service(
name,
region,
account_id,
Expand Down
49 changes: 28 additions & 21 deletions localstack-core/localstack/services/events/rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,16 @@ class RuleService:
managed_by: ManagedBy
rule: Rule

def __init__(
self,
def __init__(self, rule: Rule):
self.rule = rule
if rule.schedule_expression:
self.schedule_cron = self._get_schedule_cron(rule.schedule_expression)
else:
self.schedule_cron = None

@classmethod
def create_rule_service(
cls,
name: RuleName,
region: Optional[str] = None,
account_id: Optional[str] = None,
Expand All @@ -57,25 +65,23 @@ def __init__(
targets: Optional[TargetDict] = None,
managed_by: Optional[ManagedBy] = None,
):
self._validate_input(event_pattern, schedule_expression, event_bus_name)
if schedule_expression:
self.schedule_cron = self._get_schedule_cron(schedule_expression)
else:
self.schedule_cron = None
cls._validate_input(event_pattern, schedule_expression, event_bus_name)
# required to keep data and functionality separate for persistence
self.rule = Rule(
name,
region,
account_id,
schedule_expression,
event_pattern,
state,
description,
role_arn,
tags,
event_bus_name,
targets,
managed_by,
return cls(
Rule(
name,
region,
account_id,
schedule_expression,
event_pattern,
state,
description,
role_arn,
tags,
event_bus_name,
targets,
managed_by,
)
)

@property
Expand Down Expand Up @@ -178,8 +184,9 @@ def validate_targets_input(self, targets: TargetList) -> PutTargetsResultEntryLi

return validation_errors

@classmethod
def _validate_input(
self,
cls,
event_pattern: Optional[EventPattern],
schedule_expression: Optional[ScheduleExpression],
event_bus_name: Optional[EventBusName] = "default",
Expand Down
Loading
0