Skip to content

Commit

Permalink
Add --restricted_roles option to Monitors API (#809)
Browse files Browse the repository at this point in the history
* MNTS-90348 Adds restricted_roles to monitors API

* add restricted_roles to update_parser

* Specify comma-separated for restricted_roles

* fixing unused validate

* fixing linting

* fixing linting 2

* adds validate and ensures an empty list for restricted_roles updates the monitor accordingly

* linting review

* Fix explicit None handling

* lint

* fix validate

* Update datadog/dogshell/monitor.py

---------

Co-authored-by: Sherzod Karimov <sherzod.karimov@datadoghq.com>
Co-authored-by: skarimo <40482491+skarimo@users.noreply.github.com>
  • Loading branch information
3 people authored Jan 25, 2024
1 parent 17a5f1c commit 4dcf1ed
Showing 1 changed file with 44 additions and 1 deletion.
45 changes: 44 additions & 1 deletion datadog/dogshell/monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ 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="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(
"--priority",
Expand Down Expand Up @@ -74,6 +78,10 @@ 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="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(
"--message", help="message to include with " "notifications for this monitor", default=None
Expand Down Expand Up @@ -151,6 +159,10 @@ 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="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)
validate_parser.set_defaults(func=cls._validate)
Expand All @@ -168,6 +180,11 @@ def _post(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

body = {
"type": args.type,
"query": args.query,
Expand All @@ -177,6 +194,8 @@ def _post(cls, args):
}
if tags:
body["tags"] = tags
if restricted_roles:
body["restricted_roles"] = restricted_roles
if args.priority:
body["priority"] = args.priority

Expand All @@ -200,6 +219,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
Expand Down Expand Up @@ -245,6 +267,12 @@ def _update(cls, args):
to_update["type"] = args.type_opt
if args.query_opt:
to_update["query"] = args.query_opt
if args.restricted_roles is not None:
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:
Expand Down Expand Up @@ -274,6 +302,10 @@ def _file_update(cls, args):
"message": monitor["message"],
"options": monitor["options"]
}
# Default value is False to defferentiate between explicit None and not set
restricted_roles = monitor.get("restricted_roles", False)
if restricted_roles is not False:
body["restricted_roles"] = restricted_roles
tags = monitor.get("tags", None)
if tags:
body["tags"] = tags
Expand Down Expand Up @@ -420,8 +452,19 @@ 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)
Expand Down

0 comments on commit 4dcf1ed

Please sign in to comment.