Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Commit

Permalink
Merge pull request #33 from ohmrefresh/master
Browse files Browse the repository at this point in the history
Add annotation api & Improve code coverage
  • Loading branch information
m0nhawk authored Jul 20, 2019
2 parents 1220c6f + 7e23479 commit 3341802
Show file tree
Hide file tree
Showing 14 changed files with 1,054 additions and 22 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ Work on API implementation still in progress.
|---|---|
| Admin | + |
| Alerting | - |
| Annotations | - |
| Annotations | + |
| Authentication | +- |
| Dashboard | + |
| Dashboard Versions | - |
Expand Down
1 change: 1 addition & 0 deletions grafana_api/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
from .search import Search
from .user import User, Users
from .team import Teams
from .annotations import Annotations
2 changes: 1 addition & 1 deletion grafana_api/api/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,6 @@ def pause_all_alerts(self, pause):
:param pause:
:return:
"""
change_user_permissions = self.path + "/pause-all-alerts"
change_user_permissions = "/admin/pause-all-alerts"
r = self.api.POST(change_user_permissions, json={"paused": pause})
return r
177 changes: 177 additions & 0 deletions grafana_api/api/annotations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
from .base import Base


class Annotations(Base):
def __init__(self, api):
super(Annotations, self).__init__(api)
self.api = api

def get_annotation(
self,
time_from=None,
time_to=None,
alert_id=None,
dashboard_id=None,
panel_id=None,
tags=None,
limit=None,
):

"""
:param time_from:
:param time_to:
:param alert_id:
:param dashboard_id:
:param panel_id:
:param tags:
:param limit:
:return:
"""
list_annotations_path = "/annotations"
params = []

if time_from:
params.append("time_from=%s" % time_from)

if time_to:
params.append("time_to=%s" % time_to)

if alert_id:
params.append("alertId=%s" % alert_id)

if dashboard_id:
params.append("dashboardID=%s" % dashboard_id)

if panel_id:
params.append("panelId=%s" % panel_id)

if tags:
params.append("tags=%s" % tags)

if limit:
params.append("limit=%s" % limit)

list_annotations_path += "?"
list_annotations_path += "&".join(params)

r = self.api.GET(list_annotations_path)

return r

def add_annotation(
self,
time_from=None,
time_to=None,
is_region=True,
tags=None,
text=None,
):

"""
:param time_from:
:param time_to:
:param is_region:
:param tags:
:param text:
:return:
"""
annotations_path = "/annotations"
payload = {
"time": time_from,
"timeEnd": time_to,
"isRegion": bool(is_region),
"tags": [tags],
"text": text

}

r = self.api.POST(annotations_path, json=payload)

return r

def add_annotation_graphite(
self,
what=None,
tags=True,
when=None,
data=None,
):
"""
:param what:
:param tags:
:param when:
:param data:
:return:
"""

annotations_path = "/annotations/graphite"
payload = {
"what": what,
"tags": [tags],
"when": when,
"data": data

}

r = self.api.POST(annotations_path, json=payload)

return r

def update_annotation(
self,
time_from=None,
time_to=None,
is_region=True,
tags=None,
text=None,
):
"""
:param time_from:
:param time_to:
:param is_region:
:param tags:
:param text:
:return:
"""
annotations_path = "/annotations"
payload = {
"time": time_from,
"timeEnd": time_to,
"isRegion": bool(is_region),
"tags": [tags],
"text": text

}

r = self.api.PUT(annotations_path, json=payload)

return r

def delete_annotations_by_region_id(
self,
region_id=None
):

"""
:param region_id:
:return:
"""
annotations_path = "/annotations/region/{}".format(region_id)
r = self.api.DELETE(annotations_path)

return r

def delete_annotations_by_id(
self,
annotations_id=None
):

"""
:param annotations_id:
:return:
"""
annotations_path = "/annotations/{}".format(annotations_id)
r = self.api.DELETE(annotations_path)

return r
4 changes: 2 additions & 2 deletions grafana_api/api/folder.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ def get_folder_by_id(self, folder_id):
r = self.api.GET(path)
return r

def get_folder_permissions(self):
def get_folder_permissions(self,uid):
"""
:return:
"""
path = "/folders/%s/permissions"
path = "/folders/%s/permissions" % uid
r = self.api.GET(path)
return r

Expand Down
32 changes: 18 additions & 14 deletions grafana_api/grafana_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,21 +94,25 @@ def __request_runnner(url, json=None, headers=None):
r = runner(
__url, json=json, headers=headers, auth=self.auth, verify=self.verify
)
try:

if 500 <= r.status_code < 600:
raise GrafanaServerError(
"Server Error {0}: {1}".format(
r.status_code, r.content.decode("ascii", "replace")
if 500 <= r.status_code < 600:
raise GrafanaServerError(
"Client Error {0}: {1}".format(r.status_code, r.json()['message'])
)
)
elif r.status_code == 400:
raise GrafanaBadInputError("Bad Input: `{0}`".format(r.text))
elif r.status_code == 401:
raise GrafanaUnauthorizedError("Unauthorized")
elif 400 <= r.status_code < 500:
raise GrafanaClientError(
"Client Error {0}: {1}".format(r.status_code, r.text)
)
return r.json()
elif r.status_code == 400:
raise GrafanaBadInputError("Bad Input: `{0}`".format(r.text))
elif r.status_code == 401:
raise GrafanaUnauthorizedError("Unauthorized")
elif 400 <= r.status_code < 500:
raise GrafanaClientError(
"Client Error {0}: {1}".format(r.status_code, r.text)
)
return r.json()

except Exception as error:
print('Caught this error: ' + repr(error))



return __request_runnner
2 changes: 2 additions & 0 deletions grafana_api/grafana_face.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
User,
Users,
Teams,
Annotations
)


Expand Down Expand Up @@ -41,3 +42,4 @@ def __init__(
self.user = User(self.api)
self.users = Users(self.api)
self.teams = Teams(self.api)
self.annotations = Annotations(self.api)
Loading

0 comments on commit 3341802

Please sign in to comment.