Skip to content

Commit

Permalink
chg: Add support for alert violations
Browse files Browse the repository at this point in the history
  • Loading branch information
airstandley committed Sep 1, 2021
1 parent 07b4430 commit 7e8439e
Show file tree
Hide file tree
Showing 2 changed files with 163 additions and 0 deletions.
69 changes: 69 additions & 0 deletions newrelic_api/alert_violations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
from .base import Resource


class AlertViolations(Resource):
"""
An interface for interacting with the NewRelic Alerts Violations API.
"""
def list(self, filter_start_date=None, filter_end_date=None, filter_only_open=None, page=None):
"""
This API endpoint returns a paginated list of the violations
associated with your New Relic account. Alert violations can be filtered
by time window and open status.
:type filter_start_date: datetime.datetime
:param filter_start_date: Filter to violations created after this time
:type filter_end_date: datetime.datetime
:param filter_end_date: Filter to violations created before this time
:type filter_only_open: boolean
:param filter_start_date: Filter by open violations
:type page: int
:param page: Pagination index
:rtype: dict
:return: The JSON response of the API, with an additional 'pages' key
if there are paginated results
::
{
"violation": {
"id": "integer",
"label": "string",
"duration": "integer",
"policy_name": "string",
"condition_name": "string",
"priority": "string",
"opened_at": "integer",
"closed_at": "integer",
"entity": {
"product": "string",
"type": "string",
"group_id": "integer",
"id": "integer",
"name": "string"
},
"links": {
"policy_id": "integer",
"condition_id": "integer",
"incident_id": "integer"
}
}
}
"""
filters = [
'start_date={0}'.format(filter_start_date.isoformat()) if filter_start_date else None,
'end_date={0}'.format(filter_end_date.isoformat()) if filter_end_date else None,
'only_open={0}'.format('true' if filter_only_open else 'false') if filter_only_open is not None else None,
'page={0}'.format(page) if page else None
]

return self._get(
url='{0}alerts_violations.json'.format(self.URL),
headers=self.headers,
params=self.build_param_string(filters)
)
94 changes: 94 additions & 0 deletions newrelic_api/tests/alert_violations_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
from unittest import TestCase

from mock import patch, Mock
import requests
import datetime

from newrelic_api.alert_violations import AlertViolations


class NRAlertViolationsTests(TestCase):
def setUp(self):
super(NRAlertViolationsTests, self).setUp()
self.violation = AlertViolations(api_key='dummy_key')

self.violations_list_response = {
"violations": [
{
"id": 12345,
"label": "Violation of Default Server Policy",
"duration": 100,
"policy_name": "Default Server Policy",
"condition_name": "CPU usage alert",
"priority": "Warning",
"opened_at": 123456789012,
"entity": {
"product": "Apm",
"type": "Application",
"group_id": 123456,
"id": 1234567,
"name": "Developer Application"
},
"links": {
"policy_id": 12345,
"condition_id": 100
}
}
]
}

@patch.object(requests, 'get')
def test_list_success(self, mock_get):
"""
Test alert violations .list()
"""
mock_response = Mock(name='response')
mock_response.json.return_value = self.violations_list_response
mock_get.return_value = mock_response

# Call the method
response = self.violation.list()

self.assertIsInstance(response, dict)
mock_get.assert_called_once_with(
url='https://api.newrelic.com/v2/alerts_violations.json',
headers=self.violation.headers,
params=''
)

@patch.object(requests, 'get')
def test_list_success_with_filters(self, mock_get):
"""
Test alert viollations .list() with filters
"""
mock_response = Mock(name='response')
mock_response.json.return_value = self.violations_list_response
mock_get.return_value = mock_response

# Call the method
response = self.violation.list(
filter_start_date=datetime.datetime(2000, 1, 1, 12, 0, 0, 0),
filter_end_date=datetime.datetime(2010, 1, 1, 12, 0, 0, 0),
filter_only_open=True,
page=1
)

self.assertIsInstance(response, dict)
mock_get.assert_called_once_with(
url='https://api.newrelic.com/v2/alerts_violations.json',
headers=self.violation.headers,
params='start_date=2000-01-01T12:00:00+00:00&end_date=2010-01-01T12:00:00+00:00&only_open=true&page=1'
)

@patch.object(requests, 'get')
def test_list_failure(self, mock_get):
"""
Test alert policies .list() failure case
"""
mock_response = Mock(name='response')
mock_response.json.side_effect = ValueError('No JSON object could be decoded')
mock_get.return_value = mock_response

with self.assertRaises(ValueError):
# Call the method
self.violation.list()

0 comments on commit 7e8439e

Please sign in to comment.