From c689a73637c7cdfeaa33d5f4fa58a9b8d7adb98f Mon Sep 17 00:00:00 2001 From: pierreln-dd Date: Tue, 26 Dec 2023 11:20:46 +0100 Subject: [PATCH 01/12] MNTS-90348 Adds restricted_roles to monitors API --- datadog/dogshell/monitor.py | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/datadog/dogshell/monitor.py b/datadog/dogshell/monitor.py index ddc207e5a..fae89f995 100644 --- a/datadog/dogshell/monitor.py +++ b/datadog/dogshell/monitor.py @@ -36,6 +36,9 @@ def setup_parser(cls, subparsers): post_parser.add_argument( "--message", help="message to include with notifications" " for this monitor", default=None ) + post_parser.add_argument( + "--restricted_roles", help="list of unique role identifiers allowed to edit the monitor", default=None + ) post_parser.add_argument("--tags", help="comma-separated list of tags", default=None) post_parser.add_argument( "--priority", @@ -151,6 +154,9 @@ def setup_parser(cls, subparsers): validate_parser.add_argument( "--message", help="message to include with notifications" " for this monitor", default=None ) + validate_parser.add_argument( + "--restricted_roles", help="list of unique role identifiers allowed to edit the monitor", default=None + ) validate_parser.add_argument("--tags", help="comma-separated list of tags", default=None) validate_parser.add_argument("--options", help="json options for the monitor", default=None) validate_parser.set_defaults(func=cls._validate) @@ -175,6 +181,11 @@ def _post(cls, args): "message": args.message, "options": options } + if args.restricted_roles: + restricted_roles = sorted(set([rr.strip() for rr in args.restricted_roles.split(",") if rr.strip()])) + body["restricted_roles"] = restricted_roles + else: + restricted_roles = None if tags: body["tags"] = tags if args.priority: @@ -200,6 +211,9 @@ def _file_post(cls, args): "message": monitor["message"], "options": monitor["options"] } + restricted_roles = monitor.get("restricted_roles", None) + if restricted_roles: + body["restricted_roles"] = restricted_roles tags = monitor.get("tags", None) if tags: body["tags"] = tags @@ -245,6 +259,8 @@ def _update(cls, args): to_update["type"] = args.type_opt if args.query_opt: to_update["query"] = args.query_opt + if args.restricted_roles: + to_update["restricted_roles"] = sorted(set([rr.strip() for rr in args.tags.split(",") if rr.strip()])) if args.tags: to_update["tags"] = sorted(set([t.strip() for t in args.tags.split(",") if t.strip()])) if args.priority: @@ -274,6 +290,9 @@ def _file_update(cls, args): "message": monitor["message"], "options": monitor["options"] } + restricted_roles = monitor.get("restricted_roles", None) + if restricted_roles: + body["restricted_roles"] = restricted_roles tags = monitor.get("tags", None) if tags: body["tags"] = tags @@ -414,7 +433,10 @@ def _validate(cls, args): options = None if args.options is not None: options = json.loads(args.options) - + if args.restricted_roles: + to_update["restricted_roles"] = sorted(set([rr.strip() for rr in args.tags.split(",") if rr.strip()])) + else: + restricted_roles = None if args.tags: tags = sorted(set([t.strip() for t in args.tags.split(",") if t.strip()])) else: From a7224e3273ab2318c15bea207f8890df42323376 Mon Sep 17 00:00:00 2001 From: pierreln-dd Date: Tue, 26 Dec 2023 11:24:11 +0100 Subject: [PATCH 02/12] add restricted_roles to update_parser --- datadog/dogshell/monitor.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/datadog/dogshell/monitor.py b/datadog/dogshell/monitor.py index fae89f995..a8b7a0c05 100644 --- a/datadog/dogshell/monitor.py +++ b/datadog/dogshell/monitor.py @@ -77,6 +77,9 @@ def setup_parser(cls, subparsers): dest="query_opt", ) update_parser.add_argument("--name", help="name of the alert", default=None) + update_parser.add_argument( + "--restricted_roles", help="list of unique role identifiers allowed to edit the monitor", default=None + ) update_parser.add_argument("--tags", help="comma-separated list of tags", default=None) update_parser.add_argument( "--message", help="message to include with " "notifications for this monitor", default=None From 56af17c4db8b8a98fc8997c870b660b87ddbe520 Mon Sep 17 00:00:00 2001 From: pierreln-dd Date: Tue, 26 Dec 2023 17:40:51 +0100 Subject: [PATCH 03/12] Specify comma-separated for restricted_roles --- datadog/dogshell/monitor.py | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/datadog/dogshell/monitor.py b/datadog/dogshell/monitor.py index a8b7a0c05..b27cdab7d 100644 --- a/datadog/dogshell/monitor.py +++ b/datadog/dogshell/monitor.py @@ -37,7 +37,7 @@ def setup_parser(cls, subparsers): "--message", help="message to include with notifications" " for this monitor", default=None ) post_parser.add_argument( - "--restricted_roles", help="list of unique role identifiers allowed to edit the monitor", default=None + "--restricted_roles", help="comma-separated list of unique role identifiers allowed to edit the monitor", default=None ) post_parser.add_argument("--tags", help="comma-separated list of tags", default=None) post_parser.add_argument( @@ -78,7 +78,7 @@ def setup_parser(cls, subparsers): ) update_parser.add_argument("--name", help="name of the alert", default=None) update_parser.add_argument( - "--restricted_roles", help="list of unique role identifiers allowed to edit the monitor", default=None + "--restricted_roles", help="comma-separated list of unique role identifiers allowed to edit the monitor", default=None ) update_parser.add_argument("--tags", help="comma-separated list of tags", default=None) update_parser.add_argument( @@ -158,7 +158,7 @@ def setup_parser(cls, subparsers): "--message", help="message to include with notifications" " for this monitor", default=None ) validate_parser.add_argument( - "--restricted_roles", help="list of unique role identifiers allowed to edit the monitor", default=None + "--restricted_roles", help="comma-separated list of unique role identifiers allowed to edit the monitor", default=None ) validate_parser.add_argument("--tags", help="comma-separated list of tags", default=None) validate_parser.add_argument("--options", help="json options for the monitor", default=None) @@ -187,8 +187,6 @@ def _post(cls, args): if args.restricted_roles: restricted_roles = sorted(set([rr.strip() for rr in args.restricted_roles.split(",") if rr.strip()])) body["restricted_roles"] = restricted_roles - else: - restricted_roles = None if tags: body["tags"] = tags if args.priority: @@ -438,8 +436,6 @@ def _validate(cls, args): options = json.loads(args.options) if args.restricted_roles: to_update["restricted_roles"] = sorted(set([rr.strip() for rr in args.tags.split(",") if rr.strip()])) - else: - restricted_roles = None if args.tags: tags = sorted(set([t.strip() for t in args.tags.split(",") if t.strip()])) else: From b9233ec8c54cbda801f611269210ad9a71f8c76d Mon Sep 17 00:00:00 2001 From: pierreln-dd Date: Wed, 3 Jan 2024 17:27:33 +0100 Subject: [PATCH 04/12] fixing unused validate --- datadog/dogshell/monitor.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/datadog/dogshell/monitor.py b/datadog/dogshell/monitor.py index b27cdab7d..915206b15 100644 --- a/datadog/dogshell/monitor.py +++ b/datadog/dogshell/monitor.py @@ -434,8 +434,6 @@ def _validate(cls, args): options = None if args.options is not None: options = json.loads(args.options) - if args.restricted_roles: - to_update["restricted_roles"] = sorted(set([rr.strip() for rr in args.tags.split(",") if rr.strip()])) if args.tags: tags = sorted(set([t.strip() for t in args.tags.split(",") if t.strip()])) else: From 9952dee5e9ffaee171e4aaeaac6811e23f976d9a Mon Sep 17 00:00:00 2001 From: pierreln-dd Date: Wed, 3 Jan 2024 17:31:55 +0100 Subject: [PATCH 05/12] fixing linting --- datadog/dogshell/monitor.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/datadog/dogshell/monitor.py b/datadog/dogshell/monitor.py index 915206b15..fb32c85c8 100644 --- a/datadog/dogshell/monitor.py +++ b/datadog/dogshell/monitor.py @@ -37,7 +37,8 @@ def setup_parser(cls, subparsers): "--message", help="message to include with notifications" " for this monitor", default=None ) post_parser.add_argument( - "--restricted_roles", help="comma-separated list of unique role identifiers allowed to edit the monitor", default=None + "--restricted_roles", help="comma-separated list of unique role identifiers allowed to edit the monitor", + default=None ) post_parser.add_argument("--tags", help="comma-separated list of tags", default=None) post_parser.add_argument( @@ -78,7 +79,8 @@ def setup_parser(cls, subparsers): ) update_parser.add_argument("--name", help="name of the alert", default=None) update_parser.add_argument( - "--restricted_roles", help="comma-separated list of unique role identifiers allowed to edit the monitor", default=None + "--restricted_roles", help="comma-separated list of unique role identifiers allowed to edit the monitor", + default=None ) update_parser.add_argument("--tags", help="comma-separated list of tags", default=None) update_parser.add_argument( @@ -158,7 +160,8 @@ def setup_parser(cls, subparsers): "--message", help="message to include with notifications" " for this monitor", default=None ) validate_parser.add_argument( - "--restricted_roles", help="comma-separated list of unique role identifiers allowed to edit the monitor", default=None + "--restricted_roles", help="comma-separated list of unique role identifiers allowed to edit the monitor", + default=None ) validate_parser.add_argument("--tags", help="comma-separated list of tags", default=None) validate_parser.add_argument("--options", help="json options for the monitor", default=None) From c5ec8a73156f3bb638b32b16539a5429f711562e Mon Sep 17 00:00:00 2001 From: pierreln-dd Date: Wed, 3 Jan 2024 17:34:28 +0100 Subject: [PATCH 06/12] fixing linting 2 --- datadog/dogshell/monitor.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/datadog/dogshell/monitor.py b/datadog/dogshell/monitor.py index fb32c85c8..696c65d79 100644 --- a/datadog/dogshell/monitor.py +++ b/datadog/dogshell/monitor.py @@ -37,7 +37,7 @@ def setup_parser(cls, subparsers): "--message", help="message to include with notifications" " for this monitor", default=None ) post_parser.add_argument( - "--restricted_roles", help="comma-separated list of unique role identifiers allowed to edit the monitor", + "--restricted_roles", help="comma-separated list of unique role identifiers allowed to edit the monitor", default=None ) post_parser.add_argument("--tags", help="comma-separated list of tags", default=None) @@ -79,7 +79,7 @@ def setup_parser(cls, subparsers): ) update_parser.add_argument("--name", help="name of the alert", default=None) update_parser.add_argument( - "--restricted_roles", help="comma-separated list of unique role identifiers allowed to edit the monitor", + "--restricted_roles", help="comma-separated list of unique role identifiers allowed to edit the monitor", default=None ) update_parser.add_argument("--tags", help="comma-separated list of tags", default=None) @@ -160,7 +160,7 @@ def setup_parser(cls, subparsers): "--message", help="message to include with notifications" " for this monitor", default=None ) validate_parser.add_argument( - "--restricted_roles", help="comma-separated list of unique role identifiers allowed to edit the monitor", + "--restricted_roles", help="comma-separated list of unique role identifiers allowed to edit the monitor", default=None ) validate_parser.add_argument("--tags", help="comma-separated list of tags", default=None) From 34b041c0726efde62601c79531d6a01621434f6a Mon Sep 17 00:00:00 2001 From: pierreln-dd Date: Wed, 10 Jan 2024 14:34:31 +0100 Subject: [PATCH 07/12] adds validate and ensures an empty list for restricted_roles updates the monitor accordingly --- datadog/dogshell/monitor.py | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/datadog/dogshell/monitor.py b/datadog/dogshell/monitor.py index 696c65d79..3126f3396 100644 --- a/datadog/dogshell/monitor.py +++ b/datadog/dogshell/monitor.py @@ -179,6 +179,11 @@ def _post(cls, args): tags = sorted(set([t.strip() for t in args.tags.split(",") if t.strip()])) else: tags = None + + if args.restricted_roles: + restricted_roles = sorted(set([rr.strip() for rr in args.restricted_roles.split(",") if rr.strip()])) + else: + restricted_roles = None body = { "type": args.type, @@ -187,11 +192,10 @@ def _post(cls, args): "message": args.message, "options": options } - if args.restricted_roles: - restricted_roles = sorted(set([rr.strip() for rr in args.restricted_roles.split(",") if rr.strip()])) - body["restricted_roles"] = restricted_roles if tags: body["tags"] = tags + if restricted_roles: + body["restricted_roles"] = restricted_roles if args.priority: body["priority"] = args.priority @@ -264,7 +268,11 @@ def _update(cls, args): if args.query_opt: to_update["query"] = args.query_opt if args.restricted_roles: - to_update["restricted_roles"] = sorted(set([rr.strip() for rr in args.tags.split(",") if rr.strip()])) + if args.restricted_roles == ",": + to_update["restricted_roles"] = None + else: + to_update["restricted_roles"] = sorted( + set([rr.strip() for rr in args.restricted_roles.split(",") if rr.strip()])) if args.tags: to_update["tags"] = sorted(set([t.strip() for t in args.tags.split(",") if t.strip()])) if args.priority: @@ -442,8 +450,14 @@ def _validate(cls, args): else: tags = None + if args.restricted_roles: + restricted_roles = sorted(set([rr.strip() for rr in args.restricted_roles.split(",") if rr.strip()])) + else: + restricted_roles = None + res = api.Monitor.validate( - type=args.type, query=args.query, name=args.name, message=args.message, tags=tags, options=options + type=args.type, query=args.query, name=args.name, message=args.message, tags=tags, + restricted_roles=restricted_roles, options=options ) # report_warnings(res) # report_errors(res) From 017cdb9c64c9e41d35546e481ddf5fce42af503c Mon Sep 17 00:00:00 2001 From: pierreln-dd Date: Wed, 10 Jan 2024 14:40:53 +0100 Subject: [PATCH 08/12] linting review --- datadog/dogshell/monitor.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/datadog/dogshell/monitor.py b/datadog/dogshell/monitor.py index 3126f3396..8aa7a35ac 100644 --- a/datadog/dogshell/monitor.py +++ b/datadog/dogshell/monitor.py @@ -179,7 +179,7 @@ def _post(cls, args): tags = sorted(set([t.strip() for t in args.tags.split(",") if t.strip()])) else: tags = None - + if args.restricted_roles: restricted_roles = sorted(set([rr.strip() for rr in args.restricted_roles.split(",") if rr.strip()])) else: @@ -456,8 +456,13 @@ def _validate(cls, args): restricted_roles = None res = api.Monitor.validate( - type=args.type, query=args.query, name=args.name, message=args.message, tags=tags, - restricted_roles=restricted_roles, options=options + type=args.type, + query=args.query, + name=args.name, + message=args.message, + tags=tags, + restricted_roles=restricted_roles, + options=options ) # report_warnings(res) # report_errors(res) From 9f4634549b3c2d530600299b9e0aa5334cd10884 Mon Sep 17 00:00:00 2001 From: Sherzod Karimov Date: Fri, 19 Jan 2024 13:37:06 -0500 Subject: [PATCH 09/12] Fix explicit None handling --- datadog/dogshell/monitor.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/datadog/dogshell/monitor.py b/datadog/dogshell/monitor.py index 8aa7a35ac..4f66d27c2 100644 --- a/datadog/dogshell/monitor.py +++ b/datadog/dogshell/monitor.py @@ -267,8 +267,8 @@ def _update(cls, args): to_update["type"] = args.type_opt if args.query_opt: to_update["query"] = args.query_opt - if args.restricted_roles: - if args.restricted_roles == ",": + if args.restricted_roles != None: + if args.restricted_roles == "": to_update["restricted_roles"] = None else: to_update["restricted_roles"] = sorted( @@ -302,8 +302,9 @@ def _file_update(cls, args): "message": monitor["message"], "options": monitor["options"] } - restricted_roles = monitor.get("restricted_roles", None) - if restricted_roles: + # Default value is False to defferentiate between explicit None and not set + restricted_roles = monitor.get("restricted_roles", False) + if restricted_roles != False: body["restricted_roles"] = restricted_roles tags = monitor.get("tags", None) if tags: @@ -450,10 +451,11 @@ def _validate(cls, args): else: tags = None - if args.restricted_roles: - restricted_roles = sorted(set([rr.strip() for rr in args.restricted_roles.split(",") if rr.strip()])) - else: - restricted_roles = None + if args.restricted_roles != None: + if args.restricted_roles == "": + restricted_roles = None + else: + restricted_roles = sorted(set([rr.strip() for rr in args.restricted_roles.split(",") if rr.strip()])) res = api.Monitor.validate( type=args.type, From c94ada51a50a985027354a418f0b16c5ee3b7dcc Mon Sep 17 00:00:00 2001 From: Sherzod Karimov Date: Fri, 19 Jan 2024 13:45:24 -0500 Subject: [PATCH 10/12] lint --- datadog/dogshell/monitor.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/datadog/dogshell/monitor.py b/datadog/dogshell/monitor.py index 4f66d27c2..f4571e88a 100644 --- a/datadog/dogshell/monitor.py +++ b/datadog/dogshell/monitor.py @@ -267,7 +267,7 @@ def _update(cls, args): to_update["type"] = args.type_opt if args.query_opt: to_update["query"] = args.query_opt - if args.restricted_roles != None: + if args.restricted_roles is not None: if args.restricted_roles == "": to_update["restricted_roles"] = None else: @@ -304,7 +304,7 @@ def _file_update(cls, args): } # Default value is False to defferentiate between explicit None and not set restricted_roles = monitor.get("restricted_roles", False) - if restricted_roles != False: + if restricted_roles is not False: body["restricted_roles"] = restricted_roles tags = monitor.get("tags", None) if tags: @@ -451,7 +451,7 @@ def _validate(cls, args): else: tags = None - if args.restricted_roles != None: + if args.restricted_roles is not None: if args.restricted_roles == "": restricted_roles = None else: From cd70e57658e18191ae01da1c430e33c2015a8a8b Mon Sep 17 00:00:00 2001 From: Sherzod Karimov Date: Fri, 19 Jan 2024 14:13:42 -0500 Subject: [PATCH 11/12] fix validate --- datadog/dogshell/monitor.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/datadog/dogshell/monitor.py b/datadog/dogshell/monitor.py index f4571e88a..01f248573 100644 --- a/datadog/dogshell/monitor.py +++ b/datadog/dogshell/monitor.py @@ -451,11 +451,10 @@ def _validate(cls, args): else: tags = None - if args.restricted_roles is not None: - if args.restricted_roles == "": - restricted_roles = None - else: - restricted_roles = sorted(set([rr.strip() for rr in args.restricted_roles.split(",") if rr.strip()])) + if args.restricted_roles: + restricted_roles = sorted(set([rr.strip() for rr in args.restricted_roles.split(",") if rr.strip()])) + else: + restricted_roles = None res = api.Monitor.validate( type=args.type, From dfbfe644dd7e74eb17a939a5e9bb7fdd81acf683 Mon Sep 17 00:00:00 2001 From: skarimo <40482491+skarimo@users.noreply.github.com> Date: Thu, 25 Jan 2024 15:39:31 -0500 Subject: [PATCH 12/12] Update datadog/dogshell/monitor.py --- datadog/dogshell/monitor.py | 1 + 1 file changed, 1 insertion(+) diff --git a/datadog/dogshell/monitor.py b/datadog/dogshell/monitor.py index 01f248573..877e6cd06 100644 --- a/datadog/dogshell/monitor.py +++ b/datadog/dogshell/monitor.py @@ -446,6 +446,7 @@ def _validate(cls, args): options = None if args.options is not None: options = json.loads(args.options) + if args.tags: tags = sorted(set([t.strip() for t in args.tags.split(",") if t.strip()])) else: