Ministack: Free, open-source local AWS emulator - 60+ services, Terraform compatible, real databases. Free forever. MIT licensed.
Python awsaws-emulatoraws-localaws-sdkdevtoolsdockerdynamodbec2emulatorlambdalocalstacklocalstack-alternativeministackmock-awsopen-sourcepythons3sqsterraform
feat(events): expand EventBridge API coverage (#210)
Mohammed Al-Dokimi50bf3a02da60
diff --git a/README.md b/README.mdindex 45de1d58..0678779b 100644--- a/README.md+++ b/README.md@@ -324,7 +324,7 @@ subnet = ec2.create_subnet(| Service | Operations | Notes ||---------|-----------|-------|| **SSM Parameter Store** | PutParameter, GetParameter, GetParameters, GetParametersByPath, DeleteParameter, DeleteParameters, DescribeParameters, GetParameterHistory, LabelParameterVersion, AddTagsToResource, RemoveTagsFromResource, ListTagsForResource | Supports String, SecureString, StringList |-| **EventBridge** | CreateEventBus, DeleteEventBus, ListEventBuses, DescribeEventBus, PutRule, DeleteRule, ListRules, DescribeRule, EnableRule, DisableRule, PutTargets, RemoveTargets, ListTargetsByRule, PutEvents, TagResource, UntagResource, ListTagsForResource, CreateArchive, DeleteArchive, DescribeArchive, ListArchives, PutPermission, RemovePermission, CreateConnection, DescribeConnection, DeleteConnection, UpdateConnection, ListConnections, CreateApiDestination, DescribeApiDestination, DeleteApiDestination, UpdateApiDestination, ListApiDestinations | Lambda target dispatch on PutEvents; S3 EventBridge notifications |+| **EventBridge** | CreateEventBus, UpdateEventBus, DeleteEventBus, ListEventBuses, DescribeEventBus, PutRule, DeleteRule, ListRules, DescribeRule, EnableRule, DisableRule, PutTargets, RemoveTargets, ListTargetsByRule, ListRuleNamesByTarget, PutEvents, TestEventPattern, TagResource, UntagResource, ListTagsForResource, CreateArchive, DeleteArchive, DescribeArchive, UpdateArchive, ListArchives, PutPermission, RemovePermission, CreateConnection, DescribeConnection, DeleteConnection, UpdateConnection, DeauthorizeConnection, ListConnections, CreateApiDestination, DescribeApiDestination, DeleteApiDestination, UpdateApiDestination, ListApiDestinations, StartReplay, DescribeReplay, ListReplays, CancelReplay, CreateEndpoint, DeleteEndpoint, DescribeEndpoint, ListEndpoints, UpdateEndpoint, ActivateEventSource, DeactivateEventSource, DescribeEventSource, CreatePartnerEventSource, DeletePartnerEventSource, DescribePartnerEventSource, ListPartnerEventSources, ListPartnerEventSourceAccounts, ListEventSources, PutPartnerEvents | Lambda target dispatch on PutEvents; S3 EventBridge notifications; replays and SaaS/partner APIs are control-plane stubs || **Kinesis** | CreateStream, DeleteStream, DescribeStream, DescribeStreamSummary, ListStreams, ListShards, PutRecord, PutRecords, GetShardIterator, GetRecords, IncreaseStreamRetentionPeriod, DecreaseStreamRetentionPeriod, MergeShards, SplitShard, UpdateShardCount, StartStreamEncryption, StopStreamEncryption, EnableEnhancedMonitoring, DisableEnhancedMonitoring, RegisterStreamConsumer, DeregisterStreamConsumer, ListStreamConsumers, DescribeStreamConsumer, AddTagsToStream, RemoveTagsFromStream, ListTagsForStream | Partition key → shard routing; AWS limits enforced (1 MB/record, 500 records/batch, 5 MB payload, 256-char partition key) || **CloudWatch Metrics** | PutMetricData, GetMetricStatistics, GetMetricData, ListMetrics, PutMetricAlarm, PutCompositeAlarm, DescribeAlarms, DescribeAlarmsForMetric, DescribeAlarmHistory, DeleteAlarms, SetAlarmState, EnableAlarmActions, DisableAlarmActions, TagResource, UntagResource, ListTagsForResource, PutDashboard, GetDashboard, DeleteDashboards, ListDashboards | CBOR and JSON protocol || **SES** | SendEmail, SendRawEmail, SendTemplatedEmail, SendBulkTemplatedEmail, VerifyEmailIdentity, VerifyEmailAddress, VerifyDomainIdentity, VerifyDomainDkim, ListIdentities, ListVerifiedEmailAddresses, GetIdentityVerificationAttributes, GetIdentityDkimAttributes, DeleteIdentity, GetSendQuota, GetSendStatistics, SetIdentityNotificationTopic, SetIdentityFeedbackForwardingEnabled, CreateConfigurationSet, DeleteConfigurationSet, DescribeConfigurationSet, ListConfigurationSets, CreateTemplate, GetTemplate, UpdateTemplate, DeleteTemplate, ListTemplates | Emails stored in-memory, not sent |diff --git a/ministack/services/eventbridge.py b/ministack/services/eventbridge.pyindex a75b33a7..36875b9e 100644--- a/ministack/services/eventbridge.py+++ b/ministack/services/eventbridge.py@@ -1,16 +1,23 @@"""EventBridge Service Emulator.-JSON-based API via X-Amz-Target (AmazonEventBridge).-Supports: CreateEventBus, DeleteEventBus, ListEventBuses, DescribeEventBus,+JSON-based API via X-Amz-Target (AmazonEventBridge / AWSEvents).+Supports: CreateEventBus, UpdateEventBus, DeleteEventBus, ListEventBuses, DescribeEventBus,PutRule, DeleteRule, ListRules, DescribeRule, EnableRule, DisableRule,- PutTargets, RemoveTargets, ListTargetsByRule,- PutEvents,+ PutTargets, RemoveTargets, ListTargetsByRule, ListRuleNamesByTarget,+ PutEvents, TestEventPattern,TagResource, UntagResource, ListTagsForResource,- CreateArchive, DeleteArchive, DescribeArchive, ListArchives,+ CreateArchive, DeleteArchive, DescribeArchive, UpdateArchive, ListArchives,PutPermission, RemovePermission,- CreateConnection, DescribeConnection, DeleteConnection, ListConnections, UpdateConnection,+ CreateConnection, DescribeConnection, DeleteConnection, ListConnections,+ UpdateConnection, DeauthorizeConnection,CreateApiDestination, DescribeApiDestination, DeleteApiDestination,- ListApiDestinations, UpdateApiDestination.+ ListApiDestinations, UpdateApiDestination,+ StartReplay, DescribeReplay, ListReplays, CancelReplay,+ CreateEndpoint, DeleteEndpoint, DescribeEndpoint, ListEndpoints, UpdateEndpoint,+ ActivateEventSource, DeactivateEventSource, DescribeEventSource,+ CreatePartnerEventSource, DeletePartnerEventSource, DescribePartnerEventSource,+ ListPartnerEventSources, ListPartnerEventSourceAccounts,+ ListEventSources, PutPartnerEvents."""import copy@@ -67,6 +74,10 @@ _archives = AccountScopedDict()_event_bus_policies = AccountScopedDict() # bus_name -> {Statement: [...]}_connections = AccountScopedDict() # connection_name -> {...}_api_destinations = AccountScopedDict() # destination_name -> {...}+_replays = AccountScopedDict() # replay_name -> replay record+_endpoints = AccountScopedDict() # endpoint name -> endpoint record+# Partner event sources: key "account|name" -> metadata (cross-account style API)+_partner_event_sources: dict = {}# ── Persistence ────────────────────────────────────────────@@ -77,6 +88,9 @@ def get_state():"rules": copy.deepcopy(_rules),"targets": copy.deepcopy(_targets),"tags": copy.deepcopy(_tags),+ "replays": copy.deepcopy(_replays),+ "endpoints": copy.deepcopy(_endpoints),+ "partner_event_sources": copy.deepcopy(_partner_event_sources),}@@ -87,6 +101,12 @@ def restore_state(data):_rules.update(data.get("rules", {}))_targets.update(data.get("targets", {}))_tags.update(data.get("tags", {}))+ _replays.update(data.get("replays", {}))+ _endpoints.update(data.get("endpoints", {}))+ pe = data.get("partner_event_sources")+ if pe is not None:+ _partner_event_sources.clear()+ _partner_event_sources.update(pe)for bus in _event_buses.values():if "CreationTime" in bus:@@ -98,6 +118,11 @@ def restore_state(data):if "CreationTime" in rule:rule["CreationTime"] = _coerce_timestamp(rule["CreationTime"])+ for rep in _replays.values():+ for tk in ("ReplayStartTime", "ReplayEndTime", "EventStartTime", "EventEndTime"):+ if tk in rep and rep[tk] is not None:+ rep[tk] = _coerce_timestamp(rep[tk])+_restored = load_state("eventbridge")if _restored:@@ -128,6 +153,8 @@ async def handle_request(method, path, headers, body, query_params):"PutTargets": _put_targets,"RemoveTargets": _remove_targets,"ListTargetsByRule": _list_targets_by_rule,+ "ListRuleNamesByTarget": _list_rule_names_by_target,+ "TestEventPattern": _test_event_pattern,"PutEvents": _put_events,"TagResource": _tag_resource,"UntagResource": _untag_resource,@@ -135,7 +162,27 @@ async def handle_request(method, path, headers, body, query_params):"CreateArchive": _create_archive,"DeleteArchive": _delete_archive,"DescribeArchive": _describe_archive,+ "UpdateArchive": _update_archive,"ListArchives": _list_archives,+ "StartReplay": _start_replay,+ "DescribeReplay": _describe_replay,+ "ListReplays": _list_replays,+ "CancelReplay": _cancel_replay,+ "CreateEndpoint": _create_endpoint,+ "DeleteEndpoint": _delete_endpoint,+ "DescribeEndpoint": _describe_endpoint,+ "ListEndpoints": _list_endpoints,+ "UpdateEndpoint": _update_endpoint,+ "ActivateEventSource": _activate_event_source,+ "DeactivateEventSource": _deactivate_event_source,+ "DescribeEventSource": _describe_event_source,+ "CreatePartnerEventSource": _create_partner_event_source,+ "DeletePartnerEventSource": _delete_partner_event_source,+ "DescribePartnerEventSource": _describe_partner_event_source,+ "ListPartnerEventSources": _list_partner_event_sources,+ "ListPartnerEventSourceAccounts": _list_partner_event_source_accounts,+ "ListEventSources": _list_event_sources,+ "PutPartnerEvents": _put_partner_events,"PutPermission": _put_permission,"RemovePermission": _remove_permission,"CreateConnection": _create_connection,@@ -143,6 +190,7 @@ async def handle_request(method, path, headers, body, query_params):"DeleteConnection": _delete_connection,"ListConnections": _list_connections,"UpdateConnection": _update_connection,+ "DeauthorizeConnection": _deauthorize_connection,"CreateApiDestination": _create_api_destination,"DescribeApiDestination": _describe_api_destination,"DeleteApiDestination": _delete_api_destination,@@ -444,6 +492,80 @@ def _list_targets_by_rule(data):return json_response({"Targets": targets})+def _list_rule_names_by_target(data):+ target_arn = data.get("TargetArn", "")+ if not target_arn:+ return error_response_json("ValidationException", "TargetArn is required", 400)+ bus_filter = data.get("EventBusName", "")+ limit = int(data.get("Limit", 100))+ if limit < 1:+ limit = 100+ if limit > 100:+ limit = 100+ next_token = data.get("NextToken", "")++ matched = []+ for key, tlist in _targets.items():+ bus_name, rule_name = key.split("|", 1) if "|" in key else ("default", key)+ if bus_filter and bus_name != bus_filter:+ continue+ if not any(t.get("Arn") == target_arn for t in tlist):+ continue+ if key in _rules:+ matched.append(_rules[key]["Name"])++ matched = sorted(set(matched))+ start = 0+ if next_token:+ try:+ start = int(next_token)+ except ValueError:+ start = 0+ page = matched[start:start + limit]+ resp = {"RuleNames": page}+ if start + limit < len(matched):+ resp["NextToken"] = str(start + limit)+ return json_response(resp)+++def _event_from_test_payload(event_obj: dict) -> dict:+ """Map CloudWatch Events-shaped JSON to internal fields used by _matches_pattern."""+ detail = event_obj.get("detail", event_obj.get("Detail", {}))+ if isinstance(detail, dict):+ detail = json.dumps(detail)+ elif detail is None:+ detail = "{}"+ else:+ detail = str(detail)+ return {+ "Source": event_obj.get("source", event_obj.get("Source", "")),+ "DetailType": event_obj.get("detail-type", event_obj.get("DetailType", "")),+ "Detail": detail,+ "Account": event_obj.get("account", event_obj.get("Account", get_account_id())),+ "Region": event_obj.get("region", event_obj.get("Region", REGION)),+ "Resources": event_obj.get("resources", event_obj.get("Resources", [])),+ }+++def _test_event_pattern(data):+ event_str = data.get("Event", "")+ pattern_str = data.get("EventPattern", "")+ if not event_str:+ return error_response_json("ValidationException", "Event is required", 400)+ if not pattern_str:+ return error_response_json("ValidationException", "EventPattern is required", 400)+ try:+ event_obj = json.loads(event_str) if isinstance(event_str, str) else event_str+ except (json.JSONDecodeError, TypeError):+ return error_response_json("InvalidEventPatternException", "Event is not valid JSON", 400)+ if not isinstance(event_obj, dict):+ return error_response_json("InvalidEventPatternException", "Event must be a JSON object", 400)++ synthetic = _event_from_test_payload(event_obj)+ matched = _matches_pattern(pattern_str, synthetic)+ return json_response({"Result": bool(matched)})++# ---------------------------------------------------------------------------# PutEvents + event pattern matching + target dispatch# ---------------------------------------------------------------------------@@ -847,6 +969,39 @@ def _describe_archive(data):return json_response(archive)+def _update_archive(data):+ name = data.get("ArchiveName")+ if not name:+ return error_response_json("ValidationException", "ArchiveName is required", 400)+ archive = _archives.get(name)+ if not archive:+ return error_response_json("ResourceNotFoundException", f"Archive {name} does not exist.", 400)++ if "Description" in data:+ archive["Description"] = data["Description"]+ if "EventPattern" in data:+ ep = data["EventPattern"]+ if isinstance(ep, str) and ep:+ try:+ json.loads(ep)+ except json.JSONDecodeError:+ return error_response_json(+ "InvalidEventPatternException",+ "Event pattern is not valid JSON",+ 400,+ )+ archive["EventPattern"] = ep+ if "RetentionDays" in data:+ archive["RetentionDays"] = int(data["RetentionDays"])++ archive["LastUpdatedTime"] = _now_ts()+ return json_response({+ "ArchiveArn": archive["ArchiveArn"],+ "State": archive.get("State", "ENABLED"),+ "CreationTime": archive["CreationTime"],+ })++def _list_archives(data):prefix = data.get("NamePrefix", "")source_arn = data.get("EventSourceArn", "")@@ -863,6 +1018,306 @@ def _list_archives(data):return json_response({"Archives": results})+# ---------------------------------------------------------------------------+# Replays (minimal control plane — no archive replay engine)+# ---------------------------------------------------------------------------++def _start_replay(data):+ name = data.get("ReplayName")+ if not name:+ return error_response_json("ValidationException", "ReplayName is required", 400)+ if name in _replays:+ return error_response_json(+ "ResourceAlreadyExistsException",+ f"Replay {name} already exists",+ 400,+ )+ dest = data.get("Destination") or {}+ if not dest.get("Arn"):+ return error_response_json(+ "ValidationException",+ "Destination.Arn is required",+ 400,+ )+ arn = f"arn:aws:events:{REGION}:{get_account_id()}:replay/{name}"+ now = _now_ts()+ _replays[name] = {+ "ReplayName": name,+ "ReplayArn": arn,+ "Description": data.get("Description", ""),+ "EventSourceArn": data.get("EventSourceArn", ""),+ "EventStartTime": data.get("EventStartTime", now),+ "EventEndTime": data.get("EventEndTime", now),+ "Destination": dest,+ "State": "RUNNING",+ "ReplayStartTime": now,+ }+ return json_response({"ReplayArn": arn, "State": "RUNNING"})+++def _describe_replay(data):+ name = data.get("ReplayName")+ if not name:+ return error_response_json("ValidationException", "ReplayName is required", 400)+ rep = _replays.get(name)+ if not rep:+ return error_response_json("ResourceNotFoundException", f"Replay {name} does not exist.", 400)+ return json_response(dict(rep))+++def _list_replays(data):+ prefix = data.get("NamePrefix", "")+ state_f = data.get("State", "")+ source_f = data.get("EventSourceArn", "")+ results = []+ for n in sorted(_replays.keys()):+ rep = _replays[n]+ if prefix and not n.startswith(prefix):+ continue+ if state_f and rep.get("State") != state_f:+ continue+ if source_f and rep.get("EventSourceArn") != source_f:+ continue+ results.append({+ "ReplayName": rep["ReplayName"],+ "ReplayArn": rep["ReplayArn"],+ "State": rep["State"],+ "EventSourceArn": rep.get("EventSourceArn", ""),+ "ReplayStartTime": rep.get("ReplayStartTime", ""),+ })+ return json_response({"Replays": results})+++def _cancel_replay(data):+ name = data.get("ReplayName")+ if not name:+ return error_response_json("ValidationException", "ReplayName is required", 400)+ rep = _replays.get(name)+ if not rep:+ return error_response_json("ResourceNotFoundException", f"Replay {name} does not exist.", 400)+ if rep["State"] == "COMPLETED":+ return error_response_json(+ "ValidationException",+ "Replay is already completed",+ 400,+ )+ if rep["State"] == "CANCELLED":+ return json_response({"ReplayArn": rep["ReplayArn"], "State": "CANCELLED"})+ rep["State"] = "CANCELLED"+ rep["ReplayEndTime"] = _now_ts()+ return json_response({"ReplayArn": rep["ReplayArn"], "State": "CANCELLED"})+++# ---------------------------------------------------------------------------+# Global endpoints + SaaS partner event sources (minimal / stub)+# ---------------------------------------------------------------------------++def _create_endpoint(data):+ name = data.get("Name")+ if not name:+ return error_response_json("ValidationException", "Name is required", 400)+ if name in _endpoints:+ return error_response_json("ResourceAlreadyExistsException",+ f"Endpoint {name} already exists", 400)+ arn = f"arn:aws:events:{REGION}:{get_account_id()}:endpoint/{name}"+ now = _now_ts()+ _endpoints[name] = {+ "Name": name,+ "Description": data.get("Description", ""),+ "RoutingConfig": data.get("RoutingConfig", {}),+ "ReplicationConfig": data.get("ReplicationConfig", {}),+ "EventBuses": data.get("EventBuses", []),+ "RoleArn": data.get("RoleArn", ""),+ "Arn": arn,+ "EndpointUrl": f"https://{name}.global-events.{REGION}.amazonaws.com",+ "State": "ACTIVE",+ "CreationTime": now,+ "LastModifiedTime": now,+ }+ ep = _endpoints[name]+ return json_response({+ "Name": ep["Name"],+ "Arn": ep["Arn"],+ "RoutingConfig": ep["RoutingConfig"],+ "ReplicationConfig": ep["ReplicationConfig"],+ "EventBuses": ep["EventBuses"],+ "RoleArn": ep["RoleArn"],+ "State": ep["State"],+ })+++def _delete_endpoint(data):+ name = data.get("Name")+ if name not in _endpoints:+ return error_response_json("ResourceNotFoundException",+ f"Endpoint {name} does not exist.", 400)+ del _endpoints[name]+ return json_response({})+++def _describe_endpoint(data):+ name = data.get("Name")+ ep = _endpoints.get(name)+ if not ep:+ return error_response_json("ResourceNotFoundException",+ f"Endpoint {name} does not exist.", 400)+ return json_response({+ "Name": ep["Name"],+ "Description": ep.get("Description", ""),+ "Arn": ep["Arn"],+ "RoutingConfig": ep.get("RoutingConfig", {}),+ "ReplicationConfig": ep.get("ReplicationConfig", {}),+ "EventBuses": ep.get("EventBuses", []),+ "RoleArn": ep.get("RoleArn", ""),+ "EndpointId": ep["Name"],+ "EndpointUrl": ep["EndpointUrl"],+ "State": ep["State"],+ "StateReason": "",+ "CreationTime": ep["CreationTime"],+ "LastModifiedTime": ep.get("LastModifiedTime", ep["CreationTime"]),+ })+++def _list_endpoints(data):+ prefix = data.get("NamePrefix", "")+ home = data.get("HomeRegion", "")+ results = []+ for n in sorted(_endpoints.keys()):+ ep = _endpoints[n]+ if prefix and not n.startswith(prefix):+ continue+ if home and REGION != home:+ continue+ results.append({+ "Name": ep["Name"],+ "Arn": ep["Arn"],+ "EndpointUrl": ep["EndpointUrl"],+ "State": ep["State"],+ "CreationTime": ep["CreationTime"],+ })+ return json_response({"Endpoints": results})+++def _update_endpoint(data):+ name = data.get("Name")+ if name not in _endpoints:+ return error_response_json("ResourceNotFoundException",+ f"Endpoint {name} does not exist.", 400)+ ep = _endpoints[name]+ now = _now_ts()+ for key in ("Description", "RoutingConfig", "ReplicationConfig", "EventBuses", "RoleArn"):+ if key in data:+ ep[key] = data[key]+ ep["LastModifiedTime"] = now+ return json_response({+ "Name": ep["Name"],+ "Arn": ep["Arn"],+ "RoutingConfig": ep["RoutingConfig"],+ "ReplicationConfig": ep["ReplicationConfig"],+ "EventBuses": ep["EventBuses"],+ "RoleArn": ep["RoleArn"],+ "EndpointId": ep["Name"],+ "EndpointUrl": ep["EndpointUrl"],+ "State": ep["State"],+ })+++def _activate_event_source(data):+ _ = data.get("Name", "")+ return json_response({})+++def _deactivate_event_source(data):+ _ = data.get("Name", "")+ return json_response({})+++def _describe_event_source(data):+ name = data.get("Name", "")+ return json_response({+ "Name": name,+ "State": "ENABLED",+ "Arn": f"arn:aws:events:{REGION}::event-source/{name}" if name else "",+ })+++def _partner_key(account: str, name: str) -> str:+ return f"{account}|{name}"+++def _create_partner_event_source(data):+ name = data.get("Name")+ account = data.get("Account", "")+ if not name or not account:+ return error_response_json("ValidationException", "Name and Account are required", 400)+ pk = _partner_key(account, name)+ if pk in _partner_event_sources:+ return error_response_json("ResourceAlreadyExistsException",+ "Partner event source already exists", 400)+ arn = f"arn:aws:events:{REGION}:{account}:event-source/{name}"+ _partner_event_sources[pk] = {+ "Name": name,+ "Account": account,+ "EventSourceArn": arn,+ }+ return json_response({"EventSourceArn": arn})+++def _delete_partner_event_source(data):+ name = data.get("Name")+ account = data.get("Account", "")+ pk = _partner_key(account, name)+ if pk not in _partner_event_sources:+ return error_response_json("ResourceNotFoundException",+ "Partner event source does not exist.", 400)+ del _partner_event_sources[pk]+ return json_response({})+++def _describe_partner_event_source(data):+ name = data.get("Name")+ for pk, rec in _partner_event_sources.items():+ if rec["Name"] == name:+ return json_response({+ "Name": rec["Name"],+ "Arn": rec["EventSourceArn"],+ "State": "ACTIVE",+ })+ return error_response_json("ResourceNotFoundException",+ f"Partner event source {name} does not exist.", 400)+++def _list_partner_event_sources(data):+ prefix = data.get("NamePrefix", "")+ results = []+ for rec in _partner_event_sources.values():+ if prefix and not rec["Name"].startswith(prefix):+ continue+ results.append({+ "Name": rec["Name"],+ "Arn": rec["EventSourceArn"],+ "State": "ACTIVE",+ })+ return json_response({"PartnerEventSources": results})+++def _list_partner_event_source_accounts(data):+ _ = data.get("EventSourceName", "")+ return json_response({"PartnerEventSourceAccounts": [], "NextToken": ""})+++def _list_event_sources(data):+ prefix = data.get("NamePrefix", "")+ _ = prefix+ return json_response({"EventSources": []})+++def _put_partner_events(data):+ entries = data.get("Entries", [])+ results = [{"EventId": new_uuid()} for _ in entries]+ return json_response({"FailedEntryCount": 0, "Entries": results})++# ---------------------------------------------------------------------------# Permissions (resource policies)# ---------------------------------------------------------------------------@@ -1007,6 +1462,25 @@ def _update_connection(data):})+def _deauthorize_connection(data):+ name = data.get("Name")+ if not name:+ return error_response_json("ValidationException", "Name is required", 400)+ conn = _connections.get(name)+ if not conn:+ return error_response_json("ResourceNotFoundException",+ f"Connection {name} does not exist.", 400)+ now = _now_ts()+ conn["ConnectionState"] = "DEAUTHORIZED"+ conn["LastModifiedTime"] = now+ conn.pop("LastAuthorizedTime", None)+ return json_response({+ "ConnectionArn": conn["ConnectionArn"],+ "ConnectionState": conn["ConnectionState"],+ "LastModifiedTime": now,+ })++# ---------------------------------------------------------------------------# API Destinations# ---------------------------------------------------------------------------@@ -1112,6 +1586,9 @@ def reset():_event_bus_policies.clear()_connections.clear()_api_destinations.clear()+ _replays.clear()+ _endpoints.clear()+ _partner_event_sources.clear()_event_buses = {"default": {"Name": "default",diff --git a/tests/test_eventbridge.py b/tests/test_eventbridge.pyindex fb1442c4..bc1cd8ab 100644--- a/tests/test_eventbridge.py+++ b/tests/test_eventbridge.py@@ -53,6 +53,77 @@ def test_eventbridge_targets(eb):resp = eb.list_targets_by_rule(Rule="target-rule")assert len(resp["Targets"]) == 1++def test_eventbridge_list_rule_names_by_target(eb):+ fn_arn = "arn:aws:lambda:us-east-1:000000000000:function:list-by-tgt-fn"+ eb.create_event_bus(Name="lrt-bus")+ eb.put_rule(+ Name="rule-a",+ EventBusName="lrt-bus",+ EventPattern=json.dumps({"source": ["my.app"]}),+ State="ENABLED",+ )+ eb.put_rule(+ Name="rule-b",+ EventBusName="lrt-bus",+ EventPattern=json.dumps({"source": ["other.app"]}),+ State="ENABLED",+ )+ eb.put_targets(+ Rule="rule-a",+ EventBusName="lrt-bus",+ Targets=[{"Id": "t1", "Arn": fn_arn}],+ )+ eb.put_targets(+ Rule="rule-b",+ EventBusName="lrt-bus",+ Targets=[{"Id": "t1", "Arn": fn_arn}],+ )+ out = eb.list_rule_names_by_target(TargetArn=fn_arn, EventBusName="lrt-bus")+ assert sorted(out["RuleNames"]) == ["rule-a", "rule-b"]+++def test_eventbridge_test_event_pattern_match(eb):+ event = json.dumps({+ "source": "orders.service",+ "detail-type": "Order Placed",+ "detail": {"orderId": "42", "amount": 10},+ })+ pattern = json.dumps({+ "source": ["orders.service"],+ "detail-type": ["Order Placed"],+ })+ r = eb.test_event_pattern(Event=event, EventPattern=pattern)+ assert r["Result"] is True+++def test_eventbridge_test_event_pattern_no_match(eb):+ event = json.dumps({"source": "other", "detail-type": "X", "detail": {}})+ pattern = json.dumps({"source": ["orders.service"]})+ r = eb.test_event_pattern(Event=event, EventPattern=pattern)+ assert r["Result"] is False+++def test_eventbridge_test_event_pattern_invalid_event(eb):+ with pytest.raises(ClientError) as exc:+ eb.test_event_pattern(Event="not-json", EventPattern="{}")+ assert exc.value.response["Error"]["Code"] == "InvalidEventPatternException"+++def test_eventbridge_list_rule_names_by_target_pagination(eb):+ fn_arn = "arn:aws:lambda:us-east-1:000000000000:function:page-fn"+ eb.put_rule(Name="r1", ScheduleExpression="rate(1 hour)", State="ENABLED")+ eb.put_rule(Name="r2", ScheduleExpression="rate(1 hour)", State="ENABLED")+ eb.put_targets(Rule="r1", Targets=[{"Id": "1", "Arn": fn_arn}])+ eb.put_targets(Rule="r2", Targets=[{"Id": "1", "Arn": fn_arn}])+ p1 = eb.list_rule_names_by_target(TargetArn=fn_arn, Limit=1)+ assert len(p1["RuleNames"]) == 1+ assert "NextToken" in p1+ p2 = eb.list_rule_names_by_target(TargetArn=fn_arn, Limit=1, NextToken=p1["NextToken"])+ assert len(p2["RuleNames"]) == 1+ assert p1["RuleNames"][0] != p2["RuleNames"][0]++def test_eventbridge_permission(eb):eb.create_event_bus(Name="perm-bus")eb.put_permission(@@ -74,6 +145,20 @@ def test_eventbridge_connection(eb):assert desc["Name"] == "test-conn"eb.delete_connection(Name="test-conn")++def test_eventbridge_deauthorize_connection(eb):+ eb.create_connection(+ Name="deauth-conn",+ AuthorizationType="API_KEY",+ AuthParameters={"ApiKeyAuthParameters": {"ApiKeyName": "k", "ApiKeyValue": "v"}},+ )+ out = eb.deauthorize_connection(Name="deauth-conn")+ assert out["ConnectionState"] == "DEAUTHORIZED"+ desc = eb.describe_connection(Name="deauth-conn")+ assert desc["ConnectionState"] == "DEAUTHORIZED"+ eb.delete_connection(Name="deauth-conn")++def test_eventbridge_api_destination(eb):eb.create_connection(Name="apid-conn",@@ -292,6 +377,107 @@ def test_eventbridge_archive(eb):archives2 = eb.list_archives()assert not any(a["ArchiveName"] == archive_name for a in archives2["Archives"])++def test_eventbridge_endpoints_and_partner_stubs(eb):+ eb.create_endpoint(+ Name="my-global-endpoint",+ Description="stub",+ RoleArn="arn:aws:iam::000000000000:role/r",+ RoutingConfig={+ "FailoverConfig": {+ "Primary": {"HealthCheck": "arn:aws:route53:::healthcheck/primary"},+ "Secondary": {"Route": "secondary-route"},+ }+ },+ EventBuses=[+ {"EventBusArn": "arn:aws:events:us-east-1:000000000000:event-bus/default"},+ {"EventBusArn": "arn:aws:events:us-east-1:000000000000:event-bus/backup"},+ ],+ )+ d = eb.describe_endpoint(Name="my-global-endpoint")+ assert d["State"] == "ACTIVE"+ assert "Arn" in d+ lst = eb.list_endpoints()+ assert any(e["Name"] == "my-global-endpoint" for e in lst["Endpoints"])+ eb.update_endpoint(Name="my-global-endpoint", Description="updated")+ eb.delete_endpoint(Name="my-global-endpoint")++ eb.activate_event_source(Name="aws.partner/saas/foo")+ eb.deactivate_event_source(Name="aws.partner/saas/foo")+ src = eb.describe_event_source(Name="aws.partner/saas/foo")+ assert src["State"] == "ENABLED"++ r = eb.create_partner_event_source(Name="saas.src", Account="111111111111")+ assert "EventSourceArn" in r+ eb.describe_partner_event_source(Name="saas.src")+ pl = eb.list_partner_event_sources(NamePrefix="saas")+ assert len(pl["PartnerEventSources"]) >= 1+ eb.delete_partner_event_source(Name="saas.src", Account="111111111111")++ acc = eb.list_partner_event_source_accounts(EventSourceName="x")+ assert acc["PartnerEventSourceAccounts"] == []++ es = eb.list_event_sources()+ assert es["EventSources"] == []++ pe = eb.put_partner_events(Entries=[{"Source": "p", "DetailType": "t", "Detail": "{}"}])+ assert pe["FailedEntryCount"] == 0+++def test_eventbridge_replay_lifecycle(eb):+ arch = f"replay-arch-{_uuid_mod.uuid4().hex[:8]}"+ eb.create_archive(+ ArchiveName=arch,+ EventSourceArn="arn:aws:events:us-east-1:000000000000:event-bus/default",+ )+ archive_arn = eb.describe_archive(ArchiveName=arch)["ArchiveArn"]+ rep_name = f"replay-{_uuid_mod.uuid4().hex[:8]}"+ src = "arn:aws:events:us-east-1:000000000000:event-bus/default"+ from datetime import datetime, timezone++ t0 = datetime(2024, 1, 1, tzinfo=timezone.utc)+ t1 = datetime(2024, 1, 2, tzinfo=timezone.utc)+ start = eb.start_replay(+ ReplayName=rep_name,+ EventSourceArn=src,+ EventStartTime=t0,+ EventEndTime=t1,+ Destination={"Arn": archive_arn},+ )+ assert start["State"] == "RUNNING"+ desc = eb.describe_replay(ReplayName=rep_name)+ assert desc["ReplayName"] == rep_name+ assert desc["State"] == "RUNNING"+ listed = eb.list_replays(NamePrefix=rep_name)+ assert any(r["ReplayName"] == rep_name for r in listed["Replays"])+ cancel = eb.cancel_replay(ReplayName=rep_name)+ assert cancel["State"] == "CANCELLED"+ desc2 = eb.describe_replay(ReplayName=rep_name)+ assert desc2["State"] == "CANCELLED"+ eb.delete_archive(ArchiveName=arch)+++def test_eventbridge_update_archive(eb):+ name = f"upd-archive-{_uuid_mod.uuid4().hex[:8]}"+ eb.create_archive(+ ArchiveName=name,+ EventSourceArn="arn:aws:events:us-east-1:000000000000:event-bus/default",+ Description="old",+ RetentionDays=1,+ )+ eb.update_archive(+ ArchiveName=name,+ Description="new desc",+ RetentionDays=30,+ EventPattern=json.dumps({"source": ["app"]}),+ )+ desc = eb.describe_archive(ArchiveName=name)+ assert desc["Description"] == "new desc"+ assert desc["RetentionDays"] == 30+ assert "app" in desc["EventPattern"]+ eb.delete_archive(ArchiveName=name)++def test_eventbridge_put_remove_permission(eb):import uuid as _uuid