Skip to content

Commit 399cd6a

Browse files
authored
Merge ff3c3a7 into 161dfd5
2 parents 161dfd5 + ff3c3a7 commit 399cd6a

File tree

5 files changed

+97
-104
lines changed

5 files changed

+97
-104
lines changed

docs/content/grafana_api/model.md

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ The class includes all necessary variables to establish a connection to the Graf
9090
- `ssl_context` _ssl.SSLContext_ - Specify the custom ssl context of the Grafana system
9191
- `num_pools` _int_ - Specify the number of the connection pool
9292
- `retries` _any_ - Specify the number of the retries. Please use False as parameter to disable the retries
93+
- `follow_redirects` _bool_ - Specify if redirections should be followed (default True)
9394

9495
<a id="model.DatasourceQuery"></a>
9596

@@ -421,12 +422,12 @@ The class includes all necessary variables to generate an Alertmanager config ob
421422

422423
**Arguments**:
423424

424-
- `global_config` _dict_ - Specify the global config of the Alertmanager
425-
- `inhibit_rules` _list_ - Specify the inhibit rules of the Alertmanager
426-
- `mute_time_intervals` _list_ - Specify the mute time intervals of the Alertmanager
425+
- `global_config` _Union[dict, None]_ - Specify the global config of the Alertmanager
426+
- `inhibit_rules` _Union[list, None]_ - Specify the inhibit rules of the Alertmanager
427+
- `mute_time_intervals` _Union[list, None]_ - Specify the mute time intervals of the Alertmanager
427428
- `receivers` _list_ - Specify the receiver's of the Alertmanager
428429
- `route` _dict_ - Specify the route of the Alertmanager
429-
- `templates` _list_ - Specify an Alertmanager template
430+
- `templates` _Union[list, None]_ - Specify an Alertmanager template
430431

431432
<a id="model.AlertmanagerReceivers"></a>
432433

@@ -442,15 +443,16 @@ The class includes all necessary variables to generate an Alertmanager receiver'
442443
**Arguments**:
443444

444445
- `name` _str_ - Specify the name of the receiver
445-
- `email_configs` _list_ - Specify the email configuration of the receiver's
446+
- `email_configs` _Union[list, None]_ - Specify the email configuration of the receiver's
446447
- `grafana_managed_receiver_configs` _list_ - Specify the Grafana managed receiver configuration of the receiver's
447-
- `opsgenie_configs` _list_ - Specify the ops genie configuration of the receiver's
448-
- `pagerduty_configs` _dict_ - Specify the pager duty configuration of the receiver's
449-
- `pushover_configs` _list_ - Specify the push over configuration of the receiver's
450-
- `slack_configs` _list_ - Specify the Slack configuration of the receiver's
451-
- `victorops_configs` _list_ - Specify the victor ops configuration of the receiver's
452-
- `webhook_configs` _list_ - Specify the webhook configuration of the receiver's
453-
- `wechat_configs` _list_ - Specify the wechaty configuration of the receiver's
448+
- `opsgenie_configs` _Union[list, None]_ - Specify the ops genie configuration of the receiver's
449+
- `pagerduty_configs` _Union[list, None]_ - Specify the pager duty configuration of the receiver's
450+
- `pushover_configs` _Union[list, None]_ - Specify the push over configuration of the receiver's
451+
- `slack_configs` _Union[list, None]_ - Specify the Slack configuration of the receiver's
452+
- `sns_configs` _Union[list, None]_ - Specify the SNS configuration of the receiver's
453+
- `victorops_configs` _Union[list, None]_ - Specify the victor ops configuration of the receiver's
454+
- `webhook_configs` _Union[list, None]_ - Specify the webhook configuration of the receiver's
455+
- `wechat_configs` _Union[list, None]_ - Specify the wechaty configuration of the receiver's
454456

455457
<a id="model.RulerRule"></a>
456458

grafana_api/alerting.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -773,11 +773,11 @@ def get_ruler_group(
773773
)
774774
raise ValueError
775775

776-
def test_rule(self, data_query: list) -> dict:
776+
def test_rule(self, data_queries: list) -> dict:
777777
"""The method includes a functionality to test a rule specified by a list of datasource rule query objects
778778
779779
Args:
780-
data_query (list): Specify a list of datasource rule query objects
780+
data_queries (list): Specify a list of datasource rule query objects
781781
782782
Raises:
783783
ValueError: Missed specifying a necessary value
@@ -787,11 +787,11 @@ def test_rule(self, data_query: list) -> dict:
787787
api_call (str): Returns the result of the specified query
788788
"""
789789

