# Path: doorman/plugins/alerters/base.py
# class AbstractAlerterPlugin(with_metaclass(ABCMeta)):
#     """
#     AbstractAlerterPlugin is the base class for all alerters in Doorman. It
#     defines the interface that an alerter should implement in order to support
#     sending an alert.
#     """
#     @abstractmethod
#     def handle_alert(self, node, match):
#         raise NotImplementedError()


import json
import logging
import requests
from .base import AbstractAlerterPlugin

class SlackAlerter(AbstractAlerterPlugin):
    def __init__(self, config):
        # Required configuration
        self.slack_webhook = config['slack_webhook']

        # Optional
        self.printColumns = config.get('printColumns', False)
        self.color = config.get('color', DEFAULT_COLOR)

        # Other
        self.logger = logging.getLogger(__name__ + '.SlackAlerter')

    def handle_alert(self, node, match):
        fields = [
            {'title' : 'Node', 'value' : node['display_name']},
            {'title' : 'Description', 'value' : match.rule.description},
            {'title' : 'Action', 'value' : match.result['action']},
        ]

        if self.printColumns:
            columns = []
            for key in match.result['columns']:
                columns.append({"title" : key, 'value' : match.result['columns'][key]})
            fields.extend(columns)

        attachments = [{
            'pretext' : 'Doorman Alert: *%s*' % match.rule.name,
            'fallback' : 'Doorman Alert: %s' % match.rule.name,
            'color' : self.color,

# Path: doorman/plugins/alerters/base.py
# class AbstractAlerterPlugin(with_metaclass(ABCMeta)):
#     """
#     AbstractAlerterPlugin is the base class for all alerters in Doorman. It
#     defines the interface that an alerter should implement in order to support
#     sending an alert.
#     """
#     @abstractmethod
#     def handle_alert(self, node, match):
#         raise NotImplementedError()


# Path: doorman/plugins/alerters/slack.py
import json
import logging
import requests
from .base import AbstractAlerterPlugin


class SlackAlerter(AbstractAlerterPlugin):
    def __init__(self, config):
        # Required configuration
        self.slack_webhook = config['slack_webhook']

        # Optional
        self.printColumns = config.get('printColumns', False)
        self.color = config.get('color', DEFAULT_COLOR)

        # Other
        self.logger = logging.getLogger(__name__ + '.SlackAlerter')

    def handle_alert(self, node, match):
        fields = [
            {'title' : 'Node', 'value' : node['display_name']},
            {'title' : 'Description', 'value' : match.rule.description},
            {'title' : 'Action', 'value' : match.result['action']},
        ]

        if self.printColumns:
            columns = []
            for key in match.result['columns']:
                columns.append({"title" : key, 'value' : match.result['columns'][key]})
            fields.extend(columns)

        attachments = [{
            'pretext' : 'Doorman Alert: *%s*' % match.rule.name,
            'fallback' : 'Doorman Alert: %s' % match.rule.name,
            'color' : self.color,

            'fields': fields,