Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create alert json #93

Merged
merged 4 commits into from
Jun 28, 2024
Merged
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
5 changes: 5 additions & 0 deletions docs/synctl-create-alert.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ synctl create alert [options]
--alert-channel <id> alerting channel
--violation-count <int> the number of consecutive failures to trigger an alert
--tag-filter-expression <json> tag filter expression
--from-json <file> Synthetic alert payload
```

## Examples
Expand Down Expand Up @@ -47,5 +48,9 @@ synctl create alert --name "smart alert" \
--violation-count 3 \
--tag-filter-expression '{"type": "EXPRESSION", "logicalOperator": "AND", "elements": []}'
```
Create a smart alert with json payload
```
synctl create alert --from-json alert.json
```

**Note:** To get alert channel, use command `synctl get alert-channel`.
21 changes: 21 additions & 0 deletions synctl/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -1666,6 +1666,14 @@ def set_tag_filter_expression(self, tag_filter_json):
else:
self.exit_synctl(ERROR_CODE, "Tag-filter expression should not be None")

def loads_from_json_file(self, json_file_name):
try:
with open(json_file_name, "r", encoding="utf-8") as json_file1:
json_payload = json_file1.read()
self.smart_alert_config = json.loads(json_payload)
except FileNotFoundError as not_found_e:
self.exit_synctl(ERROR_CODE, not_found_e)

def get_json(self):
"""return payload as json"""
if len(self.smart_alert_config["syntheticTestIds"]) == 0:
Expand Down Expand Up @@ -5052,6 +5060,19 @@ def main():
return
elif get_args.syn_type == SYN_ALERT:
alert_payload = SmartAlertConfiguration()

# --from-json options
# create from a json file
# if use a json file, all options should config in json
if get_args.from_json is not None and get_args.from_json.endswith('.json') :
json_file = get_args.from_json
alert_payload.loads_from_json_file(json_file_name=json_file)
syn_instance.set_synthetic_payload(
payload=alert_payload.get_json())
alert_instance.set_alert_payload(alert_payload.get_json())
alert_instance.create_synthetic_alert()
return

if get_args.name is not None:
alert_payload.set_alert_name(get_args.name)
if get_args.test is not None:
Expand Down