Exemplo n.º 1
0
def prepare_slack_payload(alarm, bitly_access_token, sess=None):
    if is_critical_error(alarm_name=alarm.name):
        slack_data = {
            'username': '******',
            'icon_emoji': ':rotating_light:',
        }
        alarm_color = 'danger'
    else:
        slack_data = {
            'username': '******',
            'icon_emoji': ':warning:',
        }
        alarm_color = 'warning'

    slack_data['attachments'] = [{
        'color':
        alarm_color,
        'fallback':
        alarm.name,
        'title':
        alarm.name,
        'fields': [{
            'value':
            get_human_message(alarm_name=alarm.name,
                              state_reason=alarm.state_reason)
        }]
    }]

    if alarm.name == 'snapshot_scheduler_queue_not_empty':
        latest_snapshots_str = get_snapshot_report()
        if latest_snapshots_str is not None:
            slack_data['attachments'][0]['fields'].append({
                'title':
                'Latest snapshots',
                'value':
                latest_snapshots_str
            })

    messages = alarm.cloudwatch_messages()
    if messages:
        cloudwatch_message_str = '\n'.join(
            set([simplify_message(m) for m in messages]))
        slack_data['attachments'][0]['fields'].append({
            'title':
            'CloudWatch messages',
            'value':
            cloudwatch_message_str
        })

    cloudwatch_urls = alarm.cloudwatch_urls()
    if cloudwatch_urls:
        sess = sess or requests.Session()
        cloudwatch_url_str = ' / '.join([
            to_bitly(sess=sess, url=url, access_token=bitly_access_token)
            for url in cloudwatch_urls
        ])
        slack_data['attachments'][0]['fields'].append(
            {'value': cloudwatch_url_str})

    return slack_data
def prepare_slack_payload(alarm, bitly_access_token, sess=None):
    if is_critical_error(alarm_name=alarm.name):
        slack_data = {
            "username": "******",
            "icon_emoji": ":rotating_light:"
        }
        alarm_color = "danger"
    else:
        slack_data = {
            "username": "******",
            "icon_emoji": ":warning:"
        }
        alarm_color = "warning"

    slack_data["attachments"] = [{
        "color":
        alarm_color,
        "fallback":
        alarm.name,
        "title":
        alarm.name,
        "fields": [{
            "value":
            get_human_message(alarm_name=alarm.name,
                              state_reason=alarm.state_reason)
        }],
    }]

    messages = alarm.cloudwatch_messages()
    if messages:
        cloudwatch_message_str = "\n".join(
            set([simplify_message(m) for m in messages]))
        slack_data["attachments"][0]["fields"].append({
            "title":
            "CloudWatch messages",
            "value":
            cloudwatch_message_str
        })

    cloudwatch_urls = alarm.cloudwatch_urls()
    if cloudwatch_urls:
        sess = sess or requests.Session()
        cloudwatch_url_str = " / ".join([
            to_bitly(sess=sess, url=url, access_token=bitly_access_token)
            for url in cloudwatch_urls
        ])
        slack_data["attachments"][0]["fields"].append(
            {"value": cloudwatch_url_str})

    return slack_data
def test_is_critical_error(alarm_name, expected_is_critical_error):
    assert is_critical_error(alarm_name) == expected_is_critical_error