-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimport_config.py
96 lines (79 loc) · 3.12 KB
/
import_config.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
# This script will generate terraform files for all possibly Cloudflare
# settings for supported resources, and import (most of them) in the Terraform
# state file. More details in:
# https://developers.cloudflare.com/terraform/advanced-topics/import-cloudflare-resources
# The script uses a list of resources extracted from (valid in July/2024):
# https://github.com/cloudflare/cf-terraforming?tab=readme-ov-file#supported-resources
# HOW TO USE IT:
# --------------
# Clone the repo, create the .env file with the below variables and execute "run.sh":
# TF_VAR_API_TOKEN=XXXXXXXXXXXX
# ACCOUNT=XXXXXXXX
# ZONE01=XXXXXX
# ZONE02=xxxxx
#
# Ps: The number of zones is added/removed manually. Change it as necessary.
import csv
import os
from dotenv import load_dotenv # type: ignore
load_dotenv()
CLOUDFLARE_EMAIL = os.getenv('CLOUDFLARE_EMAIL')
TF_VAR_API_TOKEN = os.getenv('TF_VAR_API_TOKEN')
ACCOUNT = os.getenv('ACCOUNT')
ZONE01 = os.getenv('ZONE01')
ZONE02 = os.getenv('ZONE02')
SIMULATION = os.getenv('SIMULATION')
def read_CSV(csv_file) -> list:
"""
docstring
"""
temp = []
with open(csv_file, 'r') as file:
records = csv.DictReader(file)
for record in records:
temp.append(record)
return temp
def account_or_zone(ResourceScope) -> list:
if ResourceScope == "Account":
acc_zone = [f"--account {ACCOUNT}"]
else:
acc_zone = [f"--zone {ZONE01}", f"--zone {ZONE02}"]
return acc_zone
def generate_config(record):
if eval(str(record["GenerateSupported"])) is True:
acc_zone = account_or_zone(record["ResourceScope"])
for id in acc_zone:
if eval(str(SIMULATION)) is True:
tty_file = ""
else:
tty_file = f"> {record['Resource']}-{id[2:5].upper()}{id[10]}.tf"
os.system(f"cf-terraforming generate --email {CLOUDFLARE_EMAIL} \
--token {TF_VAR_API_TOKEN} {id} --resource-type \
{record['Resource']} {tty_file}")
return "Generated configuration"
def import_config(record):
if eval(record["ImportSupported"]) is True:
acc_zone = account_or_zone(record["ResourceScope"])
for id in acc_zone:
os.system(f"cf-terraforming import --email {CLOUDFLARE_EMAIL} \
--token {TF_VAR_API_TOKEN} {id} --resource-type \
{record['Resource']} >> import_list.txt")
with open('import_list.txt', 'r') as file:
import_list = file.readlines()
os.remove('import_list.txt')
if eval(str(SIMULATION)) is True:
print(import_list)
else:
for command in import_list:
print ("------> COMMAND:", command)
os.system(command)
return "Resource imported into state file"
def main():
resource_types = read_CSV("./resource-types.csv")
for item in resource_types:
print(">>>> Attempting generation and import for:", item)
print(generate_config(item))
print(import_config(item))
if __name__ == '__main__':
main()