forked from DataDog/datadogpy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscreenboard.py
More file actions
308 lines (264 loc) · 11.3 KB
/
screenboard.py
File metadata and controls
308 lines (264 loc) · 11.3 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# 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 argparse
import json
import platform
import sys
import webbrowser
# 3p
from datadog.util.format import pretty_json
# datadog
from datadog import api
from datadog.dogshell.common import report_errors, report_warnings, print_err
from datetime import datetime
class ScreenboardClient(object):
@classmethod
def setup_parser(cls, subparsers):
parser = subparsers.add_parser("screenboard", help="Create, edit, and delete screenboards.")
parser.add_argument(
"--string_ids",
action="store_true",
dest="string_ids",
help="Represent screenboard IDs as strings instead of ints in JSON",
)
verb_parsers = parser.add_subparsers(title="Verbs", dest="verb")
verb_parsers.required = True
post_parser = verb_parsers.add_parser("post", help="Create screenboards.")
post_parser.add_argument("title", help="title for the new screenboard")
post_parser.add_argument("description", help="short description of the screenboard")
post_parser.add_argument(
"graphs", help="graph definitions as a JSON string." " if unset, reads from stdin.", nargs="?"
)
post_parser.add_argument(
"--template_variables",
type=_template_variables,
default=[],
help="a json list of template variable dicts, e.g. "
"[{'name': 'host', 'prefix': 'host', 'default': 'host:my-host'}]",
)
post_parser.add_argument("--width", type=int, default=None, help="screenboard width in pixels")
post_parser.add_argument("--height", type=int, default=None, help="screenboard height in pixels")
post_parser.set_defaults(func=cls._post)
update_parser = verb_parsers.add_parser("update", help="Update existing screenboards.")
update_parser.add_argument("screenboard_id", help="screenboard to replace " " with the new definition")
update_parser.add_argument("title", help="title for the new screenboard")
update_parser.add_argument("description", help="short description of the screenboard")
update_parser.add_argument(
"graphs", help="graph definitions as a JSON string." " if unset, reads from stdin.", nargs="?"
)
update_parser.add_argument(
"--template_variables",
type=_template_variables,
default=[],
help="a json list of template variable dicts, e.g. "
"[{'name': 'host', 'prefix': 'host', 'default': "
"'host:my-host'}]",
)
update_parser.add_argument("--width", type=int, default=None, help="screenboard width in pixels")
update_parser.add_argument("--height", type=int, default=None, help="screenboard height in pixels")
update_parser.set_defaults(func=cls._update)
show_parser = verb_parsers.add_parser("show", help="Show a screenboard definition.")
show_parser.add_argument("screenboard_id", help="screenboard to show")
show_parser.set_defaults(func=cls._show)
delete_parser = verb_parsers.add_parser("delete", help="Delete a screenboard.")
delete_parser.add_argument("screenboard_id", help="screenboard to delete")
delete_parser.set_defaults(func=cls._delete)
share_parser = verb_parsers.add_parser("share", help="Share an existing screenboard's" " with a public URL.")
share_parser.add_argument("screenboard_id", help="screenboard to share")
share_parser.set_defaults(func=cls._share)
revoke_parser = verb_parsers.add_parser("revoke", help="Revoke an existing screenboard's" " with a public URL.")
revoke_parser.add_argument("screenboard_id", help="screenboard to revoke")
revoke_parser.set_defaults(func=cls._revoke)
pull_parser = verb_parsers.add_parser("pull", help="Pull a screenboard on the server" " into a local file")
pull_parser.add_argument("screenboard_id", help="ID of screenboard to pull")
pull_parser.add_argument("filename", help="file to pull screenboard into")
pull_parser.set_defaults(func=cls._pull)
push_parser = verb_parsers.add_parser(
"push", help="Push updates to screenboards" " from local files to the server"
)
push_parser.add_argument(
"--append_auto_text",
action="store_true",
dest="append_auto_text",
help="When pushing to the server, appends filename and"
" timestamp to the end of the screenboard description",
)
push_parser.add_argument(
"file", help="screenboard files to push to the server", nargs="+", type=argparse.FileType("r")
)
push_parser.set_defaults(func=cls._push)
new_file_parser = verb_parsers.add_parser(
"new_file", help="Create a new screenboard" " and put its contents in a file"
)
new_file_parser.add_argument("filename", help="name of file to create with" " empty screenboard")
new_file_parser.add_argument(
"graphs", help="graph definitions as a JSON string." " if unset, reads from stdin.", nargs="?"
)
new_file_parser.set_defaults(func=cls._new_file)
@classmethod
def _pull(cls, args):
cls._write_screen_to_file(args.screenboard_id, args.filename, args.timeout, args.format, args.string_ids)
# TODO Is there a test for this one ?
@classmethod
def _push(cls, args):
api._timeout = args.timeout
for f in args.file:
screen_obj = json.load(f)
if args.append_auto_text:
datetime_str = datetime.now().strftime("%x %X")
auto_text = "<br/>\nUpdated at {0} from {1} ({2}) on {3}".format(
datetime_str, f.name, screen_obj["id"], platform.node()
)
screen_obj["description"] += auto_text
if "id" in screen_obj:
# Always convert to int, in case it was originally a string.
screen_obj["id"] = int(screen_obj["id"])
res = api.Screenboard.update(**screen_obj)
else:
res = api.Screenboard.create(**screen_obj)
if "errors" in res:
print_err("Upload of screenboard {0} from file {1} failed.".format(screen_obj["id"], f.name))
report_warnings(res)
report_errors(res)
if format == "pretty":
print(pretty_json(res))
else:
print(json.dumps(res))
if args.format == "pretty":
print("Uploaded file {0} (screenboard {1})".format(f.name, screen_obj["id"]))
@classmethod
def _write_screen_to_file(cls, screenboard_id, filename, timeout, format="raw", string_ids=False):
with open(filename, "w") as f:
res = api.Screenboard.get(screenboard_id)
report_warnings(res)
report_errors(res)
screen_obj = res
if "resource" in screen_obj:
del screen_obj["resource"]
if "url" in screen_obj:
del screen_obj["url"]
if string_ids:
screen_obj["id"] = str(screen_obj["id"])
json.dump(screen_obj, f, indent=2)
if format == "pretty":
print("Downloaded screenboard {0} to file {1}".format(screenboard_id, filename))
else:
print("{0} {1}".format(screenboard_id, filename))
@classmethod
def _post(cls, args):
graphs = sys.stdin.read()
api._timeout = args.timeout
format = args.format
graphs = args.graphs
if args.graphs is None:
graphs = sys.stdin.read()
graphs = json.loads(graphs)
res = api.Screenboard.create(
title=args.title,
description=args.description,
graphs=[graphs],
template_variables=args.template_variables,
width=args.width,
height=args.height,
)
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
graphs = args.graphs
if args.graphs is None:
graphs = sys.stdin.read()
graphs = json.loads(graphs)
res = api.Screenboard.update(
args.screenboard_id,
board_title=args.title,
description=args.description,
widgets=graphs,
template_variables=args.template_variables,
width=args.width,
height=args.height,
)
report_warnings(res)
report_errors(res)
if format == "pretty":
print(pretty_json(res))
else:
print(json.dumps(res))
@classmethod
def _web_view(cls, args):
dash_id = json.load(args.file)["id"]
url = api._api_host + "/dash/dash/{0}".format(dash_id)
webbrowser.open(url)
@classmethod
def _show(cls, args):
api._timeout = args.timeout
format = args.format
res = api.Screenboard.get(args.screenboard_id)
report_warnings(res)
report_errors(res)
if args.string_ids:
res["id"] = str(res["id"])
if format == "pretty":
print(pretty_json(res))
else:
print(json.dumps(res))
@classmethod
def _delete(cls, args):
api._timeout = args.timeout
# TODO CHECK
res = api.Screenboard.delete(args.screenboard_id)
if res is not None:
report_warnings(res)
report_errors(res)
@classmethod
def _share(cls, args):
api._timeout = args.timeout
format = args.format
res = api.Screenboard.share(args.screenboard_id)
if format == "pretty":
print(pretty_json(res))
else:
print(json.dumps(res))
@classmethod
def _revoke(cls, args):
api._timeout = args.timeout
format = args.format
res = api.Screenboard.revoke(args.screenboard_id)
if format == "pretty":
print(pretty_json(res))
else:
print(json.dumps(res))
@classmethod
def _new_file(cls, args):
api._timeout = args.timeout
format = args.format
graphs = args.graphs
if args.graphs is None:
graphs = sys.stdin.read()
graphs = json.loads(graphs)
res = api.Screenboard.create(
board_title=args.filename, description="Description for {0}".format(args.filename), widgets=[graphs]
)
report_warnings(res)
report_errors(res)
cls._write_screen_to_file(res["id"], args.filename, args.timeout, format, args.string_ids)
if format == "pretty":
print(pretty_json(res))
else:
print(json.dumps(res))
def _template_variables(tpl_var_input):
if "[" not in tpl_var_input:
return [v.strip() for v in tpl_var_input.split(",")]
else:
try:
return json.loads(tpl_var_input)
except Exception:
raise argparse.ArgumentTypeError("bad template_variable json parameter")