From a625acfd3b1d64ca8a381046dde22a66c182bf7d Mon Sep 17 00:00:00 2001 From: swethalohith2 Date: Fri, 28 Jun 2024 18:19:54 +0530 Subject: [PATCH 1/4] create alert with json payload --- synctl/cli.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/synctl/cli.py b/synctl/cli.py index b5a3fd4..df6935d 100755 --- a/synctl/cli.py +++ b/synctl/cli.py @@ -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: @@ -5052,6 +5060,19 @@ def main(): return elif get_args.syn_type == SYN_ALERT: alert_payload = SmartAlertConfiguration() + + # --from-file, -f options + # create from a json file + # if use a json file, all options should config in json + if get_args.from_file is not None and get_args.from_file.endswith('.json') : + json_file = get_args.from_file + 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: From f9f6d47f917ebd5c96753f8e7d8d156af1a35eac Mon Sep 17 00:00:00 2001 From: swethalohith2 Date: Fri, 28 Jun 2024 18:24:04 +0530 Subject: [PATCH 2/4] create alert with json payload --- docs/synctl-create-alert.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/docs/synctl-create-alert.md b/docs/synctl-create-alert.md index 8f8543e..9a0b1c6 100644 --- a/docs/synctl-create-alert.md +++ b/docs/synctl-create-alert.md @@ -18,6 +18,7 @@ synctl create alert [options] --alert-channel alerting channel --violation-count the number of consecutive failures to trigger an alert --tag-filter-expression tag filter expression + -f, --from-file Synthetic alert payload ``` ## Examples @@ -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-file alert.json + ``` **Note:** To get alert channel, use command `synctl get alert-channel`. From 77814ce47a17f39b5dced2ba522d183b8fa73ce2 Mon Sep 17 00:00:00 2001 From: swethalohith2 Date: Fri, 28 Jun 2024 18:52:29 +0530 Subject: [PATCH 3/4] use --from-json to keep align with create test command --- docs/synctl-create-alert.md | 4 ++-- synctl/cli.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/synctl-create-alert.md b/docs/synctl-create-alert.md index 9a0b1c6..c94fffa 100644 --- a/docs/synctl-create-alert.md +++ b/docs/synctl-create-alert.md @@ -18,7 +18,7 @@ synctl create alert [options] --alert-channel alerting channel --violation-count the number of consecutive failures to trigger an alert --tag-filter-expression tag filter expression - -f, --from-file Synthetic alert payload + --from-json Synthetic alert payload ``` ## Examples @@ -50,7 +50,7 @@ synctl create alert --name "smart alert" \ ``` Create a smart alert with json payload ``` - synctl create alert --from-file alert.json + synctl create alert --from-json alert.json ``` **Note:** To get alert channel, use command `synctl get alert-channel`. diff --git a/synctl/cli.py b/synctl/cli.py index df6935d..1140d97 100755 --- a/synctl/cli.py +++ b/synctl/cli.py @@ -5064,8 +5064,8 @@ def main(): # --from-file, -f options # create from a json file # if use a json file, all options should config in json - if get_args.from_file is not None and get_args.from_file.endswith('.json') : - json_file = get_args.from_file + 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()) From ce7ae236b9871486c9b30fdb655b3cfbf3803d76 Mon Sep 17 00:00:00 2001 From: swethalohith2 Date: Fri, 28 Jun 2024 18:53:41 +0530 Subject: [PATCH 4/4] use --from-json to keep align with create test command --- synctl/cli.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/synctl/cli.py b/synctl/cli.py index 1140d97..511e851 100755 --- a/synctl/cli.py +++ b/synctl/cli.py @@ -5061,7 +5061,7 @@ def main(): elif get_args.syn_type == SYN_ALERT: alert_payload = SmartAlertConfiguration() - # --from-file, -f options + # --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') :