forked from mikeghen/python_api_samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_content_validator.py
More file actions
106 lines (84 loc) · 2.95 KB
/
run_content_validator.py
File metadata and controls
106 lines (84 loc) · 2.95 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
import csv
import yaml ### install the pyyaml package
from lookerapi import LookerApi
from pprint import pprint
### Requires API v. 3.1 -- set this in config.yml
### ------- HERE ARE PARAMETERS TO CONFIGURE -------
host = 'cse'
output_csv_name = 'output.csv'
### ------- OPEN THE CONFIG FILE and INSTANTIATE API -------
f = open('config.yml')
params = yaml.load(f)
f.close()
my_host = params['hosts'][host]['host']
my_secret = params['hosts'][host]['secret']
my_token = params['hosts'][host]['token']
looker = LookerApi(host=my_host,
token=my_token,
secret = my_secret)
### ------- GET AND PRINT THE LOOK -------
space_data = looker.get_all_spaces(fields='id, parent_id, name')
data = looker.get_content_validation()
host_url = my_host[:my_host.index(":19999")]
broken_content = data['content_with_errors']
# pprint(broken_content)
output = []
for item in broken_content:
if item['dashboard'] is None:
type = 'look'
else:
type = 'dashboard'
id = item[type]['id']
name = item[type]['title']
url = '{}/{}s/{}'.format(host_url,
type,
id
)
errors = item['errors']
space_id = item[type]['space']['id']
space_name = item[type]['space']['name']
space_url = '{}/spaces/{}'.format(host_url,
space_id
)
if type == 'look':
element = None
else:
dashboard_element = item['dashboard_element']
element = dashboard_element['title'] if dashboard_element is not None else None
# find parent_space
space = next(item for item in space_data if item['id'] == space_id)
parent_space_id = space['parent_id']
if parent_space_id is None:
parent_space_name = None
parent_space_url = None
pass
else:
parent_space_url = '{}/spaces/{}'.format(host_url,
parent_space_id
)
parent_space = next(item for item in space_data if item['id'] == parent_space_id)
parent_space_name = parent_space['name']
data = {
'type' : type,
'name' : name,
'dashboard_element': element,
'url' : url,
'space_name' : space_name,
'parent_space_name': parent_space_name,
'space_url' : space_url,
'parent_space_url': parent_space_url,
'errors': str(errors)
}
output.append(data)
try:
with open(output_csv_name, 'w') as csvfile:
writer = csv.DictWriter(csvfile,
fieldnames=list(output[0].keys()))
writer.writeheader()
for data in output:
writer.writerow(data)
print('Error information outputed to {}'.format(output_csv_name))
except IOError:
print("I/O error")
### ------- Done -------
print("Done")