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

feat: Improved AWS backup notification readability #222

Merged
merged 10 commits into from
Apr 24, 2024
78 changes: 78 additions & 0 deletions functions/notify_slack.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,80 @@ def format_aws_health(message: Dict[str, Any], region: str) -> Dict[str, Any]:
}


def aws_backup_field_parser(message: str) -> Dict[str, Any]:
"""
Parser for AWS Backup event message. It extracts the fields from the message and returns a dictionary.

:params message: message containing AWS Backup event
:returns: dictionary containing the fields extracted from the message
"""
field_names = ["Recovery point ARN", "Resource ARN", "BackupJob ID", "Backup Job Id", "Backed up Resource ARN", "Status Message"]

first = None
field_match = None

for field in field_names:
index = message.find(field)
if index != -1 and (first == None or index < first):
first = index
field_match = field

if first is not None:
next = None
for field in field_names:
index = message.find(field, first + len(field_match) + 1)
if index != -1 and (next == None or index < next):
next = index

if next is None:
next = len(message)

rem_message = message[next:]
fields = aws_backup_field_parser(rem_message)

text = message[first+len(field_match)+1:next]
while text.startswith(" ") or text.startswith(":"):
text = text[1:]

fields[field_match] = text
return fields
else:
return {}


def format_aws_backup(message: Dict[str, Any]) -> Dict[str, Any]:
"""
Format AWS Backup event into Slack message format

:params message: SNS message body containing AWS Backup event
:returns: formatted Slack message payload
"""

fields = []
attachments = {}

title = message.split(".")[0]


if "failed" in title:
title = title + " ⚠️"

if "completed" in title:
title = title + " ✅"

fields.append({"title": title})

list_items = aws_backup_field_parser(message)

for k,v in list_items.items():
fields.append({"value": k , "short": False})
fields.append({"value": "```\n" + v +"\n```" , "short": False})

attachments["fields"] = fields # type: ignore

return attachments


def format_default(
message: Union[str, Dict], subject: Optional[str] = None
) -> Dict[str, Any]:
Expand Down Expand Up @@ -344,6 +418,10 @@ def get_slack_message_payload(
notification = format_aws_health(message=message, region=message["region"])
attachment = notification

elif subject == "Notification from AWS Backup":
notification = format_aws_backup(message=message)
attachment = notification

elif "attachments" in message or "text" in message:
payload = {**payload, **message}

Expand Down
Loading