Skip to content

Commit

Permalink
Add tags and priority to the monitor post/update commands (#739)
Browse files Browse the repository at this point in the history
* add tags and priority to the fpost monitor request

* add default lookup

* update monitor resource with tags and priority cli commands

* remove trailing space

* rerecord cassette

* rerecord with compressed response

* build dict before api call to remove nulls

* fix wrong args.priority expr and store tags and priority in var instead of double get

---------

Co-authored-by: Kevin Zou <kevin.zou@datadoghq.com>
Co-authored-by: Kevin Zou <17015060+nkzou@users.noreply.github.com>
  • Loading branch information
3 people authored Jul 10, 2023
1 parent 099a094 commit 42d7678
Show file tree
Hide file tree
Showing 3 changed files with 567 additions and 733 deletions.
77 changes: 59 additions & 18 deletions datadog/dogshell/monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ def setup_parser(cls, subparsers):
"--message", help="message to include with notifications" " for this monitor", default=None
)
post_parser.add_argument("--tags", help="comma-separated list of tags", default=None)
post_parser.add_argument(
"--priority",
help="Integer from 1 (high) to 5 (low) indicating alert severity.",
default=None
)
post_parser.add_argument("--options", help="json options for the monitor", default=None)
post_parser.set_defaults(func=cls._post)

Expand Down Expand Up @@ -69,9 +74,15 @@ def setup_parser(cls, subparsers):
dest="query_opt",
)
update_parser.add_argument("--name", help="name of the alert", 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
)
update_parser.add_argument(
"--priority",
help="Integer from 1 (high) to 5 (low) indicating alert severity.",
default=None
)
update_parser.add_argument("--options", help="json options for the monitor", default=None)
update_parser.set_defaults(func=cls._update)

Expand Down Expand Up @@ -157,9 +168,19 @@ def _post(cls, args):
else:
tags = None

res = api.Monitor.create(
type=args.type, query=args.query, name=args.name, message=args.message, tags=tags, options=options
)
body = {
"type": args.type,
"query": args.query,
"name": args.name,
"message": args.message,
"options": options
}
if tags:
body["tags"] = tags
if args.priority:
body["priority"] = args.priority

res = api.Monitor.create(**body)
report_warnings(res)
report_errors(res)
if format == "pretty":
Expand All @@ -172,13 +193,21 @@ def _file_post(cls, args):
api._timeout = args.timeout
format = args.format
monitor = json.load(args.file)
res = api.Monitor.create(
type=monitor["type"],
query=monitor["query"],
name=monitor["name"],
message=monitor["message"],
options=monitor["options"],
)
body = {
"type": monitor["type"],
"query": monitor["query"],
"name": monitor["name"],
"message": monitor["message"],
"options": monitor["options"]
}
tags = monitor.get("tags", None)
if tags:
body["tags"] = tags
priority = monitor.get("priority", None)
if priority:
body["priority"] = priority

res = api.Monitor.create(**body)
report_warnings(res)
report_errors(res)
if format == "pretty":
Expand Down Expand Up @@ -216,6 +245,10 @@ def _update(cls, args):
to_update["type"] = args.type_opt
if args.query_opt:
to_update["query"] = args.query_opt
if args.tags:
to_update["tags"] = sorted(set([t.strip() for t in args.tags.split(",") if t.strip()]))
if args.priority:
to_update["priority"] = args.priority

if args.options is not None:
to_update["options"] = json.loads(args.options)
Expand All @@ -234,14 +267,22 @@ def _file_update(cls, args):
api._timeout = args.timeout
format = args.format
monitor = json.load(args.file)
res = api.Monitor.update(
monitor["id"],
type=monitor["type"],
query=monitor["query"],
name=monitor["name"],
message=monitor["message"],
options=monitor["options"],
)
body = {
"type": monitor["type"],
"query": monitor["query"],
"name": monitor["name"],
"message": monitor["message"],
"options": monitor["options"]
}
tags = monitor.get("tags", None)
if tags:
body["tags"] = tags
priority = monitor.get("priority", None)
if priority:
body["priority"] = priority

res = api.Monitor.update(monitor["id"], **body)

report_warnings(res)
report_errors(res)
if format == "pretty":
Expand Down
Loading

0 comments on commit 42d7678

Please sign in to comment.