forked from DataDog/datadogpy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdashboard.py
More file actions
174 lines (154 loc) · 6.47 KB
/
dashboard.py
File metadata and controls
174 lines (154 loc) · 6.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# Unless explicitly stated otherwise all files in this repository are licensed under the BSD-3-Clause License.
# This product includes software developed at Datadog (https://www.datadoghq.com/).
# Copyright 2015-Present Datadog, Inc
# stdlib
import json
import sys
# 3p
import argparse
# datadog
from datadog import api
from datadog.dogshell.common import report_errors, report_warnings
from datadog.util.format import pretty_json
class DashboardClient(object):
@classmethod
def setup_parser(cls, subparsers):
parser = subparsers.add_parser("dashboard", help="Create, edit, and delete dashboards")
verb_parsers = parser.add_subparsers(title="Verbs", dest="verb")
verb_parsers.required = True
post_parser = verb_parsers.add_parser("post", help="Create dashboards")
# Required arguments:
post_parser.add_argument("title", help="title for the new dashboard")
post_parser.add_argument(
"widgets", help="widget definitions as a JSON string. If unset," " reads from stdin.", nargs="?"
)
post_parser.add_argument("layout_type", choices=["ordered", "free"], help="Layout type of the dashboard.")
# Optional arguments:
post_parser.add_argument("--description", help="Short description of the dashboard")
post_parser.add_argument(
"--read_only",
help="Whether this dashboard is read-only. " "If True, only the author and admins can make changes to it.",
action="store_true",
)
post_parser.add_argument(
"--notify_list",
type=_json_string,
help="A json list of user handles, e.g. " '\'["user1@domain.com", "user2@domain.com"]\'',
)
post_parser.add_argument(
"--template_variables",
type=_json_string,
help="A json list of template variable dicts, e.g. "
'\'[{"name": "host", "prefix": "host", '
'"default": "my-host"}]\'',
)
post_parser.set_defaults(func=cls._post)
update_parser = verb_parsers.add_parser("update", help="Update existing dashboards")
# Required arguments:
update_parser.add_argument("dashboard_id", help="Dashboard to replace" " with the new definition")
update_parser.add_argument("title", help="New title for the dashboard")
update_parser.add_argument(
"widgets", help="Widget definitions as a JSON string." " If unset, reads from stdin", nargs="?"
)
update_parser.add_argument("layout_type", choices=["ordered", "free"], help="Layout type of the dashboard.")
# Optional arguments:
update_parser.add_argument("--description", help="Short description of the dashboard")
update_parser.add_argument(
"--read_only",
help="Whether this dashboard is read-only. " "If True, only the author and admins can make changes to it.",
action="store_true",
)
update_parser.add_argument(
"--notify_list",
type=_json_string,
help="A json list of user handles, e.g. " '\'["user1@domain.com", "user2@domain.com"]\'',
)
update_parser.add_argument(
"--template_variables",
type=_json_string,
help="A json list of template variable dicts, e.g. "
'\'[{"name": "host", "prefix": "host", '
'"default": "my-host"}]\'',
)
update_parser.set_defaults(func=cls._update)
show_parser = verb_parsers.add_parser("show", help="Show a dashboard definition")
show_parser.add_argument("dashboard_id", help="Dashboard to show")
show_parser.set_defaults(func=cls._show)
delete_parser = verb_parsers.add_parser("delete", help="Delete dashboards")
delete_parser.add_argument("dashboard_id", help="Dashboard to delete")
delete_parser.set_defaults(func=cls._delete)
@classmethod
def _post(cls, args):
api._timeout = args.timeout
format = args.format
widgets = args.widgets
if args.widgets is None:
widgets = sys.stdin.read()
widgets = json.loads(widgets)
# Required arguments
payload = {"title": args.title, "widgets": widgets, "layout_type": args.layout_type}
# Optional arguments
if args.description:
payload["description"] = args.description
if args.read_only:
payload["is_read_only"] = args.read_only
if args.notify_list:
payload["notify_list"] = args.notify_list
if args.template_variables:
payload["template_variables"] = args.template_variables
res = api.Dashboard.create(**payload)
report_warnings(res)
report_errors(res)
if format == "pretty":
print(pretty_json(res))
else:
print(json.dumps(res))
@classmethod
def _update(cls, args):
api._timeout = args.timeout
format = args.format
widgets = args.widgets
if args.widgets is None:
widgets = sys.stdin.read()
widgets = json.loads(widgets)
# Required arguments
payload = {"title": args.title, "widgets": widgets, "layout_type": args.layout_type}
# Optional arguments
if args.description:
payload["description"] = args.description
if args.read_only:
payload["is_read_only"] = args.read_only
if args.notify_list:
payload["notify_list"] = args.notify_list
if args.template_variables:
payload["template_variables"] = args.template_variables
res = api.Dashboard.update(args.dashboard_id, **payload)
report_warnings(res)
report_errors(res)
if format == "pretty":
print(pretty_json(res))
else:
print(json.dumps(res))
@classmethod
def _show(cls, args):
api._timeout = args.timeout
format = args.format
res = api.Dashboard.get(args.dashboard_id)
report_warnings(res)
report_errors(res)
if format == "pretty":
print(pretty_json(res))
else:
print(json.dumps(res))
@classmethod
def _delete(cls, args):
api._timeout = args.timeout
res = api.Dashboard.delete(args.dashboard_id)
if res is not None:
report_warnings(res)
report_errors(res)
def _json_string(str):
try:
return json.loads(str)
except Exception:
raise argparse.ArgumentTypeError("bad json parameter")