-
Notifications
You must be signed in to change notification settings - Fork 0
/
tmc.py
executable file
·425 lines (378 loc) · 12.7 KB
/
tmc.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
#!/usr/bin/env python
import argparse
from collections import namedtuple
from concurrent.futures import ThreadPoolExecutor
import jmespath
import json
import os
import requests
import sys
import textwrap
import time
EX = ThreadPoolExecutor(max_workers=5)
color = namedtuple('color', 'red green yellow blue bold endc none')(*[
lambda s, u=c: '\033[%sm%s\033[0m' % (u, s)
if (sys.platform != 'win32' and u != '') else s
for c in '91,92,93,94,1,0,'.split(',')
])
PDQ_CONFIG = {
'workspace-policies': {
'headers': [
'fullName.workspaceName', 'fullName.name',
'spec.type', 'spec.recipe'
],
'api_join': {
'path': '/v1alpha1/workspaces',
'name_attrs': ['name'],
'join_path': '/v1alpha1/workspaces/{0}/policies'
}
},
'policy-recipes': {
'headers': ['fullName.typeName', 'fullName.name'],
'api': {
'path': '/v1alpha1/policy/types/*/recipes',
'transform': 'recipes[]'
}
},
'cluster-policies': {
'headers': ['fullName.clusterName', 'spec.type', 'spec.recipe'],
'api_join': {
'path': '/v1alpha1/clusters',
'name_attrs': ['name', 'provisionerName', 'managementClusterName'],
'join_path': (
'/v1alpha1/clusters/{0}/policies?'
+ '&searchScope.managementClusterName={1}'
+ '&searchScope.provisionerName={2}'
)
}
},
'clustergroup-policies': {
'headers': [
'fullName.clusterGroupName', 'fullName.name',
'spec.type', 'spec.recipe'
],
'api_join': {
'path': '/v1alpha1/clustergroups',
'name_attrs': ['name'],
'join_path': '/v1alpha1/clustergroups/{0}/policies',
'entity': 'clusterGroups'
}
},
'cluster-namespaces': {
'headers': [
'fullName.name', 'spec.workspaceName', 'fullName.clusterName'
],
'api': {
'path': '/v1alpha1/clusters/*/namespaces',
'transform': 'namespaces[]'
}
},
'organization-policies': {
'headers': ['fullName.name', 'spec.type', 'spec.recipe'],
'api': {
'path': '/v1alpha1/organization/policies',
'transform': 'policies[]'
}
},
'cluster-policy-violations': {
'headers': [
'id', 'time', 'data.status', 'data.category',
'data.policyName', 'data.cluster.name'
],
'api': {
'path': (
'/v1alpha1/events?query='
+ 'subject:[cluster_policy_violation]'
# + 'subject:[cluster_policy_violation] and a.status:NEW'
),
'transform': "events[?data.status=='NEW']"
# 'transform': "events[]"
}
}
}
def error(msg, label='ERROR'):
print(color.red('%s: %s' % (label, msg)))
def fatal(msg):
error(msg, 'FATAL')
sys.exit(1)
def debug(msg):
if os.environ.get('TMC_DEBUG') == 'TRUE':
print(color.green('DEBUG: %s' % msg))
def now_secs():
return int(round(time.time()))
def delta_secs(start_secs):
return now_secs() - start_secs
def get_temp_basedir(base_dir=None):
default_base_dir = '/tmp'
if sys.platform == 'win32':
default_base_dir = os.path.join('C:', 'temp')
if base_dir is None:
base_dir = os.environ.get('TEMP', default_base_dir)
return base_dir
def get_cache_file(name):
return os.path.join(
get_temp_basedir(),
'tmc-cache_%s.json' % (name)
)
def get_cache(name, raw=False):
cache_file = get_cache_file(name)
# print('cache_file: %s' % cache_file)
if os.path.isfile(cache_file):
d = read_file(cache_file)
if raw is True:
return d
if now_secs() < d['expires']:
return d['data']
return None
def write_cache(name, data, expire_secs=None):
write_file(get_cache_file(name), {
'expires': now_secs() + (expire_secs or 60),
'data': data
}, print_msg=False)
def read_file(file, print_msg=False):
data = json.loads(open(file, 'r').read())
if print_msg is True:
print('read %s' % file)
return data
def write_file(file, data, print_msg=False):
with open(file, 'w') as fh:
fh.write(json.dumps(data, indent=2))
if print_msg is True:
print('written %s' % file)
def api(path, method='GET', **kwargs):
kwargs = dict(**kwargs)
transform = kwargs.pop('transform', None)
cache = kwargs.pop('cache', None)
expire_mins = kwargs.pop('expire_mins', None)
allowed_codes = kwargs.pop('allowed_codes', [200])
limit = kwargs.pop('limit', None)
data = None
no_cache = os.environ.get('TMC_NO_CACHE') == 'TRUE'
if cache is not None and no_cache is False:
data = get_cache(cache)
if data is not None:
debug('retrieved %s data from cache' % cache)
return data
paginate = kwargs.pop('paginate', None)
pagination = {
'pagination.size': 100,
'includeTotalCount': True
}
if limit is not None:
if limit < pagination['pagination.size']:
pagination['pagination.size'] = limit
# get access_token
access_token = None
if no_cache is False:
access_token = get_cache('token')
if access_token is None:
r = requests.post(
'https://console.cloud.vmware.com/csp/gateway/am'
+ '/api/auth/api-tokens/authorize',
data={'refresh_token': os.environ['TMC_TOKEN']}
)
if r.status_code != 200:
fatal('unable to get access_token HTTP %s: %s' % (
r.status_code, r.content
))
access_token = r.json()['access_token']
if no_cache is False:
write_cache('token', access_token, r.json()['expires_in'])
if 'headers' not in kwargs:
kwargs['headers'] = {
'Authorization': 'Bearer %s' % access_token,
'Accept': 'application/json'
}
url = 'https://%s.tmc.cloud.vmware.com%s' % (
os.environ['TMC_DOMAIN'], path
)
data = []
while True:
if paginate is not None:
if 'params' not in kwargs:
kwargs['params'] = {}
kwargs['params'].update(pagination)
r = requests.Session().request(
method.lower(), url, **kwargs
)
debug('HTTP kwargs: %s' % json.dumps(kwargs, indent=2))
debug('HTTP %s %s [%s]' % (method.upper(), url, r.status_code))
if r.status_code not in allowed_codes:
errmsg = None
try:
errmsg = r.json()['error']
except Exception:
errmsg = r.content
fatal('HTTP %s %s [%s] %s' % (
method, url, r.status_code, errmsg
))
_data = r.json()
if paginate is not None:
if paginate not in _data or len(_data[paginate]) == 0:
break
data.extend(_data[paginate])
if str(len(data)) == str(_data.get('totalCount')):
break
if limit is not None and len(data) >= limit:
data = data[0:limit]
break
pagination['pagination.offset'] = len(data)
continue
else:
data = _data
break
if transform is not None:
data = jmespath.search(transform, data)
if cache is not None and no_cache is False:
write_cache(cache, data, expire_mins=expire_mins)
debug('written %s data to cache' % cache)
return data
def api_join(path, name_attrs, join_path, cache=False, entity=None):
if entity is None:
entity = path.split('?')[0].split('/')[-1]
data = []
def _join(d):
join_entity = join_path.split('?')[0].split('/')[-1]
data.extend(api(
join_path.format(*d),
transform=join_entity + ' || `[]`',
allowed_codes=[200, 404],
cache='%s-%s-%s' % (entity, join_entity, d[0]) if cache else None
))
list(EX.map(lambda _: _join(_), api(
path,
transform='%s[].fullName.[%s]' % (entity, ', '.join(name_attrs)),
cache=entity if cache else None
)))
return data
def print_table(headers, data, counter=True, sort_key=None, dumpfile=None):
maxwidth = 40 if len(headers) > 1 else 80
colwidths = {h: len(h) for h in headers}
_data = []
if not isinstance(data, list) or len(data) == 0:
print(color.red(' - no data - '))
return
dumpdata = None
if dumpfile is not None:
dumpdata = json.dumps(data, indent=2)
if sort_key is not None:
data = sorted(data, key=lambda k: k[sort_key])
rc = 0
if counter is True:
colwidths[' '] = len(str(len(data)))
headers.insert(0, ' ')
for r in data:
rlines = {}
linec = 0
rc += 1
if counter is True:
r[' '] = str(rc)
for h in headers:
v = jmespath.search(h + ' || ``', r) if '.' in h else r.get(h, '')
_lines = textwrap.wrap(str(v), width=maxwidth)
if len(_lines) > linec:
linec = len(_lines)
rlines[h] = _lines
if len(_lines) > 1:
colwidths[h] = maxwidth
elif len(_lines) == 1 and len(_lines[0]) > colwidths[h]:
colwidths[h] = len(_lines[0])
for lc in range(linec):
_data.append({
h: rlines[h][lc] if lc < len(rlines[h]) else ''
for h in headers
})
def _print_line(d, _color=None, sep='|', hcolor={}):
_line = (' ' + sep + ' ').join(
str(
hcolor[h](d[h]) if h in hcolor else
_color(d[h]) if _color else d[h]
).ljust(
colwidths[h] + (
len(hcolor[h]('')) if h in hcolor else
len(_color('')) if _color else 0
)
)
for h in headers
)
print(_line)
_print_line(
{h: h for h in headers}, color.bold, color.blue('|')
)
_print_line(
{h: '-' * colwidths[h] for h in headers},
color.blue, color.blue('+')
)
for r in _data:
_print_line(r, sep=color.blue('|'), hcolor={
' ': color.bold
})
if dumpfile is not None:
dumppath = os.path.join(get_temp_basedir(), dumpfile)
with open(dumppath, 'w') as fh:
fh.write(dumpdata)
print('written %s' % dumppath)
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description='Tanzu Mission Control API Explorer'
)
parser.add_argument(
'url', help="api path or pdq (use 'pdqs' for list)"
)
parser.add_argument(
'-H', '--headers', help='attribute names in response to use in table'
)
parser.add_argument(
'-l', '--limit', type=int, help='limit results'
)
parser.add_argument(
'-p', '--paginate', help='name of entity to paginate'
)
parser.add_argument(
'-t', '--transform', help='transform response using jmespath'
)
parser.add_argument('--debug', action='store_true')
parser.add_argument('--no-cache', action='store_true')
args = parser.parse_args()
missing = [
ev for ev in ['TMC_TOKEN', 'TMC_DOMAIN'] if ev not in os.environ
]
if len(missing) > 0:
fatal('environment variable(s) required: %s' % ', '.join(missing))
transform = args.transform
headers = {}
if args.debug is True:
os.environ['TMC_DEBUG'] = 'TRUE'
if args.no_cache is True:
os.environ['TMC_NO_CACHE'] = 'TRUE'
if args.url == 'pdqs':
print_table(['name'], [{'name': _} for _ in sorted(PDQ_CONFIG.keys())])
elif args.url in PDQ_CONFIG:
pdq = PDQ_CONFIG[args.url]
print_table(
pdq['headers'],
api_join(**pdq['api_join'])
if 'api_join' in pdq else api(**pdq['api']),
dumpfile=args.url + '.json'
)
else:
if not args.url.startswith('/v1alpha1/'):
fatal('--url must start /v1alpha1/')
if args.headers is not None:
if args.paginate is None:
args.paginate = args.url.split('/')[-1]
for path in args.headers.split(','):
name = path
if '.' in path:
name = path.split('.')[-1]
headers[name] = '%s: %s' % (name, path)
transform = '[].{%s}' % ','.join(headers.values())
data = api(
args.url, paginate=args.paginate,
transform=transform,
limit=args.limit
)
if args.headers is not None:
print_table(list(headers.keys()), data)
else:
print(json.dumps(data, indent=2))