Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions docs/content/grafana_api/model.md
Original file line number Diff line number Diff line change
Expand Up @@ -327,10 +327,10 @@ The class includes all necessary variables to generate an alert rule route that

**Arguments**:

- `continue_parameter` _bool_ - Specify the continue parameter
- `group_by_str` _List[str]_ - Specify the list of group by strings
- `receiver` _str_ - Specify the receiver
- `provenance` _str_ - Specify the provenance
- `continue_parameter` _bool_ - Specify the continue parameter (default None)
- `group_by_str` _List[str]_ - Specify the list of group by strings (default None)
- `provenance` _str_ - Specify the provenance (default None)
- `object_matchers` _List[Matcher]_ - Specify the list of object matchers (default None)
- `group_interval` _str_ - Specify the group time interval (default None)
- `group_wait` _str_ - Specify the group wait time (default None)
Expand Down
10 changes: 6 additions & 4 deletions grafana_api/alerting_provisioning.py
Original file line number Diff line number Diff line change
Expand Up @@ -755,8 +755,9 @@ def _create_alert_route_dictionary(self, route: Route) -> dict:
result (dict): Returns the alert route dictionary
"""

return dict(
{
return {
k: v
for k, v in {
"continue": route.continue_parameter,
"group_by": route.group_by_str,
"mute_time_intervals": route.mute_time_intervals,
Expand All @@ -769,8 +770,9 @@ def _create_alert_route_dictionary(self, route: Route) -> dict:
),
"provenance": route.provenance,
"repeat_interval": route.repeat_interval,
}
)
}.items()
if v is not None
}

def _create_alert_routes_list(self, routes: List[Route]) -> (list, None):
"""The method includes a functionality to create the alert route list
Expand Down
12 changes: 6 additions & 6 deletions grafana_api/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,10 +345,10 @@ class Route:
"""The class includes all necessary variables to generate an alert rule route that is necessary to communicate with the Grafana alert provisioning endpoint

Args:
continue_parameter (bool): Specify the continue parameter
group_by_str (List[str]): Specify the list of group by strings
receiver (str): Specify the receiver
provenance (str): Specify the provenance
continue_parameter (bool): Specify the continue parameter (default None)
group_by_str (List[str]): Specify the list of group by strings (default None)
provenance (str): Specify the provenance (default None)
object_matchers (List[Matcher]): Specify the list of object matchers (default None)
group_interval (str): Specify the group time interval (default None)
group_wait (str): Specify the group wait time (default None)
Expand All @@ -357,10 +357,10 @@ class Route:
mute_time_intervals (List[str]): Specify the mute time interval as list (default None)
"""

continue_parameter: bool
group_by_str: List[str]
receiver: str
provenance: str
continue_parameter: bool = None
group_by_str: List[str] = None
provenance: str = None
object_matchers: List[Matcher] = None
group_interval: str = None
group_wait: str = None
Expand Down
4 changes: 2 additions & 2 deletions tests/integrationtest/test_alerting_provisioning.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,9 @@ def test_j_get_notification_policies(self):

def test_k_add_notification_policies(self):
route: Route = Route(
"test1",
False,
["grafana_folder", "alertname"],
"test1",
"test",
group_wait="5m",
group_interval="30s",
Expand Down Expand Up @@ -229,9 +229,9 @@ def test_u_delete_message_template(self):

def doCleanups(self):
route: Route = Route(
"grafana-default-email",
False,
["grafana_folder", "alertname"],
"grafana-default-email",
"test",
group_wait="5m",
group_interval="30s",
Expand Down
19 changes: 17 additions & 2 deletions tests/unittests/test_alerting_provisioning.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ def setUp(self):
"test", "test", {"test": "test"}
)
matcher: Matcher = Matcher("test", MatchType.MatchEqual, "test")
route_2: Route = Route(False, ["test"], "test", "test")
route_2: Route = Route("test", False, ["test"], "test")
self.route: Route = Route(
"test",
False,
["test"],
"test",
"test",
object_matchers=[matcher],
routes=[route_2],
)
Expand Down Expand Up @@ -386,6 +386,21 @@ def test_add_notification_policies(self, call_the_api_mock):
None, alerting_provisioning.add_notification_policies(self.route)
)

@patch("grafana_api.api.Api.call_the_api")
def test_add_notification_policies_receiver_only(self, call_the_api_mock):
model: APIModel = APIModel(host=MagicMock(), token=MagicMock())
alerting_provisioning: AlertingProvisioning = AlertingProvisioning(
grafana_api_model=model
)

call_the_api_mock.return_value = dict({"status": 201})

minimal_route: Route = Route("grafana-default-email")
self.assertEqual(
None,
alerting_provisioning.add_notification_policies(minimal_route),
)

def test_add_notification_policies_no_rule(self):
model: APIModel = APIModel(host=MagicMock(), token=MagicMock())
alerting_provisioning: AlertingProvisioning = AlertingProvisioning(
Expand Down