Skip to content

Commit aa00dd9

Browse files
authored
Improve support for folders, by adding parent_uid option (#164)
1 parent 6aa9701 commit aa00dd9

File tree

4 files changed

+75
-8
lines changed

4 files changed

+75
-8
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
uses `GrafanaTimeoutError` instead. Other than this, [Niquests] is a drop-
1010
in replacement for [Requests] and therefore is largely compatible.
1111
* Remove Python 3.6 support. Thanks, @Ousret.
12+
* Improve support for folders, by adding ``parent_uid`` option to relevant
13+
endpoints, and by adding missing ``move_folder``. Thanks, @grafuls.
1214

1315
[Niquests]: https://niquests.readthedocs.io/
1416
[Riquests]: https://requests.readthedocs.io/

grafana_client/elements/_async/folder.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@ def __init__(self, client):
66
super(Folder, self).__init__(client)
77
self.client = client
88

9-
async def get_all_folders(self):
9+
async def get_all_folders(self, parent_uid=None):
1010
"""
1111
1212
:return:
1313
"""
1414
path = "/folders"
15-
return await self.client.GET(path)
15+
data = {}
16+
if parent_uid:
17+
data["parentUid"] = parent_uid
18+
return await self.client.GET(path, data=data)
1619

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

26-
async def create_folder(self, title, uid=None):
29+
async def create_folder(self, title, uid=None, parent_uid=None):
2730
"""
2831
2932
:param title:
3033
:param uid:
34+
:param parent_uid:
3135
:return:
3236
"""
3337
json_data = dict(title=title)
3438
if uid is not None:
3539
json_data["uid"] = uid
40+
if parent_uid is not None:
41+
json_data["parentUid"] = parent_uid
3642
return await self.client.POST("/folders", json=json_data)
3743

44+
async def move_folder(self, uid, parent_uid):
45+
"""
46+
Move a folder beneath another parent folder.
47+
48+
This is relevant only if nested folders are enabled.
49+
50+
:param uid:
51+
:param parent_uid:
52+
:return:
53+
"""
54+
path = "/folders/%s/move" % uid
55+
return await self.client.POST(path, json={"parentUid": parent_uid})
56+
3857
async def update_folder(self, uid, title=None, version=None, overwrite=False, new_uid=None):
3958
"""
4059

grafana_client/elements/folder.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@ def __init__(self, client):
66
super(Folder, self).__init__(client)
77
self.client = client
88

9-
def get_all_folders(self):
9+
def get_all_folders(self, parent_uid=None):
1010
"""
1111
1212
:return:
1313
"""
1414
path = "/folders"
15-
return self.client.GET(path)
15+
data = {}
16+
if parent_uid:
17+
data["parentUid"] = parent_uid
18+
return self.client.GET(path, data=data)
1619

1720
def get_folder(self, uid):
1821
"""
@@ -23,18 +26,34 @@ def get_folder(self, uid):
2326
path = "/folders/%s" % uid
2427
return self.client.GET(path)
2528

26-
def create_folder(self, title, uid=None):
29+
def create_folder(self, title, uid=None, parent_uid=None):
2730
"""
2831
2932
:param title:
3033
:param uid:
34+
:param parent_uid:
3135
:return:
3236
"""
3337
json_data = dict(title=title)
3438
if uid is not None:
3539
json_data["uid"] = uid
40+
if parent_uid is not None:
41+
json_data["parentUid"] = parent_uid
3642
return self.client.POST("/folders", json=json_data)
3743

44+
def move_folder(self, uid, parent_uid):
45+
"""
46+
Move a folder beneath another parent folder.
47+
48+
This is relevant only if nested folders are enabled.
49+
50+
:param uid:
51+
:param parent_uid:
52+
:return:
53+
"""
54+
path = "/folders/%s/move" % uid
55+
return self.client.POST(path, json={"parentUid": parent_uid})
56+
3857
def update_folder(self, uid, title=None, version=None, overwrite=False, new_uid=None):
3958
"""
4059

test/elements/test_folder.py

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def test_get_all_folders(self, m):
3232
}
3333
],
3434
)
35-
folders = self.grafana.folder.get_all_folders()
35+
folders = self.grafana.folder.get_all_folders(parent_uid="gFtOEwFlbb")
3636
self.assertEqual(folders[0]["id"], 1)
3737
self.assertEqual(len(folders), 1)
3838

@@ -66,6 +66,7 @@ def test_create_folder(self, m):
6666
json={
6767
"id": 1,
6868
"uid": "nErXDvCkzz",
69+
"parentUid": "gFtOEwFlbb",
6970
"title": "Departmenet ABC",
7071
"url": "/dashboards/f/nErXDvCkzz/department-abc",
7172
"hasAcl": "false",
@@ -79,8 +80,9 @@ def test_create_folder(self, m):
7980
"version": 1,
8081
},
8182
)
82-
folder = self.grafana.folder.create_folder(title="Departmenet ABC", uid="nErXDvCkzz")
83+
folder = self.grafana.folder.create_folder(title="Departmenet ABC", uid="nErXDvCkzz", parent_uid="gFtOEwFlbb")
8384
self.assertEqual(folder["uid"], "nErXDvCkzz")
85+
self.assertEqual(folder["parentUid"], "gFtOEwFlbb")
8486

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

97+
@requests_mock.Mocker()
98+
def test_move_folder(self, m):
99+
m.post(
100+
"http://localhost/api/folders/nErXDvCkzz/move",
101+
json={
102+
"id": 1,
103+
"uid": "nErXDvCkzz",
104+
"parentUid": "gFtOEwFlbb",
105+
"title": "Departmenet ABC",
106+
"url": "/dashboards/f/nErXDvCkzz/department-abc",
107+
"hasAcl": "false",
108+
"canSave": "false",
109+
"canEdit": "false",
110+
"canAdmin": "false",
111+
"createdBy": "admin",
112+
"created": "2018-01-31T17:43:12+01:00",
113+
"updatedBy": "admin",
114+
"updated": "2018-01-31T17:43:12+01:00",
115+
"version": 1,
116+
},
117+
)
118+
folder = self.grafana.folder.move_folder(uid="nErXDvCkzz", parent_uid="gFtOEwFlbb")
119+
self.assertEqual(folder["uid"], "nErXDvCkzz")
120+
self.assertEqual(folder["parentUid"], "gFtOEwFlbb")
121+
95122
@requests_mock.Mocker()
96123
def test_update_folder(self, m):
97124
m.put(

0 commit comments

Comments
 (0)