<gh_stars>0
import os
import json
import logging
import requests

logger = logging.getLogger('slack')
logger.setLevel(logging.INFO)

def notify(url, account, userName, existingAccessKey, accessKey=None, secretKey=None, instruction=None, deleteAfterDays=None):
    if accessKey is not None:
        # New key pair generated
        logger.info('Sending notification to {} about new access key generation via {}'.format(userName, url))
        msg = {
            "blocks": [
                {
                    "type": "section",
                    "text": {
                        "type": "mrkdwn",
                        "text": ":mega: NEW KEY PAIR GENERATED FOR *{}* :mega:".format(userName)
                    }
                },
                {
                    "type": "section",
                    "fields": [
                        {
                            "type": "mrkdwn",
                            "text": "*Account ID:*\n{}".format(account['id'])
                        },
                        {
                            "type": "mrkdwn",
                            "text": "*Account Name:*\n{}".format(account['name'])
                        }
                    ]
                },
                {
                    "type": "section",
                    "fields": [
                        {
                            "type": "mrkdwn",
                            "text": "*Access Key:*\n{}".format(accessKey)
                        },
                        {
                            "type": "mrkdwn",
                            "text": "*Secret Key:*\n{}".format(secretKey)
                        }
                    ]
                },
                {
                    "type": "section",
                    "text": {
                        "type": "mrkdwn",
                        "text": "*Instruction:* {}".format(instruction)
                    }
                },
                {
                  "type": "divider"
                },
                {
                    "type": "section",
                    "text": {
                        "type": "mrkdwn",
                        "text": "*NOTE:* Existing key pair *{}* will be deleted after {} days so please update the new key pair wherever required".format(existingAccessKey, deleteAfterDays)
                    }
                },
            ]
        }
    else:
        # Old key pair is deleted
        logger.info('Sending notification to {} about deletion of old access key via {}'.format(userName, url))
        msg = {
            "blocks": [
                {
                    "type": "section",
                    "text": {
                        "type": "mrkdwn",
                        "text": ":mega: OLD KEY PAIR DELETED :mega:".format(userName)
                    }
                },
                {
                    "type": "section",
                    "fields": [
                        {
                            "type": "mrkdwn",
                            "text": "*Account ID:*\n{}".format(account['id'])
                        },
                        {
                            "type": "mrkdwn",
                            "text": "*Account Name:*\n{}".format(account['name'])
                        }
                    ]
                },
                {
                    "type": "section",
                    "fields": [
                        {
                            "type": "mrkdwn",
                            "text": "*User:*\n{}".format(userName)
                        },
                        {
                            "type": "mrkdwn",
                            "text": "*Old Access Key:*\n{}".format(existingAccessKey)
                        }
                    ]
                }
            ]
        }

    resp = requests.post(url=url, json=msg)
    if resp.status_code == 200:
        logger.info('Notification sent to {} about key deletion via {}'.format(userName, url))
    else:
        logger.error('Notificaiton failed with {} status code. Reason: {}'.format(resp.status_code, resp.text))

import os
import json
import logging
import requests

logger = logging.getLogger('slack')
logger.setLevel(logging.INFO)

def notify(url, account, userName, existingAccessKey, accessKey=None, secretKey=None, instruction=None, deleteAfterDays=None):
    if accessKey is not None:
        # New key pair generated
        logger.info('Sending notification to {} about new access key generation via {}'.format(userName, url))
        msg = {
            "blocks": [
                {
                    "type": "section",
                    "text": {
                        "type": "mrkdwn",
                        "text": ":mega: NEW KEY PAIR GENERATED FOR *{}* :mega:".format(userName)
                    }
                },
                {
                    "type": "section",
                    "fields": [
                        {
                            "type": "mrkdwn",
                            "text": "*Account ID:*\n{}".format(account['id'])
                        },
                        {
                            "type": "mrkdwn",
                            "text": "*Account Name:*\n{}".format(account['name'])
                        }
                    ]
                },
                {
                    "type": "section",
                    "fields": [
                        {
                            "type": "mrkdwn",
                            "text": "*Access Key:*\n{}".format(accessKey)
                        },
                        {
                            "type": "mrkdwn",
                            "text": "*Secret Key:*\n{}".format(secretKey)
                        }
                    ]
                },
                {
                    "type": "section",
                    "text": {
                        "type": "mrkdwn",
                        "text": "*Instruction:* {}".format(instruction)
                    }
                },
                {
                  "type": "divider"
                },
                {
                    "type": "section",
                    "text": {
                        "type": "mrkdwn",
                        "text": "*NOTE:* Existing key pair *{}* will be deleted after {} days so please update the new key pair wherever required".format(existingAccessKey, deleteAfterDays)
                    }
                },
            ]
        }
    else:
        # Old key pair is deleted
        logger.info('Sending notification to {} about deletion of old access key via {}'.format(userName, url))
        msg = {
            "blocks": [
                {
                    "type": "section",
                    "text": {
                        "type": "mrkdwn",
                        "text": ":mega: OLD KEY PAIR DELETED :mega:".format(userName)
                    }
                },
                {
                    "type": "section",
                    "fields": [
                        {
                            "type": "mrkdwn",
                            "text": "*Account ID:*\n{}".format(account['id'])
                        },
                        {
                            "type": "mrkdwn",
                            "text": "*Account Name:*\n{}".format(account['name'])
                        }
                    ]
                },
                {
                    "type": "section",
                    "fields": [
                        {
                            "type": "mrkdwn",
                            "text": "*User:*\n{}".format(userName)
                        },
                        {
                            "type": "mrkdwn",
                            "text": "*Old Access Key:*\n{}".format(existingAccessKey)
                        }
                    ]
                }
            ]
        }

    resp = requests.post(url=url, json=msg)
    if resp.status_code == 200:
        logger.info('Notification sent to {} about key deletion via {}'.format(userName, url))
    else:
        logger.error('Notificaiton failed with {} status code. Reason: {}'.format(resp.status_code, resp.text))
# New key pair generated # Old key pair is deleted