Esempio n. 1
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)
Esempio n. 2
0
 def test_add_notification_channels_to_policy(self):
     """
     Check that the add_notification_channels_to_policy function adds the given notification channels to
     the given policy.
     """
     policy_id = 1
     channel_ids = ['10', '11']
     url = '{}?policy_id={}&channel_ids={}'.format(
         newrelic.ALERTS_POLICIES_CHANNELS_API_URL, policy_id,
         ','.join(channel_ids))
     responses.add(responses.PUT, url, json='', status=200)
     newrelic.add_notification_channels_to_policy(policy_id, channel_ids)
     self.assertEqual(len(responses.calls), 1)
     request_body = responses.calls[0].request.body
     request_headers = responses.calls[0].request.headers
     self.assertEqual(request_headers['x-api-key'], 'admin-api-key')
     self.assertEqual(request_body, None)