-
Notifications
You must be signed in to change notification settings - Fork 1
/
format-policies.py
68 lines (50 loc) · 1.5 KB
/
format-policies.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
import argparse
import pickle
import re
invalid_chars = re.compile(r'[^0-9A-Za-z_-]')
def format_output_name(policy_name):
return invalid_chars.sub('_', policy_name)
def arn2url(arn):
return 'https://console.aws.amazon.com/iam/home?#/policies/{}'.format(arn)
def policies2outputs(policies):
template = '\n'.join([
'output "{}" {{',
' value = "{}"',
' description = "{}"',
'}}',
''
])
return '\n'.join([
template.format(
format_output_name(policy['PolicyName']),
policy['Arn'],
arn2url(policy['Arn']),
)
for policy in policies
])
def policies2readme(policies):
table = [
'| Name | Description |',
'|------|-------------|',
]
table.extend([
'| {} | {} |'.format(
format_output_name(policy['PolicyName']),
arn2url(policy['Arn']),
)
for policy in policies
])
return '\n'.join(table)
def main():
parser = argparse.ArgumentParser()
parser.add_argument('policies', help='Pickle dump of policies.')
parser.add_argument('-t', '--type', dest='type', choices=['outputs.tf', 'README.md'], help='Format type.')
args = parser.parse_args()
with open(args.policies, 'rb') as f:
policies = pickle.load(f)
if args.type == 'outputs.tf':
print(policies2outputs(policies))
elif args.type == 'README.md':
print(policies2readme(policies))
if __name__ == '__main__':
main()