Пример #1
0
 def _deliver_slack_message(self, queue_message):
     from c7n_mailer.slack_delivery import SlackDelivery
     slack_delivery = SlackDelivery(self.config,
                                    self.logger,
                                    SendGridDelivery(self.config, self.session, self.logger))
     slack_messages = slack_delivery.get_to_addrs_slack_messages_map(queue_message)
     try:
         self.logger.info('Sending message to Slack.')
         slack_delivery.slack_handler(queue_message, slack_messages)
     except Exception as error:
         self.logger.exception(error)
Пример #2
0
    def test_slack_handler(self, mock_post):
        mock_post.return_value.status_code = 200
        mock_post.return_value.json.return_value = {'ok': True}

        slack = SlackDelivery(self.config, self.logger, self.email_delivery)
        result = slack.get_to_addrs_slack_messages_map(self.message)
        slack.slack_handler(self.message, result)

        self.logger.info.assert_called_with("Sending account:core-services-dev "
                                            "policy:ebs-mark-unattached-deletion ebs:1 slack:slack"
                                            "_default to test-channel")
Пример #3
0
    def process_azure_queue_message(self, encoded_azure_queue_message):
        queue_message = json.loads(
            zlib.decompress(base64.b64decode(encoded_azure_queue_message.content)))

        self.logger.debug("Got account:%s message:%s %s:%d policy:%s recipients:%s" % (
            queue_message.get('account', 'na'),
            encoded_azure_queue_message.id,
            queue_message['policy']['resource'],
            len(queue_message['resources']),
            queue_message['policy']['name'],
            ', '.join(queue_message['action'].get('to'))))

        if any(e.startswith('slack') or e.startswith('https://hooks.slack.com/')
                for e in queue_message.get('action', ()).get('to')):
            from c7n_mailer.slack_delivery import SlackDelivery
            slack_delivery = SlackDelivery(self.config,
                                           self.logger,
                                           SendGridDelivery(self.config, self.logger))
            slack_messages = slack_delivery.get_to_addrs_slack_messages_map(queue_message)
            try:
                slack_delivery.slack_handler(queue_message, slack_messages)
            except Exception:
                traceback.print_exc()
                pass

        # this section gets the map of metrics to send to datadog and delivers it
        if any(e.startswith('datadog') for e in queue_message.get('action', ()).get('to')):
            from c7n_mailer.datadog_delivery import DataDogDelivery
            datadog_delivery = DataDogDelivery(self.config, self.session, self.logger)
            datadog_message_packages = datadog_delivery.get_datadog_message_packages(queue_message)

            try:
                datadog_delivery.deliver_datadog_messages(datadog_message_packages, queue_message)
            except Exception:
                traceback.print_exc()
                pass

        # this section sends a notification to the resource owner via SendGrid
        try:
            sendgrid_delivery = SendGridDelivery(self.config, self.logger)
            email_messages = sendgrid_delivery.get_to_addrs_sendgrid_messages_map(queue_message)

            if 'smtp_server' in self.config:
                smtp_delivery = SmtpDelivery(config=self.config,
                                             session=self.session,
                                             logger=self.logger)
                for to_addrs, message in six.iteritems(email_messages):
                    smtp_delivery.send_message(message=message, to_addrs=list(to_addrs))
            else:
                return sendgrid_delivery.sendgrid_handler(queue_message, email_messages)
        except Exception:
            traceback.print_exc()

        return True
    def process_azure_queue_message(self, encoded_azure_queue_message):
        queue_message = json.loads(
            zlib.decompress(base64.b64decode(encoded_azure_queue_message.content)))

        self.logger.debug("Got account:%s message:%s %s:%d policy:%s recipients:%s" % (
            queue_message.get('account', 'na'),
            encoded_azure_queue_message.id,
            queue_message['policy']['resource'],
            len(queue_message['resources']),
            queue_message['policy']['name'],
            ', '.join(queue_message['action'].get('to'))))

        if any(e.startswith('slack') or e.startswith('https://hooks.slack.com/')
                for e in queue_message.get('action', ()).get('to')):
            from c7n_mailer.slack_delivery import SlackDelivery
            slack_delivery = SlackDelivery(self.config,
                                           self.logger,
                                           SendGridDelivery(self.config, self.logger))
            slack_messages = slack_delivery.get_to_addrs_slack_messages_map(queue_message)
            try:
                slack_delivery.slack_handler(queue_message, slack_messages)
            except Exception:
                traceback.print_exc()
                pass

        # this section gets the map of metrics to send to datadog and delivers it
        if any(e.startswith('datadog') for e in queue_message.get('action', ()).get('to')):
            from c7n_mailer.datadog_delivery import DataDogDelivery
            datadog_delivery = DataDogDelivery(self.config, self.session, self.logger)
            datadog_message_packages = datadog_delivery.get_datadog_message_packages(queue_message)

            try:
                datadog_delivery.deliver_datadog_messages(datadog_message_packages, queue_message)
            except Exception:
                traceback.print_exc()
                pass

        # this section sends a notification to the resource owner via SendGrid
        try:
            sendgrid_delivery = SendGridDelivery(self.config, self.logger)
            sendgrid_messages = sendgrid_delivery.get_to_addrs_sendgrid_messages_map(queue_message)
            return sendgrid_delivery.sendgrid_handler(queue_message, sendgrid_messages)
        except Exception:
            traceback.print_exc()