mirror of
https://github.com/ansible/awx.git
synced 2026-02-23 14:05:59 -03:30
Grafana notifications: Fix panel/dashboardId type (#11083)
* Grafana notifications: Fix panel/dashboardId type
Latest grafana fails with
Error sending notification grafana: 400
[{"classification":"DeserializationError",
"message":"json: cannot unmarshal string into Go struct
field PostAnnotationsCmd.dashboardId of type int64"}]
So ensure the IDs are really int and not strings.
* Fix the dashboard/panelId=0 case
0 is avlaid valid for the ID's, so ensure to allow them.
* Update tests to new behavior
Panel/Dashboard Id fields are not sent if they where not requested.
Alos add tests for the ID=0 case.
This commit is contained in:
@@ -54,8 +54,8 @@ class GrafanaBackend(AWXBaseEmailBackend, CustomNotificationBase):
|
|||||||
):
|
):
|
||||||
super(GrafanaBackend, self).__init__(fail_silently=fail_silently)
|
super(GrafanaBackend, self).__init__(fail_silently=fail_silently)
|
||||||
self.grafana_key = grafana_key
|
self.grafana_key = grafana_key
|
||||||
self.dashboardId = dashboardId
|
self.dashboardId = int(dashboardId) if dashboardId is not None else None
|
||||||
self.panelId = panelId
|
self.panelId = int(panelId) if panelId is not None else None
|
||||||
self.annotation_tags = annotation_tags if annotation_tags is not None else []
|
self.annotation_tags = annotation_tags if annotation_tags is not None else []
|
||||||
self.grafana_no_verify_ssl = grafana_no_verify_ssl
|
self.grafana_no_verify_ssl = grafana_no_verify_ssl
|
||||||
self.isRegion = isRegion
|
self.isRegion = isRegion
|
||||||
@@ -86,8 +86,10 @@ class GrafanaBackend(AWXBaseEmailBackend, CustomNotificationBase):
|
|||||||
if not self.fail_silently:
|
if not self.fail_silently:
|
||||||
raise Exception(smart_str(_("Error converting time {} and/or timeEnd {} to int.").format(m.body['started'], m.body['finished'])))
|
raise Exception(smart_str(_("Error converting time {} and/or timeEnd {} to int.").format(m.body['started'], m.body['finished'])))
|
||||||
grafana_data['isRegion'] = self.isRegion
|
grafana_data['isRegion'] = self.isRegion
|
||||||
grafana_data['dashboardId'] = self.dashboardId
|
if self.dashboardId is not None:
|
||||||
grafana_data['panelId'] = self.panelId
|
grafana_data['dashboardId'] = self.dashboardId
|
||||||
|
if self.panelId is not None:
|
||||||
|
grafana_data['panelId'] = self.panelId
|
||||||
if self.annotation_tags:
|
if self.annotation_tags:
|
||||||
grafana_data['tags'] = self.annotation_tags
|
grafana_data['tags'] = self.annotation_tags
|
||||||
grafana_data['text'] = m.subject
|
grafana_data['text'] = m.subject
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
from unittest import mock
|
from unittest import mock
|
||||||
import datetime as dt
|
import datetime as dt
|
||||||
from django.core.mail.message import EmailMessage
|
from django.core.mail.message import EmailMessage
|
||||||
|
import pytest
|
||||||
|
|
||||||
import awx.main.notifications.grafana_backend as grafana_backend
|
import awx.main.notifications.grafana_backend as grafana_backend
|
||||||
|
|
||||||
@@ -29,7 +30,7 @@ def test_send_messages():
|
|||||||
requests_mock.post.assert_called_once_with(
|
requests_mock.post.assert_called_once_with(
|
||||||
'https://example.com/api/annotations',
|
'https://example.com/api/annotations',
|
||||||
headers={'Content-Type': 'application/json', 'Authorization': 'Bearer testapikey'},
|
headers={'Content-Type': 'application/json', 'Authorization': 'Bearer testapikey'},
|
||||||
json={'text': 'test subject', 'isRegion': True, 'timeEnd': 120000, 'panelId': None, 'time': 60000, 'dashboardId': None},
|
json={'text': 'test subject', 'isRegion': True, 'timeEnd': 120000, 'time': 60000},
|
||||||
verify=True,
|
verify=True,
|
||||||
)
|
)
|
||||||
assert sent_messages == 1
|
assert sent_messages == 1
|
||||||
@@ -59,20 +60,21 @@ def test_send_messages_with_no_verify_ssl():
|
|||||||
requests_mock.post.assert_called_once_with(
|
requests_mock.post.assert_called_once_with(
|
||||||
'https://example.com/api/annotations',
|
'https://example.com/api/annotations',
|
||||||
headers={'Content-Type': 'application/json', 'Authorization': 'Bearer testapikey'},
|
headers={'Content-Type': 'application/json', 'Authorization': 'Bearer testapikey'},
|
||||||
json={'text': 'test subject', 'isRegion': True, 'timeEnd': 120000, 'panelId': None, 'time': 60000, 'dashboardId': None},
|
json={'text': 'test subject', 'isRegion': True, 'timeEnd': 120000, 'time': 60000},
|
||||||
verify=False,
|
verify=False,
|
||||||
)
|
)
|
||||||
assert sent_messages == 1
|
assert sent_messages == 1
|
||||||
|
|
||||||
|
|
||||||
def test_send_messages_with_dashboardid():
|
@pytest.mark.parametrize("dashboardId", [42, 0])
|
||||||
|
def test_send_messages_with_dashboardid(dashboardId):
|
||||||
with mock.patch('awx.main.notifications.grafana_backend.requests') as requests_mock:
|
with mock.patch('awx.main.notifications.grafana_backend.requests') as requests_mock:
|
||||||
requests_mock.post.return_value.status_code = 200
|
requests_mock.post.return_value.status_code = 200
|
||||||
m = {}
|
m = {}
|
||||||
m['started'] = dt.datetime.utcfromtimestamp(60).isoformat()
|
m['started'] = dt.datetime.utcfromtimestamp(60).isoformat()
|
||||||
m['finished'] = dt.datetime.utcfromtimestamp(120).isoformat()
|
m['finished'] = dt.datetime.utcfromtimestamp(120).isoformat()
|
||||||
m['subject'] = "test subject"
|
m['subject'] = "test subject"
|
||||||
backend = grafana_backend.GrafanaBackend("testapikey", dashboardId=42)
|
backend = grafana_backend.GrafanaBackend("testapikey", dashboardId=dashboardId)
|
||||||
message = EmailMessage(
|
message = EmailMessage(
|
||||||
m['subject'],
|
m['subject'],
|
||||||
{"started": m['started'], "finished": m['finished']},
|
{"started": m['started'], "finished": m['finished']},
|
||||||
@@ -89,20 +91,21 @@ def test_send_messages_with_dashboardid():
|
|||||||
requests_mock.post.assert_called_once_with(
|
requests_mock.post.assert_called_once_with(
|
||||||
'https://example.com/api/annotations',
|
'https://example.com/api/annotations',
|
||||||
headers={'Content-Type': 'application/json', 'Authorization': 'Bearer testapikey'},
|
headers={'Content-Type': 'application/json', 'Authorization': 'Bearer testapikey'},
|
||||||
json={'text': 'test subject', 'isRegion': True, 'timeEnd': 120000, 'panelId': None, 'time': 60000, 'dashboardId': 42},
|
json={'text': 'test subject', 'isRegion': True, 'timeEnd': 120000, 'time': 60000, 'dashboardId': dashboardId},
|
||||||
verify=True,
|
verify=True,
|
||||||
)
|
)
|
||||||
assert sent_messages == 1
|
assert sent_messages == 1
|
||||||
|
|
||||||
|
|
||||||
def test_send_messages_with_panelid():
|
@pytest.mark.parametrize("panelId", [42, 0])
|
||||||
|
def test_send_messages_with_panelid(panelId):
|
||||||
with mock.patch('awx.main.notifications.grafana_backend.requests') as requests_mock:
|
with mock.patch('awx.main.notifications.grafana_backend.requests') as requests_mock:
|
||||||
requests_mock.post.return_value.status_code = 200
|
requests_mock.post.return_value.status_code = 200
|
||||||
m = {}
|
m = {}
|
||||||
m['started'] = dt.datetime.utcfromtimestamp(60).isoformat()
|
m['started'] = dt.datetime.utcfromtimestamp(60).isoformat()
|
||||||
m['finished'] = dt.datetime.utcfromtimestamp(120).isoformat()
|
m['finished'] = dt.datetime.utcfromtimestamp(120).isoformat()
|
||||||
m['subject'] = "test subject"
|
m['subject'] = "test subject"
|
||||||
backend = grafana_backend.GrafanaBackend("testapikey", dashboardId=None, panelId=42)
|
backend = grafana_backend.GrafanaBackend("testapikey", dashboardId=None, panelId=panelId)
|
||||||
message = EmailMessage(
|
message = EmailMessage(
|
||||||
m['subject'],
|
m['subject'],
|
||||||
{"started": m['started'], "finished": m['finished']},
|
{"started": m['started'], "finished": m['finished']},
|
||||||
@@ -119,7 +122,7 @@ def test_send_messages_with_panelid():
|
|||||||
requests_mock.post.assert_called_once_with(
|
requests_mock.post.assert_called_once_with(
|
||||||
'https://example.com/api/annotations',
|
'https://example.com/api/annotations',
|
||||||
headers={'Content-Type': 'application/json', 'Authorization': 'Bearer testapikey'},
|
headers={'Content-Type': 'application/json', 'Authorization': 'Bearer testapikey'},
|
||||||
json={'text': 'test subject', 'isRegion': True, 'timeEnd': 120000, 'panelId': 42, 'time': 60000, 'dashboardId': None},
|
json={'text': 'test subject', 'isRegion': True, 'timeEnd': 120000, 'panelId': panelId, 'time': 60000},
|
||||||
verify=True,
|
verify=True,
|
||||||
)
|
)
|
||||||
assert sent_messages == 1
|
assert sent_messages == 1
|
||||||
@@ -179,7 +182,7 @@ def test_send_messages_with_tags():
|
|||||||
requests_mock.post.assert_called_once_with(
|
requests_mock.post.assert_called_once_with(
|
||||||
'https://example.com/api/annotations',
|
'https://example.com/api/annotations',
|
||||||
headers={'Content-Type': 'application/json', 'Authorization': 'Bearer testapikey'},
|
headers={'Content-Type': 'application/json', 'Authorization': 'Bearer testapikey'},
|
||||||
json={'tags': ['ansible'], 'text': 'test subject', 'isRegion': True, 'timeEnd': 120000, 'panelId': None, 'time': 60000, 'dashboardId': None},
|
json={'tags': ['ansible'], 'text': 'test subject', 'isRegion': True, 'timeEnd': 120000, 'time': 60000},
|
||||||
verify=True,
|
verify=True,
|
||||||
)
|
)
|
||||||
assert sent_messages == 1
|
assert sent_messages == 1
|
||||||
|
|||||||
Reference in New Issue
Block a user