-
Notifications
You must be signed in to change notification settings - Fork 130
/
Copy pathdogmover.py
executable file
·520 lines (450 loc) · 21.5 KB
/
dogmover.py
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
#!/usr/bin/env python3
"""Usage:
dogmover.py pull (<type>) [--tag tag]... [--dry-run] [-h]
dogmover.py push (<type>) [--dry-run] [-h]
Examples:
Dashboards:
dogmover.py pull dashboards
dogmover.py push dashboards
Synthetic api tests using --tag that only pulls tests if all the defined tags exist on them:
dogmover.py pull synthetic_api_tests p --tag env:production --tag application:abc
dogmover.py push synthetic_api_tests
Run with --dry-run without making any changes to your Datadog account:
dogmover.py pull dashboards --dry-run
dogmover.py push dashboards --dry-run
Supported arguments:
dogmover.py pull|push dashboards|monitors|users|synthetics_api_tests|synthetics_browser_tests|awsaccounts|logpipelines|notebooks (--tag tag) (--dry-run|-h)
Note. --tag is currently only supported for synthetics_api_tests, synthetics_browser_tests and monitors.
Options:
-h, --help
-d, --dry-run
--tag=<k:v> Specify a tag in the format key:value
"""
__author__ = "Misiu Pajor <[email protected]>"
__version__ = "2.1.0"
from docopt import docopt
import json
import os
import glob
import requests
from datadog import initialize, api
def _init_options(action):
config_file = "config.json"
try:
with open(config_file) as f:
config = json.load(f)
except IOError:
exit("No configuration file named: {} could be found.".format(config_file))
options = {}
if action == "pull":
options = {
'api_key': config["source_api_key"],
'app_key': config["source_app_key"],
'api_host': config["source_api_host"]
}
elif action == "push":
options = {
'api_key': config["dest_api_key"],
'app_key': config["dest_app_key"],
'api_host': config["dest_api_host"]
}
initialize(**options)
return options
def _ensure_directory(directory):
if not os.path.exists(directory):
os.makedirs(directory)
return directory
def _json_to_file(path, fileName, data):
filePathNameWExt = './' + path + '/' + fileName + '.json'
_ensure_directory(path)
with open(filePathNameWExt, 'w') as fp:
json.dump(data, fp, sort_keys = True, indent = 4)
return filePathNameWExt
def _files_to_json(type):
files = glob.glob('{}/*.json'.format(type))
return files
def pull_dashboards():
path = False
count = 0
dashboards = api.Dashboard.get_all()
for dashboard in dashboards["dashboards"]:
count = count + 1
json_data = api.Dashboard.get(dashboard["id"])
if not arguments["--dry-run"]:
path = _json_to_file('dashboards', dashboard["id"], json_data)
print("Pulling dashboard: {} with id: {}, writing to file: {}".format(dashboard["title"].encode('utf8'), dashboard["id"], path))
print("Retrieved '{}' dashboards.".format(count))
def pull_monitors(tag):
path = False
count = 0
good_keys = ['tags', 'deleted', 'query', 'message', 'matching_downtimes', 'multi', 'name', 'type', 'options', 'id']
tags = [] if not tag else tag
monitors = api.Monitor.get_all()
for monitor in monitors:
if monitor["type"] == "synthetics alert":
print("Skipping \"{}\" as this is a monitor belonging to a synthetic test. Synthetic monitors will be automatically re-created when you push synthetic tests.".format(monitor["name"].encode('utf8')))
continue
all_tags_found = True
for tag in tags:
if not tag in monitor["tags"]:
all_tags_found = False
print("Tag: {} not found in monitor: \"{}\" with tags {}".format(tag, monitor["name"].encode('utf8'), monitor["tags"]))
break
if all_tags_found == False:
print("Skipping \"{}\" because its tags do not match the filter.".format(monitor["name"].encode('utf8')))
if all_tags_found == True:
count = count + 1
new_monitor = {}
for k, v in monitor.items():
if k in good_keys:
new_monitor[k] = v
if not arguments["--dry-run"]:
path = _json_to_file('monitors', str(new_monitor["id"]), new_monitor)
print("Pulling monitor: \"{}\" with id: {}, writing to file: {}".format(new_monitor["name"].encode('utf8'), new_monitor["id"], path))
print("Retrieved '{}' monitors.".format(count))
def pull_users():
path = False
count = 0
users = api.User.get_all()
for user in users["users"]:
if not user["disabled"]: # don't pull disabled users
count = count + 1
json_data = api.User.get(user["handle"])
if not arguments["--dry-run"]:
path = _json_to_file('users', user["handle"], json_data["user"])
print("Pulling user: {} with role: {}, writing to file: {}".format(user["handle"].encode('utf8'), user["access_role"], path))
print("Retrieved '{}' users.".format(count))
def pull_synthetics_api_tests(options, tag):
path = False
count = 0
tags = [] if not tag else tag
r = requests.get('{}api/v1/synthetics/tests?api_key={}&application_key={}'.format(options["api_host"], options["api_key"], options["app_key"]))
synthetics = r.json()
for synthetic in synthetics["tests"]:
if synthetic["type"] == "api":
all_tags_found="true"
for tag in tags:
if not tag in synthetic["tags"]:
all_tags_found="false"
print("Tag: {} not found in synthetic: \"{}\" with tags {}".format(tag, synthetic["name"].encode('utf8'), synthetic["tags"]))
break
if all_tags_found == "false":
print("Skipping \"{}\" because its tags do not match the filter.".format(synthetic["name"].encode('utf8')))
if all_tags_found == "true":
count = count + 1
json_data = requests.get('{}api/v1/synthetics/tests/{}?api_key={}&application_key={}'.format(
options["api_host"],
synthetic["public_id"],
options["api_key"],
options["app_key"]
)).json()
path = _json_to_file('synthetics_api_tests', synthetic["public_id"], json_data)
print("Pulling: {} and writing to file: {}".format(synthetic["name"].encode('utf8'), path))
print("Retrieved '{}' synthetic tests.".format(count))
def pull_synthetics_browser_tests(options, tag):
path = False
count = 0
tags = [] if not tag else tag
r = requests.get('{}api/v1/synthetics/tests?api_key={}&application_key={}'.format(options["api_host"], options["api_key"], options["app_key"]))
synthetics = r.json()
for synthetic in synthetics["tests"]:
if synthetic["type"] == "browser":
all_tags_found="true"
for tag in tags:
if not tag in synthetic["tags"]:
all_tags_found="false"
print("Tag: {} not found in synthetic: \"{}\" with tags {}".format(tag, synthetic["name"].encode('utf8'), synthetic["tags"]))
break
if all_tags_found == "false":
print("Skipping \"{}\" because its tags do not match the filter.".format(synthetic["name"].encode('utf8')))
if all_tags_found == "true":
count = count + 1
json_data = requests.get('{}api/v1/synthetics/tests/browser/{}?api_key={}&application_key={}'.format(
options["api_host"],
synthetic["public_id"],
options["api_key"],
options["app_key"]
)).json()
path = _json_to_file('synthetics_browser_tests', synthetic["public_id"], json_data)
print("Pulling: {} and writing to file: {}".format(synthetic["name"].encode('utf8'), path))
print("Retrieved '{}' synthetic tests.".format(count))
def pull_awsaccounts(options):
path = False
count = 0
r = requests.get('{}api/v1/integration/aws?api_key={}&application_key={}'.format(options["api_host"], options["api_key"], options["app_key"]))
awsaccounts = r.json()
for awsaccount in awsaccounts["accounts"]:
count = count + 1
if not arguments["--dry-run"]:
path = _json_to_file('awsaccounts', awsaccount["account_id"], awsaccount)
print("Retrieved '{}' AWS accounts.".format(count))
def pull_logpipelines(options):
path = False
count = 0
r = requests.get('{}api/v1/logs/config/pipelines?api_key={}&application_key={}'.format(options["api_host"], options["api_key"], options["app_key"]))
rJSON = r.json()
for item in rJSON:
count = count + 1
path = _json_to_file('logpipelines', item["id"], item)
print("Retrieved '{}' log pipelines.".format(count))
def pull_notebooks(options):
path = False
count = 0
headers = {'content-type': 'application/json', 'DD-API-KEY': options["api_key"], 'DD-APPLICATION-KEY': options["app_key"]}
r = requests.get('{}/api/v1/notebooks'.format(options["api_host"]), headers=headers)
notebooks = r.json()
if notebooks["meta"]["page"]["total_filtered_count"] == 0:
exit("No notebook could be found for your organization.")
for notebook in notebooks["data"]:
count = count + 1
path = _json_to_file('notebooks', str(notebook["id"]), {"data": notebook})
print("Pulling notebook: {} with id: {}, writing to file: {}".format(notebook["attributes"]["name"].encode('utf8'), notebook["id"], path))
print("Retrieved '{}' notebooks.".format(count))
def pull_slos(options):
path = False
count = 0
r = requests.get('{}api/v1/slo?api_key={}&application_key={}'.format(options["api_host"], options["api_key"], options["app_key"]))
slos = r.json()
for slo in slos["data"]:
count = count + 1
path = _json_to_file('slos', str(slo["id"]), slo)
print("Retrieved '{}' SLOs.".format(count))
def push_dashboards():
count = 0
dashboards = _files_to_json("dashboards")
if not dashboards:
exit("No dashboards are locally available. Consider pulling dashboards first.")
for dashboard in dashboards:
with open(dashboard) as f:
data = json.load(f)
count = count + 1
print("Pushing {}".format(data["title"].encode('utf8')))
if not arguments["--dry-run"]:
api.Dashboard.create(
title=data["title"],
description=data["description"],
widgets=data["widgets"],
template_variables=data["template_variables"],
layout_type=data["layout_type"],
notify_list=data["notify_list"],
is_read_only=data["is_read_only"]
)
print("Pushed '{}' dashboards".format(count))
def push_monitors():
count = 0
err_count = 0
ids = {}
monitors = _files_to_json("monitors")
if not monitors:
exit("No monitors are locally available. Consider pulling monitors first.")
# first loop to import non composite monitors
for monitor in monitors:
with open(monitor) as f:
data = json.load(f)
if not data["type"] == "composite":
old_id = str(data["id"])
print("Pushing monitors:", data["id"], data["name"].encode('utf8'))
if not arguments["--dry-run"]:
result = api.Monitor.create(type=data['type'],
query=data['query'],
name=data['name'],
message=data['message'],
tags=data['tags'],
options=data['options'])
if 'errors' in result:
print('Error pushing monitor:',data["id"],json.dumps(result, indent=4, sort_keys=True))
err_count=err_count+1
else:
count = count + 1
new_id= result['id']
api.Monitor.mute(new_id)
print("New monitor ", str(new_id)," has been muted.")
ids[old_id] = str(new_id)
else:
# Fake new id for dry-run purpose
ids[old_id] = old_id[:3] + "xxx"
# Second loop to import composite monitors
for monitor in monitors:
with open(monitor) as f:
data = json.load(f)
if data["type"] == "composite":
new_query = data["query"]
for old_id, new_id in ids.items():
new_query=new_query.replace(old_id, new_id)
print("Pushing composite monitors:", data["id"], data["name"].encode('utf8')," with query ", new_query.encode('utf8'))
if not arguments["--dry-run"]:
result = api.Monitor.create(type=data['type'],
query=new_query,
name=data['name'],
message=data['message'],
tags=data['tags'],
options=data['options'])
if 'errors' in result:
print('Error pushing monitor:',data["id"],json.dumps(result, indent=4, sort_keys=True))
err_count=err_count+1
else:
count = count + 1
new_id= result['id']
api.Monitor.mute(new_id)
print("New monitor ", str(new_id)," has been muted.")
if count > 0:
print("Pushed '{}' monitors in muted status, navigate to Monitors -> Manage downtime to unmute.".format(count))
if err_count > 0:
print("Error pushing '{}' monitors, please check !".format(err_count))
def push_users():
count = 0
users = _files_to_json("users")
if not users:
exit("No users are locally available. Consider pulling users first.")
for user in users:
with open(user) as f:
data = json.load(f)
count = count + 1
print("Pushing: {}".format(data["handle"].encode('utf8')))
if not arguments["--dry-run"]:
api.User.create(
handle=data["handle"],
name=data["name"],
access_role=data["access_role"]
)
print("Pushed '{}' users".format(count))
def push_synthetics_api_tests(options):
count = 0
synthetics = _files_to_json("synthetics_api_tests")
if not synthetics:
exit("No synthetic tests are locally available. Consider synthetics first.")
for synthetic in synthetics:
with open(synthetic) as f:
data = json.load(f)
count = count + 1
invalid_keys = ["public_id", "monitor_id"]
list(map(data.pop, invalid_keys))
print("Pushing {}".format(data["name"].encode('utf8')))
if not arguments["--dry-run"]:
r = requests.post('{}api/v1/synthetics/tests?api_key={}&application_key={}'.format(options["api_host"], options["api_key"], options["app_key"]), json=data)
print("Pushed '{}' synthetic tests.".format(count))
def push_synthetics_browser_tests(options):
count = 0
synthetics = _files_to_json("synthetics_browser_tests")
if not synthetics:
exit("No synthetic tests are locally available. Consider synthetics first.")
for synthetic in synthetics:
with open(synthetic) as f:
data = json.load(f)
count = count + 1
invalid_keys = ["public_id", "monitor_id"]
list(map(data.pop, invalid_keys))
print("Pushing {}".format(data["name"].encode('utf8')))
if not arguments["--dry-run"]:
r = requests.post('{}api/v1/synthetics/tests?api_key={}&application_key={}'.format(options["api_host"], options["api_key"], options["app_key"]), json=data)
print("Pushed '{}' synthetic tests.".format(count))
def push_awsaccounts(options):
count = 0
awsaccounts = _files_to_json("awsaccounts")
if not awsaccounts:
exit("No awsaccounts are locally available. Consider pulling awsaccounts first.")
for awsaccount in awsaccounts:
with open(awsaccount) as f:
data = json.load(f)
count = count + 1
print("Pushing {}".format(data["account_id"].encode('utf8')))
if not arguments["--dry-run"]:
r = requests.post('{}api/v1/integration/aws?api_key={}&application_key={}'.format(options["api_host"], options["api_key"], options["app_key"]), json=data)
json_data = json.loads(r.text)
json_data["account_id"] = data["account_id"]
print(json.dumps(json_data))
path = _json_to_file('awsaccounts.out', data["account_id"], json_data)
print("Pushed '{}' AWS accounts.".format(count))
print("You can now use the json files in the awsaccounts.out folder to automate the AWS External ID onboarding using AWS APIs.")
def push_logpipelines(options):
count = 0
fJSON = _files_to_json("logpipelines")
if not fJSON:
exit("No logpipelines are locally available. Consider pulling logpipelines first.")
for item in fJSON:
with open(item) as f:
data = json.load(f)
count = count + 1
print("Pushing {}".format(data["id"].encode('utf8')))
itemId = data['id']
del data['id']
del data['is_read_only']
del data['type']
headers = {'content-type': 'application/json'}
if not arguments["--dry-run"]:
r = requests.post('{}api/v1/logs/config/pipelines?api_key={}&application_key={}'.format(options["api_host"], options["api_key"], options["app_key"]), headers=headers, json=data)
json_data = json.loads(r.text)
json_data["id"] = itemId
path = _json_to_file('logpipelines.out', itemId, json_data)
print("Pushed '{}' log pipelines.".format(count))
def push_notebooks(options):
count = 0
notebooks = _files_to_json("notebooks")
if not notebooks:
exit("No notebooks are locally available. Consider pulling notebooks first.")
for notebook in notebooks:
with open(notebook) as f:
data = json.load(f)
count = count + 1
print("Pushing: {}".format(data["data"]["attributes"]["name"].encode('utf8')))
if not arguments["--dry-run"]:
headers={'content-type': 'application/json', 'DD-API-KEY': options["api_key"], 'DD-APPLICATION-KEY': options["app_key"]}
r = requests.post('{}/api/v1/notebooks'.format(options["api_host"]), headers=headers, json=data)
print("Pushed '{}' notebooks".format(count))
def push_slos(options):
count=0
slos = _files_to_json("slos")
if not slos:
exit("No SLOs are locally available. Consider pulling the SLOs first.")
for slo in slos:
with open(slo) as f:
data = json.load(f)
count = count + 1
print("Pushing: {}".format(data["name"].encode('utf-8')))
if not arguments["--dry-run"]:
r = requests.post('{}api/v1/slo?api_key={}&application_key={}'.format(options["api_host"], options["api_key"], options["app_key"]), json=data)
print("Pushed '{}' SLOs".format(count))
if __name__ == '__main__':
arguments = docopt(__doc__, version='0.1.1rc')
if arguments["--dry-run"]:
print("You are running in dry-mode. No changes will be commmited to your Datadog account(s).")
if arguments["pull"]:
_init_options("pull")
if arguments['<type>'] == 'dashboards':
pull_dashboards()
elif arguments['<type>'] == 'monitors':
pull_monitors(arguments["--tag"])
elif arguments['<type>'] == 'users':
pull_users()
elif arguments['<type>'] == 'synthetics_api_tests':
pull_synthetics_api_tests(_init_options("pull"), arguments["--tag"])
elif arguments['<type>'] == 'synthetics_browser_tests':
pull_synthetics_browser_tests(_init_options("pull"), arguments["--tag"])
elif arguments['<type>'] == 'awsaccounts':
pull_awsaccounts(_init_options("pull"))
elif arguments['<type>'] == 'logpipelines':
pull_logpipelines(_init_options("pull"))
elif arguments['<type>'] == 'notebooks':
pull_notebooks(_init_options("pull"))
elif arguments['<type>'] == 'slos':
pull_slos(_init_options("pull"))
elif arguments["push"]:
_init_options("push")
if arguments['<type>'] == 'dashboards':
push_dashboards()
elif arguments['<type>'] == 'monitors':
push_monitors()
elif arguments['<type>'] == 'users':
push_users()
elif arguments['<type>'] == 'synthetics_api_tests':
push_synthetics_api_tests(_init_options("push"))
elif arguments['<type>'] == 'synthetics_browser_tests':
push_synthetics_browser_tests(_init_options("push"))
elif arguments['<type>'] == 'awsaccounts':
push_awsaccounts(_init_options("push"))
elif arguments['<type>'] == 'logpipelines':
push_logpipelines(_init_options("push"))
elif arguments['<type>'] == 'notebooks':
push_notebooks(_init_options("push"))
elif arguments['<type>'] == 'slos':
push_slos(_init_options("push"))