Exemplo n.º 1
0
 def test_add_email_notification_channel(self):
     """
     Check that the add_email_notification_channel function adds a notification channel.
     """
     url = '{}.json'.format(newrelic.ALERTS_CHANNELS_API_URL)
     email_address = '*****@*****.**'
     channel_id = 1
     response_json = {
         'channels': [
             {
                 'id': channel_id
             }
         ]
     }
     responses.add(responses.POST, url, json=response_json, status=201)
     self.assertEqual(newrelic.add_email_notification_channel(email_address), channel_id)
     self.assertEqual(len(responses.calls), 1)
     request_json = json.loads(responses.calls[0].request.body.decode())
     request_headers = responses.calls[0].request.headers
     self.assertEqual(request_headers['x-api-key'], 'admin-api-key')
     self.assertEqual(request_json, {
         'channel': {
             'name': email_address,
             'type': 'email',
             'configuration': {
                 'recipients': email_address,
                 'include_json_attachment': False
             }
         }
     })
Exemplo n.º 2
0
 def _add_emails(self, alert_policy, emails):
     """
     Create a notification channel for each given email address if it doesn't exist and add it to this instance's
     alert policy.
     """
     channel_ids = []
     for email in emails:
         try:
             channel = NewRelicEmailNotificationChannel.objects.get(
                 email=email)
             self.logger.info(
                 'Email notification channel for {} already exists. Using it.'
                 .format(email))
             channel_id = channel.id
         except NewRelicEmailNotificationChannel.DoesNotExist:
             self.logger.info(
                 'Creating a new email notification channel for {}'.format(
                     email))
             channel_id = newrelic.add_email_notification_channel(email)
             channel = NewRelicEmailNotificationChannel.objects.create(
                 id=channel_id, email=email)
         channel_ids.append(channel_id)
     # Always add all the notification channels corresponding to the given emails to the policy.
     # Existing email notification channels are ignored.
     newrelic.add_notification_channels_to_policy(alert_policy.id,
                                                  sorted(channel_ids))
     for email_notification_channel in NewRelicEmailNotificationChannel.objects.filter(
             email__in=emails):
         alert_policy.email_notification_channels.add(
             email_notification_channel)