-
Notifications
You must be signed in to change notification settings - Fork 72
Events API v2 Support #84
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jjinno
wants to merge
9
commits into
dropbox:master
Choose a base branch
from
jjinno:events_v2
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+160
−11
Open
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
094e46c
Update docs for Events API v2
jjinno a3dc8d8
Create events_v2.py
jjinno a20c606
Create event_request_v2.json
jjinno c4bb0da
Add Events API v2 test
jjinno 554742c
Fixed flake8 bugs
jjinno 6e7e1c8
moved images/links out of payload
jjinno f544cb0
Made payload optional
jjinno 9d25ada
Fixed flake8 issues
jjinno b77b824
fixed whitespace issue
jjinno File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| # These methods are compatible with Pagerduty Events API v2 | ||
| # - https://v2.developer.pagerduty.com/docs/events-api-v2 | ||
| # - https://support.pagerduty.com/docs/pd-cef | ||
|
|
||
| from six.moves import urllib | ||
| from .exceptions import Error, IntegrationAPIError, BadRequest, NotFound | ||
| from .common import ( | ||
| _json_dumper, | ||
| Requester, | ||
| ) | ||
|
|
||
|
|
||
| INTEGRATION_API_URL = "https://events.pagerduty.com/v2/enqueue" | ||
|
|
||
|
|
||
| class Events(object): | ||
| def __init__(self, routing_key, requester=None): | ||
| self.routing_key = routing_key | ||
| self.headers = { | ||
| "Content-type": "application/json", | ||
| "Accept": "application/vnd.pagerduty+json;version=2", | ||
| "X-Routing-Key": self.routing_key | ||
| } | ||
| if requester is None: | ||
| self.requester = Requester() | ||
| else: | ||
| self.requester = requester | ||
|
|
||
| def enqueue(self, **kwargs): | ||
| # Required (according to documentation) PD-CEF fields | ||
| data = {"event_action": kwargs['event_action']} | ||
| data['payload'] = kwargs.get('payload', {}) | ||
| for key in ['summary', 'source', 'severity']: | ||
| if key in kwargs: | ||
| data['payload'][key] = kwargs[key] | ||
| elif key not in data['payload']: | ||
| raise KeyError(key) | ||
|
|
||
| # Optional event fields | ||
| for key in ['dedup_key', 'routing_key', 'images', 'links']: | ||
| if key in kwargs.keys(): | ||
| data[key] = kwargs[key] | ||
|
|
||
| # Optional PD-CEF fields | ||
| for key in ['component', 'group', 'class', | ||
| 'custom_details', 'timestamp']: | ||
| if key in kwargs.keys(): | ||
| data['payload'][key] = kwargs[key] | ||
|
|
||
| request = urllib.request.Request(INTEGRATION_API_URL, | ||
| data=_json_dumper(data).encode('utf-8'), | ||
| headers=self.headers) | ||
| response = self.requester.execute_request(request) | ||
|
|
||
| if not response.get("status", "failure") == "success": | ||
| raise IntegrationAPIError(response["message"], kwargs['event_action']) | ||
| return response["dedup_key"] | ||
|
|
||
| def resolve_incident(self, **kwargs): | ||
| """ Causes the referenced incident to enter resolved state. | ||
| Send a resolve event when the problem that caused the initial | ||
| trigger has been fixed. | ||
| """ | ||
| kwargs['event_action'] = 'resolve' | ||
| return self.enqueue(**kwargs) | ||
|
|
||
| def acknowledge_incident(self, **kwargs): | ||
| """ Causes the referenced incident to enter the acknowledged state. | ||
| Send an acknowledge event when someone is presently working on the | ||
| incident. | ||
| """ | ||
| kwargs['event_action'] = 'acknowledge' | ||
| return self.enqueue(**kwargs) | ||
|
|
||
| def trigger_incident(self, **kwargs): | ||
| """ Report a new or ongoing problem. When PagerDuty receives a trigger, | ||
| it will either open a new incident, or add a new log entry to an | ||
| existing incident. | ||
| """ | ||
| kwargs['event_action'] = 'trigger' | ||
| return self.enqueue(**kwargs) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| { | ||
| "event_action": "trigger", | ||
| "payload": { | ||
| "custom_details": { | ||
| "Service Name": "Test Service", | ||
| "Problem": "Something explanation", | ||
| "Service ID": "P123456", | ||
| "Result": "Error testing occurred" | ||
| }, | ||
| "severity": "error", | ||
| "component": "Test Component", | ||
| "summary": "Service (P123456) Test Error", | ||
| "source": "Test Source", | ||
| "class": "Unit Test" | ||
| }, | ||
| "dedup_key": "Service (P123456) Test Dedup Key" | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A teammate pointed out that
payloadis optional, but looks like this assumes payload is required (by checking for these three keys).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for pointing this out. 👍