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

Add --restricted_roles option to Monitors API #809

Merged
merged 13 commits into from
Jan 25, 2024
46 changes: 44 additions & 2 deletions 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 @@ -414,14 +446,24 @@ def _validate(cls, args):
options = None
if args.options is not None:
options = json.loads(args.options)

if args.tags:
skarimo marked this conversation as resolved.
Show resolved Hide resolved
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

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
Loading