Skip to content

Commit

Permalink
Improve support for folders, by adding parent_uid option
Browse files Browse the repository at this point in the history
... and by adding missing `move_folder`.
  • Loading branch information
amotl committed Mar 29, 2024
1 parent 6aa9701 commit a274c2a
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 8 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
uses `GrafanaTimeoutError` instead. Other than this, [Niquests] is a drop-
in replacement for [Requests] and therefore is largely compatible.
* Remove Python 3.6 support. Thanks, @Ousret.
* Improve support for folders, by adding ``parent_uid`` option to relevant
endpoints, and by adding missing ``move_folder``. Thanks, @grafuls.

[Niquests]: https://niquests.readthedocs.io/
[Riquests]: https://requests.readthedocs.io/
Expand Down
25 changes: 22 additions & 3 deletions grafana_client/elements/_async/folder.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@ def __init__(self, client):
super(Folder, self).__init__(client)
self.client = client

async def get_all_folders(self):
async def get_all_folders(self, parent_uid=None):
"""
:return:
"""
path = "/folders"
return await self.client.GET(path)
data = {}
if parent_uid:
data["parentUid"] = parent_uid
return await self.client.GET(path, data=data)

async def get_folder(self, uid):
"""
Expand All @@ -23,18 +26,34 @@ async def get_folder(self, uid):
path = "/folders/%s" % uid
return await self.client.GET(path)

async def create_folder(self, title, uid=None):
async def create_folder(self, title, uid=None, parent_uid=None):
"""
:param title:
:param uid:
:param parent_uid:
:return:
"""
json_data = dict(title=title)
if uid is not None:
json_data["uid"] = uid
if parent_uid is not None:
json_data["parentUid"] = parent_uid
return await self.client.POST("/folders", json=json_data)

async def move_folder(self, uid, parent_uid):
"""
Move a folder beneath another parent folder.
This is relevant only if nested folders are enabled.
:param uid:
:param parent_uid:
:return:
"""
path = "/folders/%s/move" % uid
return await self.client.POST(path, json={"parentUid": parent_uid})

async def update_folder(self, uid, title=None, version=None, overwrite=False, new_uid=None):
"""
Expand Down
25 changes: 22 additions & 3 deletions grafana_client/elements/folder.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@ def __init__(self, client):
super(Folder, self).__init__(client)
self.client = client

def get_all_folders(self):
def get_all_folders(self, parent_uid=None):
"""
:return:
"""
path = "/folders"
return self.client.GET(path)
data = {}
if parent_uid:
data["parentUid"] = parent_uid
return self.client.GET(path, data=data)

def get_folder(self, uid):
"""
Expand All @@ -23,18 +26,34 @@ def get_folder(self, uid):
path = "/folders/%s" % uid
return self.client.GET(path)

def create_folder(self, title, uid=None):
def create_folder(self, title, uid=None, parent_uid=None):
"""
:param title:
:param uid:
:param parent_uid:
:return:
"""
json_data = dict(title=title)
if uid is not None:
json_data["uid"] = uid
if parent_uid is not None:
json_data["parentUid"] = parent_uid
return self.client.POST("/folders", json=json_data)

def move_folder(self, uid, parent_uid):
"""
Move a folder beneath another parent folder.
This is relevant only if nested folders are enabled.
:param uid:
:param parent_uid:
:return:
"""
path = "/folders/%s/move" % uid
return self.client.POST(path, json={"parentUid": parent_uid})

def update_folder(self, uid, title=None, version=None, overwrite=False, new_uid=None):
"""
Expand Down
31 changes: 29 additions & 2 deletions test/elements/test_folder.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def test_get_all_folders(self, m):
}
],
)
folders = self.grafana.folder.get_all_folders()
folders = self.grafana.folder.get_all_folders(parent_uid="gFtOEwFlbb")
self.assertEqual(folders[0]["id"], 1)
self.assertEqual(len(folders), 1)

Expand Down Expand Up @@ -66,6 +66,7 @@ def test_create_folder(self, m):
json={
"id": 1,
"uid": "nErXDvCkzz",
"parentUid": "gFtOEwFlbb",
"title": "Departmenet ABC",
"url": "/dashboards/f/nErXDvCkzz/department-abc",
"hasAcl": "false",
Expand All @@ -79,8 +80,9 @@ def test_create_folder(self, m):
"version": 1,
},
)
folder = self.grafana.folder.create_folder(title="Departmenet ABC", uid="nErXDvCkzz")
folder = self.grafana.folder.create_folder(title="Departmenet ABC", uid="nErXDvCkzz", parent_uid="gFtOEwFlbb")
self.assertEqual(folder["uid"], "nErXDvCkzz")
self.assertEqual(folder["parentUid"], "gFtOEwFlbb")

@requests_mock.Mocker()
def test_create_folder_empty_uid(self, m):
Expand All @@ -92,6 +94,31 @@ def test_create_folder_empty_uid(self, m):
with self.assertRaises(GrafanaBadInputError):
self.grafana.folder.create_folder(title="Departmenet ABC")

@requests_mock.Mocker()
def test_move_folder(self, m):
m.post(
"http://localhost/api/folders/nErXDvCkzz/move",
json={
"id": 1,
"uid": "nErXDvCkzz",
"parentUid": "gFtOEwFlbb",
"title": "Departmenet ABC",
"url": "/dashboards/f/nErXDvCkzz/department-abc",
"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.grafana.folder.move_folder(uid="nErXDvCkzz", parent_uid="gFtOEwFlbb")
self.assertEqual(folder["uid"], "nErXDvCkzz")
self.assertEqual(folder["parentUid"], "gFtOEwFlbb")

@requests_mock.Mocker()
def test_update_folder(self, m):
m.put(
Expand Down

0 comments on commit a274c2a

Please sign in to comment.