moto/moto/events/notifications.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

72 lines
2.1 KiB
Python
Raw Normal View History

import json
2023-03-05 09:27:17 -01:00
from typing import Any, Dict
2023-03-05 09:27:17 -01:00
_EVENT_S3_OBJECT_CREATED: Dict[str, Any] = {
"version": "0",
"id": "17793124-05d4-b198-2fde-7ededc63b103",
"detail-type": "Object Created",
"source": "aws.s3",
"account": "123456789012",
"time": "2021-11-12T00:00:00Z",
"region": None,
"resources": [],
"detail": None,
}
2023-03-05 09:27:17 -01:00
def send_notification(
source: str, event_name: str, region: str, resources: Any, detail: Any
) -> None:
try:
_send_safe_notification(source, event_name, region, resources, detail)
except: # noqa
# If anything goes wrong, we should never fail
pass
2023-03-05 09:27:17 -01:00
def _send_safe_notification(
source: str, event_name: str, region: str, resources: Any, detail: Any
) -> None:
from .models import events_backends
event = None
if source == "aws.s3" and event_name == "CreateBucket":
event = _EVENT_S3_OBJECT_CREATED.copy()
event["region"] = region
event["resources"] = resources
event["detail"] = detail
if event is None:
return
2022-08-13 09:49:43 +00:00
for account_id, account in events_backends.items():
for backend in account.values():
applicable_targets = []
for rule in backend.rules.values():
if rule.state != "ENABLED":
continue
pattern = rule.event_pattern.get_pattern()
if source in pattern.get("source", []):
if event_name in pattern.get("detail", {}).get("eventName", []):
applicable_targets.extend(rule.targets)
2022-08-13 09:49:43 +00:00
for target in applicable_targets:
if target.get("Arn", "").startswith("arn:aws:lambda"):
_invoke_lambda(account_id, target.get("Arn"), event=event)
2023-03-05 09:27:17 -01:00
def _invoke_lambda(account_id: str, fn_arn: str, event: Any) -> None:
from moto.awslambda import lambda_backends
lmbda_region = fn_arn.split(":")[3]
body = json.dumps(event)
2022-08-13 09:49:43 +00:00
lambda_backends[account_id][lmbda_region].invoke(
function_name=fn_arn,
qualifier=None,
body=body,
headers=dict(),
response_headers=dict(),
)