Skip to content

Commit a30fc83

Browse files
Create alert json (#93)
* create alert with json payload * create alert with json payload * use --from-json to keep align with create test command * use --from-json to keep align with create test command
1 parent 834f9f2 commit a30fc83

2 files changed

Lines changed: 26 additions & 0 deletions

File tree

docs/synctl-create-alert.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ synctl create alert [options]
1818
--alert-channel <id> alerting channel
1919
--violation-count <int> the number of consecutive failures to trigger an alert
2020
--tag-filter-expression <json> tag filter expression
21+
--from-json <file> Synthetic alert payload
2122
```
2223

2324
## Examples
@@ -47,5 +48,9 @@ synctl create alert --name "smart alert" \
4748
--violation-count 3 \
4849
--tag-filter-expression '{"type": "EXPRESSION", "logicalOperator": "AND", "elements": []}'
4950
```
51+
Create a smart alert with json payload
52+
```
53+
synctl create alert --from-json alert.json
54+
```
5055

5156
**Note:** To get alert channel, use command `synctl get alert-channel`.

synctl/cli.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1666,6 +1666,14 @@ def set_tag_filter_expression(self, tag_filter_json):
16661666
else:
16671667
self.exit_synctl(ERROR_CODE, "Tag-filter expression should not be None")
16681668

1669+
def loads_from_json_file(self, json_file_name):
1670+
try:
1671+
with open(json_file_name, "r", encoding="utf-8") as json_file1:
1672+
json_payload = json_file1.read()
1673+
self.smart_alert_config = json.loads(json_payload)
1674+
except FileNotFoundError as not_found_e:
1675+
self.exit_synctl(ERROR_CODE, not_found_e)
1676+
16691677
def get_json(self):
16701678
"""return payload as json"""
16711679
if len(self.smart_alert_config["syntheticTestIds"]) == 0:
@@ -5052,6 +5060,19 @@ def main():
50525060
return
50535061
elif get_args.syn_type == SYN_ALERT:
50545062
alert_payload = SmartAlertConfiguration()
5063+
5064+
# --from-json options
5065+
# create from a json file
5066+
# if use a json file, all options should config in json
5067+
if get_args.from_json is not None and get_args.from_json.endswith('.json') :
5068+
json_file = get_args.from_json
5069+
alert_payload.loads_from_json_file(json_file_name=json_file)
5070+
syn_instance.set_synthetic_payload(
5071+
payload=alert_payload.get_json())
5072+
alert_instance.set_alert_payload(alert_payload.get_json())
5073+
alert_instance.create_synthetic_alert()
5074+
return
5075+
50555076
if get_args.name is not None:
50565077
alert_payload.set_alert_name(get_args.name)
50575078
if get_args.test is not None:

0 commit comments

Comments
 (0)