Skip to content

Commit

Permalink
Merge pull request #595 from airbnb/javier-streamalert-pagerduty-note
Browse files Browse the repository at this point in the history
[output] Adding support to add notes to incidents
  • Loading branch information
jacknagz authored Feb 14, 2018
2 parents d508896 + 206fd8e commit e1ecf3c
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
29 changes: 29 additions & 0 deletions stream_alert/alert_processor/outputs/pagerduty.py
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,35 @@ def _incident_assignment(self, context):
# Verify escalation policy, return tuple
return 'escalation_policy', self._policy_verify(policy_to_assign, self._escalation_policy)

def _add_incident_note(self, incident_id, note):
"""Method to add a text note to the provided incident id
Args:
incident_id (str): ID of the incident to add the note to
Returns:
str: ID of the note after being added to the incident or False if it fails
"""
notes_path = '{}/{}/notes'.format(self.INCIDENTS_ENDPOINT, incident_id)
incident_notes_url = self._get_endpoint(self._base_url, notes_path)
data = {
'note': {
'content': note
}
}
try:
resp = self._post_request_retry(incident_notes_url, data, self._headers, True)
except OutputRequestFailure:
return False

response = resp.json()
if not response:
return False

note_rec = response.get('note', {})

return note_rec.get('id', False)

def dispatch(self, **kwargs):
"""Send incident to Pagerduty Incidents API v2
Keyword Args:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,50 @@ def test_incident_assignment_policy_default(self, get_mock):
assert_equal(assigned_value['id'], 'verified_policy_id')
assert_equal(assigned_value['type'], 'escalation_policy_reference')

@patch('requests.post')
def test_add_note_incident_success(self, post_mock):
"""PagerDutyIncidentOutput - Add Note to Incident Success"""
post_mock.return_value.status_code = 200
json_note = {'note': {'id': 'created_note_id'}}
post_mock.return_value.json.return_value = json_note

note_id = self._dispatcher._add_incident_note('incident_id', 'this is the note')

assert_equal(note_id, 'created_note_id')

@patch('requests.post')
def test_add_note_incident_fail(self, post_mock):
"""PagerDutyIncidentOutput - Add Note to Incident Fail"""
post_mock.return_value.status_code = 200
json_note = {'note': {'not_id': 'created_note_id'}}
post_mock.return_value.json.return_value = json_note

note_id = self._dispatcher._add_incident_note('incident_id', 'this is the note')

assert_false(note_id)

@patch('requests.post')
def test_add_note_incident_bad_request(self, post_mock):
"""PagerDutyIncidentOutput - Add Note to Incident Bad Request"""
post_mock.return_value.status_code = 400
json_note = {'note': {'id': 'created_note_id'}}
post_mock.return_value.json.return_value = json_note

note_id = self._dispatcher._add_incident_note('incident_id', 'this is the note')

assert_false(note_id)

@patch('requests.post')
def test_add_note_incident_no_response(self, post_mock):
"""PagerDutyIncidentOutput - Add Note to Incident No Response"""
post_mock.return_value.status_code = 200
json_note = {}
post_mock.return_value.json.return_value = json_note

note_id = self._dispatcher._add_incident_note('incident_id', 'this is the note')

assert_false(note_id)

@patch('requests.get')
def test_item_verify_fail(self, get_mock):
"""PagerDutyIncidentOutput - Item Verify Fail"""
Expand Down

0 comments on commit e1ecf3c

Please sign in to comment.