-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_gateway_enum.py
More file actions
58 lines (40 loc) · 1.63 KB
/
api_gateway_enum.py
File metadata and controls
58 lines (40 loc) · 1.63 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
import os
import boto3
from utils.json_encoder import json_encoder
from utils.json_writer import json_writer
from utils.json_printer import json_printer
from utils.session import get_session
from utils.regions import get_all_regions
from utils.boto_error_handling import yield_handling_errors
def get_api_gateways_for_region(client):
for rest_api in client.get_rest_apis()['items']:
yield rest_api
def get_authorizers(client, api_id):
return client.get_authorizers(restApiId=api_id)['items']
def main():
all_data = {}
session = get_session()
for region in get_all_regions(session):
all_data[region] = {}
client = session.client('apigateway', region_name=region)
iterator = yield_handling_errors(get_api_gateways_for_region, client)
for rest_api in iterator:
api_id = rest_api['id']
print('Region: %s / API ID: %s' % (region, api_id))
try:
authorizers = get_authorizers(client, api_id)
except Exception as e:
msg = 'Failed to retrieve authorizers for %s @ %s. Error: "%s"'
args = (api_id, region, e)
print(msg % args)
authorizers = {}
all_data[region][api_id] = {}
all_data[region][api_id]['main'] = rest_api
all_data[region][api_id]['authorizers'] = authorizers
else:
print('Region: %s / No API gateways' % region)
os.makedirs('output', exist_ok=True)
json_writer('output/api-gateways.json', all_data)
json_printer(all_data)
if __name__ == '__main__':
main()