def test_deliver_datadog_messages_should_not_send_metric_if_metrics_are_empty( self, mock_datadog_api, mock_time): datadog_delivery = DataDogDelivery(self.config, self.session, self.logger) datadog_delivery.deliver_datadog_messages([], SQS_MESSAGE_3) mock_datadog_api.assert_not_called()
def test_datadog_message_packages_should_return_messages(self, mock_time): data_dog_delivery = DataDogDelivery(self.config, self.session, self.logger) answer = data_dog_delivery.get_datadog_message_packages(SQS_MESSAGE_2) answer[0]['tags'].sort() answer[1]['tags'].sort() assert len(answer) == 2 assert answer == DATADOG_METRIC_SQS_MESSAGE_2
def test_datadog_message_packages_should_return_messages(self, mock_time): data_dog_delivery = DataDogDelivery(self.config, self.session, self.logger) answer = data_dog_delivery.get_datadog_message_packages(SQS_MESSAGE_2) answer[0]['tags'].sort() answer[1]['tags'].sort() assert len(answer) == 2 assert answer == DATADOG_METRIC_SQS_MESSAGE_2
def _deliver_datadog_message(self, queue_message): 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: self.logger.info('Sending message to Datadog.') datadog_delivery.deliver_datadog_messages(datadog_message_packages, queue_message) except Exception as error: self.logger.exception(error)
def test_deliver_datadog_messages_should_send_correct_metric_value_to_datadog( self, mock_datadog_api, mock_time): datadog_delivery = DataDogDelivery(self.config, self.session, self.logger) datadog_message_packages = datadog_delivery.get_datadog_message_packages(SQS_MESSAGE_3) datadog_delivery.deliver_datadog_messages(datadog_message_packages, SQS_MESSAGE_3) answer = mock_datadog_api.mock_calls[0][1][0] answer[0]['tags'].sort() assert answer == DATADOG_METRIC_SQS_MESSAGE_3
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 test_deliver_datadog_messages_should_send_correct_metric_value_to_datadog( self, mock_datadog_api, mock_time): datadog_delivery = DataDogDelivery(self.config, self.session, self.logger) datadog_message_packages = datadog_delivery.get_datadog_message_packages( SQS_MESSAGE_3) datadog_delivery.deliver_datadog_messages(datadog_message_packages, SQS_MESSAGE_3) answer = mock_datadog_api.mock_calls[0][1][0] answer[0]['tags'].sort() assert answer == DATADOG_METRIC_SQS_MESSAGE_3
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()
def test_datadog_message_packages_should_return_empty_list_if_no_sqs_messages_returned( self): data_dog_delivery = DataDogDelivery(self.config, self.session, self.logger) assert data_dog_delivery.get_datadog_message_packages(None) == []
def test_should_not_initialize_datadog_with_no_keys_in_config(self): DataDogDelivery({}, self.session, self.logger) self.mock_datadog_initialize.assert_not_called()
def test_should_initialize_datadog_with_keys_in_config(self): DataDogDelivery(self.config, self.session, self.logger) self.mock_datadog_initialize.assert_called_with( api_key=DATADOG_API_KEY, app_key=DATADOG_APPLICATION_KEY)
def test_datadog_message_packages_should_return_empty_list_if_no_sqs_messages_returned(self): data_dog_delivery = DataDogDelivery(self.config, self.session, self.logger) assert data_dog_delivery.get_datadog_message_packages(None) == []
def test_deliver_datadog_messages_should_not_send_metric_if_metrics_are_empty( self, mock_datadog_api, mock_time): datadog_delivery = DataDogDelivery(self.config, self.session, self.logger) datadog_delivery.deliver_datadog_messages([], SQS_MESSAGE_3) mock_datadog_api.assert_not_called()