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

Commit

Permalink
Add Snapshot API & Improve Unit Test (#34)
Browse files Browse the repository at this point in the history
* add snapshot api

* add snapshot api & Improve unit test

* Update readme

* Improve unit test
  • Loading branch information
ohmrefresh authored and m0nhawk committed Jul 23, 2019
1 parent cc91809 commit 6174e2e
Show file tree
Hide file tree
Showing 8 changed files with 529 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ Work on API implementation still in progress.
| Organisation | + |
| Other | + |
| Preferences | + |
| Snapshot | - |
| Snapshot | + |
| Teams | + |
| User | + |

Expand Down
2 changes: 2 additions & 0 deletions grafana_api/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@
from .user import User, Users
from .team import Teams
from .annotations import Annotations
from .snapshots import Snapshots

92 changes: 92 additions & 0 deletions grafana_api/api/snapshots.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
from .base import Base


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

def create_new_snapshot(
self,
dashboard=None,
name=None,
expires=None,
external=None,
key=None,
delete_key=None,
):
"""
:param dashboard: Required. The complete dashboard model.
:param name: Optional. snapshot name
:param expires: Optional. When the snapshot should expire in seconds. 3600 is 1 hour, 86400 is 1 day. Default is never to expire.
:param external: Optional. Save the snapshot on an external server rather than locally. Default is false.
:param key: Optional. Define the unique key. Required if external is true.
:param deleteKey: Optional. Unique key used to delete the snapshot. It is different from the key so that only the creator can delete the snapshot. Required if external is true.
:return:
"""

path = "/snapshots"
post_json = {
"dashboard": dashboard
}
if name:
post_json["name"] = name
if expires:
post_json["expires"] = expires
if external:
post_json["external"] = external
if key:
post_json["key"] = key
if delete_key:
post_json["deleteKey"] = delete_key

r = self.api.POST(path, json=post_json)
return r

def get_dashboard_snapshots(self):
"""
:return:
"""
path = "/dashboard/snapshots"
r = self.api.GET(path)
return r

def get_snapshot_by_key(self, key):
"""
:param key:
:return:
"""
path = "/snapshots/%s" % key
r = self.api.GET(path)
return r

def delete_snapshot_by_key(
self,
snapshot_id=None
):
"""
:param snapshot_id:
:return:
"""
path = "/snapshots/{}".format(snapshot_id)
r = self.api.DELETE(path)

return r

def delete_snapshot_by_delete_key(
self,
snapshot_delete_key=None
):
"""
:param snapshot_delete_key:
:return:
"""
path = "/snapshots-delete/{}".format(snapshot_delete_key)
r = self.api.DELETE(path)

return r
4 changes: 3 additions & 1 deletion grafana_api/grafana_face.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
User,
Users,
Teams,
Annotations
Snapshots,
Annotations,
)


Expand Down Expand Up @@ -43,3 +44,4 @@ def __init__(
self.users = Users(self.api)
self.teams = Teams(self.api)
self.annotations = Annotations(self.api)
self.snapshots = Snapshots(self.api)
35 changes: 35 additions & 0 deletions test/api/test_folder.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import requests_mock

from grafana_api.grafana_face import GrafanaFace
from grafana_api.grafana_api import GrafanaBadInputError


class FolderTestCase(unittest.TestCase):
Expand Down Expand Up @@ -83,6 +84,17 @@ def test_create_folder(self, m):
folder = self.cli.folder.create_folder(title="Departmenet ABC", uid="nErXDvCkzz")
self.assertEqual(folder["uid"], "nErXDvCkzz")

@requests_mock.Mocker()
def test_create_folder_empty_uid(self, m):
m.post(
"http://localhost/api/folders",
json={
"message": "Folder title cannot be empty"
}, status_code=400
)
folder = self.cli.folder.create_folder(title="Departmenet ABC")
self.assertRaises(GrafanaBadInputError)

@requests_mock.Mocker()
def test_update_folder(self, m):
m.put(
Expand All @@ -106,6 +118,29 @@ def test_update_folder(self, m):
folder = self.cli.folder.update_folder(title="Departmenet DEF", uid="nErXDvCkzz", version=1, overwrite=True)
self.assertEqual(folder["title"], "Departmenet DEF")

@requests_mock.Mocker()
def test_update_folder_some_param(self, m):
m.put(
"http://localhost/api/folders/nErXDvCkzz",
json={
"id": 1,
"uid": "nErXDvCkzz",
"title": "Departmenet DEF",
"url": "/dashboards/f/nErXDvCkzz/department-def",
"hasAcl": "false",
"canSave": "false",
"canEdit": "false",
"canAdmin": "false",
"createdBy": "admin",
"created": "2018-01-31T17:43:12+01:00",
"updatedBy": "admin",
"updated": "2018-01-31T17:43:12+01:00",
"version": 1
}
)
folder = self.cli.folder.update_folder(title="Departmenet DEF", uid="nErXDvCkzz")
self.assertEqual(folder["title"], "Departmenet DEF")

@requests_mock.Mocker()
def test_get_folder_by_id(self, m):
m.get(
Expand Down
Loading

0 comments on commit 6174e2e

Please sign in to comment.