Skip to content

Commit 162f647

Browse files
committed
Fix add_annotation to also accept dashboard_uid argument
1 parent ca42f53 commit 162f647

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## unreleased
44

55
* Add support for service accounts. Thanks, @einar-lanfranco and @feerbau.
6+
* Fix `add_annotation` to also accept `dashboard_uid` argument. Thanks, @IBUMBLEBEE.
67

78

89
## 3.10.0 (2023-10-30)

grafana_client/elements/annotations.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ def get_annotation(
7878
def add_annotation(
7979
self,
8080
dashboard_id=None,
81+
dashboard_uid=None,
8182
panel_id=None,
8283
time_from=None,
8384
time_to=None,
@@ -88,6 +89,7 @@ def add_annotation(
8889
https://grafana.com/docs/grafana/latest/http_api/annotations/#create-annotation
8990
9091
:param dashboard_id:
92+
:param dashboard_uid:
9193
:param panel_id
9294
:param time_from:
9395
:param time_to:
@@ -98,13 +100,16 @@ def add_annotation(
98100

99101
annotations_path = "/annotations"
100102
payload = {
101-
"dashboardId": dashboard_id,
102103
"panelId": panel_id,
103104
"time": time_from,
104105
"timeEnd": time_to,
105106
"tags": tags,
106107
"text": text,
107108
}
109+
if dashboard_id is not None:
110+
payload["dashboardId"] = dashboard_id
111+
if dashboard_uid is not None:
112+
payload["dashboardUID"] = dashboard_uid
108113

109114
r = self.client.POST(annotations_path, json=payload)
110115

test/elements/test_annotations.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ def test_delete_annotations_by_id_bad_input(self, m):
131131
self.grafana.annotations.delete_annotations_by_id(annotations_id=None)
132132

133133
@requests_mock.Mocker()
134-
def test_add_annotation(self, m):
134+
def test_add_annotation_no_dashboard(self, m):
135135
m.post(
136136
"http://localhost/api/annotations",
137137
json={"endId": 80, "id": 79, "message": "Annotation added"},
@@ -143,6 +143,28 @@ def test_add_annotation(self, m):
143143
self.assertEqual(annotation["id"], 79)
144144
self.assertEqual(annotation["message"], "Annotation added")
145145

146+
@requests_mock.Mocker()
147+
def test_add_annotation_dashboard_id(self, m):
148+
m.post(
149+
"http://localhost/api/annotations",
150+
json={"dashboardId": 42},
151+
)
152+
annotation = self.grafana.annotations.add_annotation(
153+
dashboard_id=42, time_from=1563183710618, time_to=1563185212275, tags=["tags-test"], text="Test"
154+
)
155+
self.assertEqual(annotation["dashboardId"], 42)
156+
157+
@requests_mock.Mocker()
158+
def test_add_annotation_dashboard_uid(self, m):
159+
m.post(
160+
"http://localhost/api/annotations",
161+
json={"dashboardUID": "jcIIG-07z"},
162+
)
163+
annotation = self.grafana.annotations.add_annotation(
164+
dashboard_uid="jcIIG-07z", time_from=1563183710618, time_to=1563185212275, tags=["tags-test"], text="Test"
165+
)
166+
self.assertEqual(annotation["dashboardUID"], "jcIIG-07z")
167+
146168
@requests_mock.Mocker()
147169
def test_update_annotation(self, m):
148170
m.put(

0 commit comments

Comments
 (0)