790-
if data_query != list():
790+
if data_queries != list():
791791
datasource_rule_query_objects_json: list = list()
792792
datasource_rule_query_object_json: dict = dict()
793793

794-
for datasource_rule_query_object in data_query:
794+
for datasource_rule_query_object in data_queries:
795795
datasource_rule_query_object_json["datasourceUid"] = (
796796
datasource_rule_query_object.datasource_uid
797797
)
@@ -828,22 +828,22 @@ def test_rule(self, data_query: list) -> dict:
828828
else:
829829
return api_call
830830
else:
831-
logging.error("There is no data_query defined.")
831+
logging.error("There is no data_queries defined.")
832832
raise ValueError
833833

834834
def test_datasource_uid_rule(
835835
self,
836836
expr: str,
837837
condition: str,
838-
data_query: list,
838+
data_queries: list,
839839
datasource_uid: str = "grafana",
840840
) -> dict:
841841
"""The method includes a functionality to test a datasource uid rule specified by the expr, the condition, a list of data queries and the datasource_uid
842842
843843
Args:
844844
expr (str): Specify a list of datasource rule query objects
845845
condition (str): Specify the condition
846-
data_query (list): Specify a list of datasource rule query objects
846+
data_queries (list): Specify a list of datasource rule query objects
847847
datasource_uid (str): Specify the datasource uid or recipient of the alerts (default grafana)
848848
849849
Raises:
@@ -858,12 +858,12 @@ def test_datasource_uid_rule(
858858
len(datasource_uid) != 0
859859
and len(expr) != 0
860860
and len(condition) != 0
861-
and data_query != list()
861+
and data_queries != list()
862862
):
863863
datasource_rule_query_objects_json: list = list()
864864
datasource_rule_query_object_json: dict = dict()
865865

866-
for datasource_rule_query_object in data_query:
866+
for datasource_rule_query_object in data_queries:
867867
datasource_rule_query_object_json["datasourceUid"] = (
868868
datasource_rule_query_object.datasource_uid
869869
)
@@ -889,7 +889,7 @@ def test_datasource_uid_rule(
889889
json.dumps(
890890
{
891891
"expr": expr,
892-
"grafana_alert": {
892+
"grafana_condition": {
893893
"condition": condition,
894894
"data": datasource_rule_query_objects_json,
895895
"now": str(datetime.datetime.now()),
@@ -905,16 +905,16 @@ def test_datasource_uid_rule(
905905
return api_call
906906
else:
907907
logging.error(
908-
"There is no datasource_uid, expr, condition or data_query defined."
908+
"There is no datasource_uid, expr, condition or data_queries defined."
909909
)
910910
raise ValueError
911911

912-
def test_backtest_rule(self, condition: str, data_query: list) -> dict:
913-
"""The method includes a functionality to test a rule specified by the condition, a list of data queries
912+
def test_backtest_rule(self, condition: str, data_queries: list) -> dict:
913+
"""The method includes a functionality to test a rule specified by the condition and a list of data queries
914914
915915
Args:
916916
condition (str): Specify the condition
917-
data_query (list): Specify a list of datasource rule query objects
917+
data_queries (list): Specify a list of datasource rule query objects
918918
919919
Raises:
920920
ValueError: Missed specifying a necessary value
@@ -924,11 +924,11 @@ def test_backtest_rule(self, condition: str, data_query: list) -> dict:
924924
api_call (dict): Returns the result of the specified rule
925925
"""
926926

927-
if len(condition) != 0 and data_query != list():
927+
if len(condition) != 0 and data_queries != list():
928928
datasource_rule_query_objects_json: list = list()
929929
datasource_rule_query_object_json: dict = dict()
930930

931-
for datasource_rule_query_object in data_query:
931+
for datasource_rule_query_object in data_queries:
932932
datasource_rule_query_object_json["datasourceUid"] = (
933933
datasource_rule_query_object.datasource_uid
934934
)
@@ -966,7 +966,7 @@ def test_backtest_rule(self, condition: str, data_query: list) -> dict:
966966
return api_call
967967
else:
968968
logging.error(
969-
"There is no datasource_uid, condition or data_query defined."
969+
"There is no condition or data_queries defined."
970970
)
971971
raise ValueError
972972

