Skip to content
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

Updated to v2 of OpsGenie API #65

Merged
merged 1 commit into from
Feb 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 5 additions & 13 deletions loginsightwebhookdemo/opsgenie.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,40 +7,32 @@

__author__ = "Steve Flanders"
__license__ = "Apache v2"
__version__ = "1.0"
__version__ = "2.0"


# opsgenie post url defined by https://v2.developer.opsgenie.com/v2/docs/trigger-events - don't change
OPSGENIEURL = 'https://api.opsgenie.com/v1/json/alert'
OPSGENIEURL = 'https://api.opsgenie.com/v2/alerts'


@app.route("/endpoint/opsgenie/<APIKEY>", methods=['POST'])
@app.route("/endpoint/opsgenie/<APIKEY>/<ALERTID>", methods=['POST','PUT'])
@app.route("/endpoint/opsgenie/<APIKEY>/<TEAM>", methods=['POST'])
@app.route("/endpoint/opsgenie/<APIKEY>/<TEAM>/<ALERTID>", methods=['POST','PUT'])
def opsgenie(APIKEY=None, TEAM=None, ALERTID=None):
"""
Create a new incident for the opsgenie service identified by `APIKEY` in the URL.
Uses https://www.opsgenie.com/docs/web-api/alert-api#createAlertRequest.
Uses https://docs.opsgenie.com/docs/alert-api.
"""
if not OPSGENIEURL:
return ("OPSGENIEURL parameter must be set properly, please edit the shim!", 500, None)
if not APIKEY:
return ("APIKEY must be set in the URL (e.g. /endpoint/opsgenie/<APIKEY>", 500, None)
HEADERS = {"Authorization": "GenieKey " + APIKEY}

# Retrieve fields in notification
a = parse(request)

payload = {
"apiKey": APIKEY,
"alias": a['AlertName'],
"message": a['AlertName'],
"details": {
"notes": a['info'],
"events": str(a['Messages']),
},
"description": a['moreinfo']
}
if TEAM is not None:
payload.append({"teams": [TEAM]})
return callapi(OPSGENIEURL, 'post', json.dumps(payload))
return callapi(OPSGENIEURL, 'post', json.dumps(payload), HEADERS)
16 changes: 4 additions & 12 deletions tests/opsgenie_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,14 @@ def test_opsgenie_nourl():
assert rsp.status == '404 NOT FOUND'
rsp = client.post('/endpoint/opsgenie/' + APIKEY, data=conftest.payload, content_type="application/json")
assert rsp.status == '500 INTERNAL SERVER ERROR'
rsp = client.post('/endpoint/opsgenie/' + APIKEY + '/' + TEAM, data=conftest.payload, content_type="application/json")
assert rsp.status == '500 INTERNAL SERVER ERROR'


def test_opsgenie_allparams():
loginsightwebhookdemo.opsgenie.OPSGENIEURL = 'https://api.opsgenie.com/v1/json/alert'
loginsightwebhookdemo.opsgenie.OPSGENIEURL = 'https://api.opsgenie.com/v2/alerts'

rsp = client.post('/endpoint/opsgenie/' + APIKEY, data=conftest.payload, content_type="application/json")
assert rsp.status == '401 UNAUTHORIZED'
assert rsp.status == '422 UNPROCESSABLE ENTITY'
rsp = client.post('/endpoint/opsgenie/' + APIKEY, data=conftest.payloadvROps60, content_type="application/json")
assert rsp.status == '401 UNAUTHORIZED'
assert rsp.status == '422 UNPROCESSABLE ENTITY'
rsp = client.post('/endpoint/opsgenie/' + APIKEY, data=conftest.payloadLI_test, content_type="application/json")
assert rsp.status == '401 UNAUTHORIZED'
rsp = client.post('/endpoint/opsgenie/' + APIKEY + '/' + TEAM, data=conftest.payload, content_type="application/json")
assert rsp.status == '500 INTERNAL SERVER ERROR'
rsp = client.post('/endpoint/opsgenie/' + APIKEY + '/' + TEAM, data=conftest.payloadvROps60, content_type="application/json")
assert rsp.status == '500 INTERNAL SERVER ERROR'
rsp = client.post('/endpoint/opsgenie/' + APIKEY + '/' + TEAM, data=conftest.payloadLI_test, content_type="application/json")
assert rsp.status == '500 INTERNAL SERVER ERROR'
assert rsp.status == '422 UNPROCESSABLE ENTITY'
5 changes: 1 addition & 4 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,13 @@ commands=
py.test {posargs} \
--cov=loginsightwebhookdemo --cov-config=tox.ini --cov-fail-under=70 --cov-report term-missing \
--flakes --pep8 \
--ignore=tests/servicenow_test.py \
--ignore=tests/opsgenie_test.py
--ignore=tests/servicenow_test.py

[report]
omit =
loginsightwebhookdemo/servicenow.py,\
loginsightwebhookdemo/template.py, \
loginsightwebhookdemo/kafkatopic.py, \
loginsightwebhookdemo/opsgenie.py, \
loginsightwebhookdemo/moogsoft.py

[run]
Expand All @@ -32,7 +30,6 @@ omit =
loginsightwebhookdemo/servicenow.py,\
loginsightwebhookdemo/template.py, \
loginsightwebhookdemo/kafkatopic.py, \
loginsightwebhookdemo/opsgenie.py, \
loginsightwebhookdemo/moogsoft.py


Expand Down