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

Refactor: Events v2: Move existing provider code to v1 folder #10730

Merged
merged 4 commits into from
May 8, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion localstack/services/events/event_bus.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import Optional

from localstack.aws.api.events import Arn, EventBusName, TagList
from localstack.services.events.models_v2 import EventBus, RuleDict
from localstack.services.events.models import EventBus, RuleDict


class EventBusService:
Expand Down
2 changes: 1 addition & 1 deletion localstack/services/events/event_ruler.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
from functools import cache
from pathlib import Path

from localstack.services.events.models import InvalidEventPatternException
from localstack.services.events.packages import event_ruler_package
from localstack.services.events.utils import InvalidEventPatternException
from localstack.utils.objects import singleton_factory

THIS_FOLDER = os.path.dirname(os.path.realpath(__file__))
Expand Down
103 changes: 98 additions & 5 deletions localstack/services/events/models.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,104 @@
from typing import Dict
from dataclasses import dataclass, field
from typing import Optional

from localstack.services.stores import AccountRegionBundle, BaseStore, LocalAttribute
from localstack.aws.api.core import ServiceException
from localstack.aws.api.events import (
Arn,
CreatedBy,
EventBusName,
EventPattern,
ManagedBy,
RoleArn,
RuleDescription,
RuleName,
RuleState,
ScheduleExpression,
TagList,
Target,
TargetId,
)
from localstack.services.stores import (
AccountRegionBundle,
BaseStore,
LocalAttribute,
)

TargetDict = dict[TargetId, Target]


@dataclass
class Rule:
name: RuleName
region: str
account_id: str
schedule_expression: Optional[ScheduleExpression] = None
event_pattern: Optional[EventPattern] = None
state: Optional[RuleState] = None
description: Optional[RuleDescription] = None
role_arn: Optional[RoleArn] = None
tags: TagList = field(default_factory=list)
event_bus_name: EventBusName = "default"
targets: TargetDict = field(default_factory=dict)
managed_by: Optional[ManagedBy] = None # can only be set by AWS services
created_by: CreatedBy = field(init=False)
arn: Arn = field(init=False)

def __post_init__(self):
if self.event_bus_name == "default":
self.arn = f"arn:aws:events:{self.region}:{self.account_id}:rule/{self.name}"
else:
self.arn = f"arn:aws:events:{self.region}:{self.account_id}:rule/{self.event_bus_name}/{self.name}"
self.created_by = self.account_id
if self.tags is None:
self.tags = []
if self.targets is None:
self.targets = {}
if self.state is None:
self.state = RuleState.ENABLED


RuleDict = dict[RuleName, Rule]


@dataclass
class EventBus:
name: EventBusName
region: str
account_id: str
event_source_name: Optional[str] = None
tags: TagList = field(default_factory=list)
policy: Optional[str] = None
rules: RuleDict = field(default_factory=dict)
arn: Arn = field(init=False)

def __post_init__(self):
self.arn = f"arn:aws:events:{self.region}:{self.account_id}:event-bus/{self.name}"
if self.rules is None:
self.rules = {}
if self.tags is None:
self.tags = []


EventBusDict = dict[EventBusName, EventBus]


class EventsStore(BaseStore):
# maps rule name to job_id
rule_scheduled_jobs: Dict[str, str] = LocalAttribute(default=dict)
# Map of eventbus names to eventbus objects. The name MUST be unique per account and region (works with AccountRegionBundle)
event_buses: EventBusDict = LocalAttribute(default=dict)


events_store = AccountRegionBundle("events", EventsStore)


class ValidationException(ServiceException):
code: str = "ValidationException"
sender_fault: bool = True
status_code: int = 400


class InvalidEventPatternException(Exception):
reason: str

events_stores = AccountRegionBundle("events", EventsStore)
def __init__(self, reason=None, message=None) -> None:
self.reason = reason
self.message = message or f"Event pattern is not valid. Reason: {reason}"
96 changes: 0 additions & 96 deletions localstack/services/events/models_v2.py

This file was deleted.