diff --git a/awx/main/notifications/grafana_backend.py b/awx/main/notifications/grafana_backend.py index 51a27a897e..0d7dff9e05 100644 --- a/awx/main/notifications/grafana_backend.py +++ b/awx/main/notifications/grafana_backend.py @@ -54,8 +54,8 @@ class GrafanaBackend(AWXBaseEmailBackend, CustomNotificationBase): ): super(GrafanaBackend, self).__init__(fail_silently=fail_silently) self.grafana_key = grafana_key - self.dashboardId = dashboardId - self.panelId = panelId + self.dashboardId = int(dashboardId) if dashboardId is not None else None + self.panelId = int(panelId) if panelId is not None else None self.annotation_tags = annotation_tags if annotation_tags is not None else [] self.grafana_no_verify_ssl = grafana_no_verify_ssl self.isRegion = isRegion @@ -86,8 +86,10 @@ class GrafanaBackend(AWXBaseEmailBackend, CustomNotificationBase): if not self.fail_silently: 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['dashboardId'] = self.dashboardId - grafana_data['panelId'] = self.panelId + if self.dashboardId is not None: + grafana_data['dashboardId'] = self.dashboardId + if self.panelId is not None: + grafana_data['panelId'] = self.panelId if self.annotation_tags: grafana_data['tags'] = self.annotation_tags grafana_data['text'] = m.subject diff --git a/awx/main/tests/unit/notifications/test_grafana.py b/awx/main/tests/unit/notifications/test_grafana.py index ccf4241dbb..70750e3315 100644 --- a/awx/main/tests/unit/notifications/test_grafana.py +++ b/awx/main/tests/unit/notifications/test_grafana.py @@ -1,6 +1,7 @@ from unittest import mock import datetime as dt from django.core.mail.message import EmailMessage +import pytest import awx.main.notifications.grafana_backend as grafana_backend @@ -29,7 +30,7 @@ def test_send_messages(): requests_mock.post.assert_called_once_with( 'https://example.com/api/annotations', 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, ) assert sent_messages == 1 @@ -59,20 +60,21 @@ def test_send_messages_with_no_verify_ssl(): requests_mock.post.assert_called_once_with( 'https://example.com/api/annotations', 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, ) 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: requests_mock.post.return_value.status_code = 200 m = {} m['started'] = dt.datetime.utcfromtimestamp(60).isoformat() m['finished'] = dt.datetime.utcfromtimestamp(120).isoformat() m['subject'] = "test subject" - backend = grafana_backend.GrafanaBackend("testapikey", dashboardId=42) + backend = grafana_backend.GrafanaBackend("testapikey", dashboardId=dashboardId) message = EmailMessage( m['subject'], {"started": m['started'], "finished": m['finished']}, @@ -89,20 +91,21 @@ def test_send_messages_with_dashboardid(): requests_mock.post.assert_called_once_with( 'https://example.com/api/annotations', 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, ) 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: requests_mock.post.return_value.status_code = 200 m = {} m['started'] = dt.datetime.utcfromtimestamp(60).isoformat() m['finished'] = dt.datetime.utcfromtimestamp(120).isoformat() m['subject'] = "test subject" - backend = grafana_backend.GrafanaBackend("testapikey", dashboardId=None, panelId=42) + backend = grafana_backend.GrafanaBackend("testapikey", dashboardId=None, panelId=panelId) message = EmailMessage( m['subject'], {"started": m['started'], "finished": m['finished']}, @@ -119,7 +122,7 @@ def test_send_messages_with_panelid(): requests_mock.post.assert_called_once_with( 'https://example.com/api/annotations', 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, ) assert sent_messages == 1 @@ -179,7 +182,7 @@ def test_send_messages_with_tags(): requests_mock.post.assert_called_once_with( 'https://example.com/api/annotations', 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, ) assert sent_messages == 1