Skip to content

Commit 7b2321d

Browse files
authored
Adjust the alerting functionality and add redirections support (#117)
* feat: Adjust the alerting model * feat: Add redirect support * fix: Adjust the alerting functionality * fix: Adjust the linting issues
1 parent 161dfd5 commit 7b2321d

File tree

6 files changed

+103
-110
lines changed

6 files changed

+103
-110
lines changed

docs/content/grafana_api/alerting.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -595,14 +595,14 @@ The method includes a functionality to get a ruler group specified by the namesp
595595
#### test\_rule
596596

597597
```python
598-
def test_rule(data_query: list) -> dict
598+
def test_rule(data_queries: list) -> dict
599599
```
600600

601601
The method includes a functionality to test a rule specified by a list of datasource rule query objects
602602

603603
**Arguments**:
604604

605-
- `data_query` _list_ - Specify a list of datasource rule query objects
605+
- `data_queries` _list_ - Specify a list of datasource rule query objects
606606

607607

608608
**Raises**:
@@ -622,7 +622,7 @@ The method includes a functionality to test a rule specified by a list of dataso
622622
```python
623623
def test_datasource_uid_rule(expr: str,
624624
condition: str,
625-
data_query: list,
625+
data_queries: list,
626626
datasource_uid: str = "grafana") -> dict
627627
```
628628

@@ -632,7 +632,7 @@ The method includes a functionality to test a datasource uid rule specified by t
632632

633633
- `expr` _str_ - Specify a list of datasource rule query objects
634634
- `condition` _str_ - Specify the condition
635-
- `data_query` _list_ - Specify a list of datasource rule query objects
635+
- `data_queries` _list_ - Specify a list of datasource rule query objects
636636
- `datasource_uid` _str_ - Specify the datasource uid or recipient of the alerts (default grafana)
637637

638638

@@ -651,15 +651,15 @@ The method includes a functionality to test a datasource uid rule specified by t
651651
#### test\_backtest\_rule
652652

653653
```python
654-
def test_backtest_rule(condition: str, data_query: list) -> dict
654+
def test_backtest_rule(condition: str, data_queries: list) -> dict
655655
```
656656

657-
The method includes a functionality to test a rule specified by the condition, a list of data queries
657+
The method includes a functionality to test a rule specified by the condition and a list of data queries
658658

659659
**Arguments**:
660660

661661
- `condition` _str_ - Specify the condition
662-
- `data_query` _list_ - Specify a list of datasource rule query objects
662+
- `data_queries` _list_ - Specify a list of datasource rule query objects
663663

664664

665665
**Raises**:

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

0 commit comments

Comments
 (0)