Skip to content

Commit ace82bf

Browse files
committed
chore(docs): Add python script to generate .tfvars file from CSV input and add usage to README.md
1 parent d3acc20 commit ace82bf

File tree

3 files changed

+128
-0
lines changed

3 files changed

+128
-0
lines changed

examples/terraform/multiple_unique_creds/README.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,51 @@ terraform plan
2323
terraform apply
2424
```
2525

26+
### Generate tfvars file from CSV
27+
Alternatively, you can generate the `.tfvars` file from a CSV file using the template `example.csv` and running the
28+
python script `csv2tfvars.py`. This script will generate a `.tfvars` based on the inputs of the CSV file.
29+
30+
#### Usage
31+
```text
32+
python csv2tfvars.py -h
33+
usage: csv2tfvars.py [-h] -csv CSV_FILE -orch ORCHESTRATOR_NAME [-i] [output_tfvars_file]
34+
35+
Convert CSV to TFVARS. This script parses a given CSV file containing camera information and generates a Terraform variables file (.tfvars) with the data structured for Terraform usage.
36+
37+
Usage:
38+
csv2tfvars.py -csv <input_csv_file> -orch <orchestrator_name> [output_tfvars_file] [-i]
39+
csv2tfvars.py --help
40+
41+
The -i flag enables interactive mode, prompting for any missing required inputs.
42+
43+
positional arguments:
44+
output_tfvars_file Output TFVARS file path. Optional, defaults to BoschIPCameraStores.tfvars.
45+
46+
optional arguments:
47+
-h, --help show this help message and exit
48+
-csv CSV_FILE, --csv_file CSV_FILE
49+
Path to the input CSV file. Required unless in interactive mode.
50+
-orch ORCHESTRATOR_NAME, --orchestrator_name ORCHESTRATOR_NAME
51+
Orchestrator name. Required unless in interactive mode.
52+
-i, --interactive Run in interactive mode. Prompts for missing inputs.
53+
```
54+
55+
#### Interactive Example
56+
```bash
57+
python csv2tfvars.py -i
58+
```
59+
```text
60+
Enter the input CSV file path: example.csv
61+
Enter the orchestrator_name: my-uo-client-name
62+
Enter the output TFVARS file path (default is 'BoschIPCameraStores.tfvars'):
63+
TFVARS file generated: BoschIPCameraStores.tfvars
64+
```
65+
66+
#### Non-Interactive Example
67+
```bash
68+
python csv2tfvars.py -csv example.csv -orch my-uo-client-name
69+
```
70+
2671
<!-- BEGIN_TF_DOCS -->
2772
## Requirements
2873

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import argparse
2+
import csv
3+
import os
4+
import sys
5+
6+
DEFAULT_OUTPUT_TFVARS_FILE = 'BoschIPCameraStores.tfvars'
7+
8+
def validate_file_exists(file_path):
9+
if not os.path.exists(file_path):
10+
print(f"Error: The file '{file_path}' does not exist.")
11+
sys.exit(1)
12+
13+
def get_args(interactive):
14+
parser = argparse.ArgumentParser(description="""
15+
Convert CSV to TFVARS. This script parses a given CSV file containing camera information and generates a Terraform variables file (.tfvars) with the data structured for Terraform usage.
16+
17+
Usage:
18+
csv2tfvars.py -csv <input_csv_file> -orch <orchestrator_name> [output_tfvars_file] [-i]
19+
csv2tfvars.py --help
20+
21+
The -i flag enables interactive mode, prompting for any missing required inputs.""",
22+
formatter_class=argparse.RawTextHelpFormatter)
23+
24+
parser.add_argument('-csv', '--csv_file', type=str, required=False, help='Path to the input CSV file. Required unless in interactive mode.')
25+
parser.add_argument('-orch', '--orchestrator_name', type=str, required=False, help='Orchestrator name. Required unless in interactive mode.')
26+
parser.add_argument('output_tfvars_file', nargs='?', default=DEFAULT_OUTPUT_TFVARS_FILE, help='Output TFVARS file path. Optional, defaults to BoschIPCameraStores.tfvars.')
27+
parser.add_argument('-i', '--interactive', action='store_true', help='Run in interactive mode. Prompts for missing inputs.')
28+
29+
args = parser.parse_args()
30+
31+
if interactive:
32+
if not args.csv_file:
33+
args.csv_file = input("Enter the input CSV file path: ")
34+
if not args.orchestrator_name:
35+
args.orchestrator_name = input("Enter the orchestrator_name: ")
36+
if args.output_tfvars_file == DEFAULT_OUTPUT_TFVARS_FILE: # Default value
37+
args.output_tfvars_file = input("Enter the output TFVARS file path (default is 'BoschIPCameraStores.tfvars'): ") or DEFAULT_OUTPUT_TFVARS_FILE
38+
else:
39+
if not args.csv_file or not args.orchestrator_name:
40+
parser.print_help()
41+
sys.exit(1)
42+
43+
validate_file_exists(args.csv_file)
44+
return args
45+
46+
def main():
47+
args = get_args('-i' in sys.argv)
48+
49+
camera_map = {}
50+
with open(args.csv_file, mode='r', encoding='utf-8') as csvfile:
51+
reader = csv.DictReader(csvfile)
52+
for row in reader:
53+
camera_map[row['serial_number']] = {
54+
'ip': row['ip'],
55+
'username': row['username'],
56+
'password': row['password']
57+
}
58+
59+
with open(args.output_tfvars_file, mode='w', encoding='utf-8') as tfvarsfile:
60+
tfvarsfile.write(f'orchestrator_name="{args.orchestrator_name}"\n')
61+
tfvarsfile.write('camera_map = {\n')
62+
for serial, details in camera_map.items():
63+
tfvarsfile.write(f' "{serial}" = {{\n')
64+
tfvarsfile.write(f' ip = "{details["ip"]}"\n')
65+
tfvarsfile.write(f' username = "{details["username"]}"\n')
66+
tfvarsfile.write(f' password = "{details["password"]}"\n')
67+
tfvarsfile.write(' }\n')
68+
tfvarsfile.write('}\n')
69+
print(f"TFVARS file generated: {args.output_tfvars_file}")
70+
71+
if __name__ == "__main__":
72+
main()
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
serial_number,ip,username,password
2+
068745431065110091,192.168.0.1:4444,camera1_admin,camera1_password
3+
068745431065110092,192.168.0.2:4444,camera2_admin,camera2_password
4+
068745431065110093,192.168.0.3:4444,camera3_admin,camera3_password
5+
068745431065110094,192.168.0.4:4444,camera4_admin,camera4_password
6+
068745431065110095,192.168.0.5:4444,camera5_admin,camera5_password
7+
068745431065110096,192.168.0.6:4444,camera6_admin,camera6_password
8+
068745431065110097,192.168.0.7:4444,camera7_admin,camera7_password
9+
068745431065110098,192.168.0.8:4444,camera8_admin,camera8_password
10+
068745431065110099,192.168.0.8:4444,camera9_admin,camera9_password
11+
068745431065110100,192.168.0.9:4444,camera10_admin,camera10_password

0 commit comments

Comments
 (0)