grafana_api/api.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,7 @@ def create_the_http_api_client(
343343
headers=headers,
344344
transport=async_transport,
345345
verify=self.grafana_api_model.ssl_context,
346+
follow_redirects=self.grafana_api_model.follow_redirects,
346347
)
347348
else:
348349
return httpx.Client(
@@ -351,4 +352,5 @@ def create_the_http_api_client(
351352
headers=headers,
352353
transport=transport,
353354
verify=self.grafana_api_model.ssl_context,
355+
follow_redirects=self.grafana_api_model.follow_redirects,
354356
)

grafana_api/model.py

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ class APIModel:
8686
ssl_context (ssl.SSLContext): Specify the custom ssl context of the Grafana system
8787
num_pools (int): Specify the number of the connection pool
8888
retries (any): Specify the number of the retries. Please use False as parameter to disable the retries
89+
follow_redirects (bool): Specify if redirections should be followed (default True)
8990
"""
9091

9192
host: str
@@ -98,6 +99,7 @@ class APIModel:
9899
ssl_context: ssl.SSLContext = httpx.create_ssl_context()
99100
num_pools: int = 10
100101
retries: any = 10
102+
follow_redirects: bool = True
101103

102104

103105
@dataclass
@@ -438,20 +440,20 @@ class AlertmanagerConfig:
438440
"""The class includes all necessary variables to generate an Alertmanager config object that is necessary to communicate and set up the Grafana Alertmanager endpoint
439441
440442
Args:
441-
global_config (dict): Specify the global config of the Alertmanager
442-
inhibit_rules (list): Specify the inhibit rules of the Alertmanager
443-
mute_time_intervals (list): Specify the mute time intervals of the Alertmanager
443+
global_config (Union[dict, None]): Specify the global config of the Alertmanager
444+
inhibit_rules (Union[list, None]): Specify the inhibit rules of the Alertmanager
445+
mute_time_intervals (Union[list, None]): Specify the mute time intervals of the Alertmanager
444446
receivers (list): Specify the receiver's of the Alertmanager
445447
route (dict): Specify the route of the Alertmanager
446-
templates (list): Specify an Alertmanager template
448+
templates (Union[list, None]): Specify an Alertmanager template
447449
"""
448450

449-
global_config: dict
450-
inhibit_rules: list
451-
mute_time_intervals: list
451+
global_config: Union[dict, None]
452+
inhibit_rules: Union[list, None]
453+
mute_time_intervals: Union[list, None]
452454
receivers: list
453455
route: dict
454-
templates: list
456+
templates: Union[list, None]
455457

456458

457459
@dataclass
@@ -460,28 +462,29 @@ class AlertmanagerReceivers:
460462
461463
Args:
462464
name (str): Specify the name of the receiver
463-
email_configs (list): Specify the email configuration of the receiver's
465+
email_configs (Union[list, None]): Specify the email configuration of the receiver's
464466
grafana_managed_receiver_configs (list): Specify the Grafana managed receiver configuration of the receiver's
465-
opsgenie_configs (list): Specify the ops genie configuration of the receiver's
466-
pagerduty_configs (dict): Specify the pager duty configuration of the receiver's
467-
pushover_configs (list): Specify the push over configuration of the receiver's
468-
slack_configs (list): Specify the Slack configuration of the receiver's
469-
victorops_configs (list): Specify the victor ops configuration of the receiver's
470-
webhook_configs (list): Specify the webhook configuration of the receiver's
471-
wechat_configs (list): Specify the wechaty configuration of the receiver's
467+
opsgenie_configs (Union[list, None]): Specify the ops genie configuration of the receiver's
468+
pagerduty_configs (Union[list, None]): Specify the pager duty configuration of the receiver's
469+
pushover_configs (Union[list, None]): Specify the push over configuration of the receiver's
470+
slack_configs (Union[list, None]): Specify the Slack configuration of the receiver's
471+
sns_configs (Union[list, None]): Specify the SNS configuration of the receiver's
472+
victorops_configs (Union[list, None]): Specify the victor ops configuration of the receiver's
473+
webhook_configs (Union[list, None]): Specify the webhook configuration of the receiver's
474+
wechat_configs (Union[list, None]): Specify the wechaty configuration of the receiver's
472475
"""
473476

474477
name: str
475-
email_configs: list
478+
email_configs: Union[list, None]
476479
grafana_managed_receiver_configs: list
477-
opsgenie_configs: list
478-
pagerduty_configs: list
479-
pushover_configs: list
480-
slack_configs: list
481-
sns_configs: list
482-
victorops_configs: list
483-
webhook_configs: list
484-
wechat_configs: list
480+
opsgenie_configs: Union[list, None]
481+
pagerduty_configs: Union[list, None]
482+
pushover_configs: Union[list, None]
483+
slack_configs: Union[list, None]
484+
sns_configs: Union[list, None]
485+
victorops_configs: Union[list, None]
486+
webhook_configs: Union[list, None]
487+
wechat_configs: Union[list, None]
485488

486489

487490
@dataclass

tests/integrationtest/test_alerting.py

Lines changed: 36 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from grafana_api.model import (
77
APIModel,
88
AlertmanagerConfig,
9+
AlertmanagerReceivers,
910
DatasourceRuleQuery,
1011
)
1112
from grafana_api.alerting import Alerting
@@ -107,40 +108,40 @@ def test_c_delete_alertmanager_config(self):
107108

108109
self.assertEqual(result, self.alerting.get_alertmanager_config())
109110

110-
# def test_test_alertmanager_receivers(self):
111-
# grafana_managed_receiver_configs: list = [
112-
# {
113-
# "uid": None,
114-
# "name": "email receiver",
115-
# "type": "email",
116-
# "disableResolveMessage": False,
117-
# "settings": {"addresses": "[email protected]"},
118-
# "secureFields": {},
119-
# }
120-
# ]
121-
#
122-
# alertmangager_receivers: AlertmanagerReceivers = AlertmanagerReceivers(
123-
# name="Test",
124-
# email_configs=None,
125-
# grafana_managed_receiver_configs=grafana_managed_receiver_configs,
126-
# opsgenie_configs=None,
127-
# pagerduty_configs=None,
128-
# pushover_configs=None,
129-
# slack_configs=None,
130-
# sns_configs=None,
131-
# victorops_configs=None,
132-
# webhook_configs=None,
133-
# wechat_configs=None,
134-
# )
135-
# self.assertEqual(
136-
# None,
137-
# self.alerting.test_alertmanager_receivers(
138-
# alert=dict(
139-
# {"annotations": {"test": "test"}, "labels": {"test": "test"}}
140-
# ),
141-
# receivers=list([alertmangager_receivers]),
142-
# ),
143-
# )
111+
def test_test_alertmanager_receivers(self):
112+
grafana_managed_receiver_configs: list = [
113+
{
114+
"uid": None,
115+
"name": "email receiver",
116+
"type": "email",
117+
"disableResolveMessage": False,
118+
"settings": {"addresses": "[email protected]"},
119+
"secureFields": {},
120+
}
121+
]
122+
123+
alertmangager_receivers: AlertmanagerReceivers = AlertmanagerReceivers(
124+
name="Test",
125+
email_configs=None,
126+
grafana_managed_receiver_configs=grafana_managed_receiver_configs,
127+
opsgenie_configs=None,
128+
pagerduty_configs=None,
129+
pushover_configs=None,
130+
slack_configs=None,
131+
sns_configs=None,
132+
victorops_configs=None,
133+
webhook_configs=None,
134+
wechat_configs=None,
135+
)
136+
self.assertEqual(
137+
None,
138+
self.alerting.test_alertmanager_receivers(
139+
alert=dict(
140+
{"annotations": {"test": "test"}, "labels": {"test": "test"}}
141+
),
142+
receivers=list([alertmangager_receivers]),
143+
),
144+
)
144145

145146
def test_get_prometheus_alerts(self):
146147
MAX_TRIES: int = 3
@@ -190,19 +191,4 @@ def test_test_rule(self):
190191
data_queries: list = list([datasource_rule_query])
191192

192193
with self.assertRaises(Exception):
193-
self.alerting.test_rule(data_queries)
194-
195-
def test_test_recipient_rule(self):
196-
datasource_rule_query: DatasourceRuleQuery = DatasourceRuleQuery(
197-
datasource_uid="test",
198-
model=dict(),
199-
query_type="",
200-
ref_id="test1",
201-
relative_time_range=dict({"from": 20, "to": 90}),
202-
)
203-
data_queries: list = list([datasource_rule_query])
204-
205-
with self.assertRaises(Exception):
206-
self.alerting.test_recipient_rule(
207-
expr="test", condition="test", data_query=data_queries
208-
)
194+
self.alerting.test_rule(data_queries)

0 commit comments

Comments
 (0)