-
Notifications
You must be signed in to change notification settings - Fork 0
/
var_format.py
56 lines (51 loc) · 1.99 KB
/
var_format.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
import csv
from decouple import config
# Function to Read CSV File
def read_csv(file):
with open(file) as csv_file:
csv_data = csv.reader(csv_file, delimiter=',')
csv_data_list = []
csv_data_final_list = []
for row in csv_data:
csv_data_list.append(row)
for csv_final_list in csv_data_list:
del csv_final_list[-1]
csv_data_final_list.append(csv_final_list)
return csv_data_final_list
"""
The goal of the script is to read the AWS account IDs from a CSV file and
create a new file that will be used by Terraform to add the aws account IDs
to the Policies in the AWS SDP Prod account.
"""
def main():
# Read CSV File and assing to a variable
file_var = config('IMPORT_FILE')
master_list = read_csv(file_var)
aws_ids_arn_list = []
aws_account_ids_list = []
# Read Line by Line of CSV File
for aws_ids_line in master_list:
if aws_ids_line[8].upper().replace(" ", "") == "Y":
aws_account_id_list = aws_ids_line[4].split(",")
for aws_account_id in aws_account_id_list:
if aws_account_id not in aws_account_ids_list:
aws_account_ids_list.append(aws_account_id)
aws_ids_arn_list.append('"arn:aws:iam::' + aws_account_id + ': role/SDP_NameResolver"')
arn_parameters = str(aws_ids_arn_list)
else:
pass
else:
pass
# Create the variables file and append line by line
try:
with open("./variables.tf", 'w') as var_file:
var_file.write('variable "aws_account_ids" {\n')
var_file.write('\tdescription = "Create Assumed roles"\n')
var_file.write('\ttype = list\n')
var_file.write('\tdefault = ')
var_file.write(arn_parameters.replace("'", "").replace(" ",""))
var_file.write('\n}')
except NameError:
arn_parameters = None
if __name__ == "__main__":
main()