Skip to content

Commit 6672991

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 6672991

File tree

6 files changed

+274
-3
lines changed

6 files changed

+274
-3
lines changed

examples/terraform/multiple_same_creds/README.md

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,17 @@ This is an example of how to create multiple camera stores in Keyfactor Command
88
- The Keyfactor Terraform provider is installed and configured to communicate to Keyfactor Command. Review
99
the [Keyfactor provider documentation](https://registry.terraform.io/providers/keyfactor-pub/keyfactor/latest/docs)
1010
for more information.
11-
- The `BIPCamera` store type is already created in Keyfactor Command. See the [Extension specific documentation](https://github.com/Keyfactor/bosch-ipcamera-orchestrator?tab=readme-ov-file#store-type-configuration)
11+
- The `BIPCamera` store type is already created in Keyfactor Command. See
12+
the [Extension specific documentation](https://github.com/Keyfactor/bosch-ipcamera-orchestrator?tab=readme-ov-file#store-type-configuration)
1213
for more information.
13-
- An orchestrator with the BoschIPCamera extension is registered and approved in Keyfactor Command.
14+
- An orchestrator with the BoschIPCamera extension is registered and approved in Keyfactor Command.
1415

1516
## Usage
1617

1718
Modify the `example.tfvars` file to include the necessary information for your environment. Alternatively Terraform will
1819
prompt for each input if no value is provided.
1920

20-
*NOTE*: This example assumes all cameras are using the same credentials, if this does not suit your use-case then modify
21+
*NOTE*: This example assumes all cameras are using the same credentials, if this does not suit your use-case then modify
2122
accordingly.
2223

2324
```bash
@@ -26,6 +27,63 @@ terraform plan
2627
terraform apply
2728
```
2829

30+
### Generate tfvars file from CSV
31+
32+
Alternatively, you can generate the `.tfvars` file from a CSV file using the template `example.csv` and running the
33+
python script `csv2tfvars.py`. This script will generate a `.tfvars` based on the inputs of the CSV file.
34+
35+
#### Usage
36+
37+
```text
38+
python csv2tfvars.py -h
39+
usage: csv2tfvars.py [-h] [-csv CSV_FILE] [-u SERVER_USERNAME] [-p SERVER_PASSWORD] [-orch ORCHESTRATOR_NAME] [-i] [output_tfvars_file]
40+
41+
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.
42+
43+
Usage:
44+
csv2tfvars.py -csv <input_csv_file> -orch <orchestrator_name> [output_tfvars_file] [-i]
45+
csv2tfvars.py --help
46+
47+
The -i flag enables interactive mode, prompting for any missing required inputs.
48+
49+
positional arguments:
50+
output_tfvars_file Output TFVARS file path. Optional, defaults to BoschIPCameraStores.tfvars.
51+
52+
optional arguments:
53+
-h, --help show this help message and exit
54+
-csv CSV_FILE, --csv_file CSV_FILE
55+
Path to the input CSV file. Required unless in interactive mode.
56+
-u SERVER_USERNAME, --server_username SERVER_USERNAME
57+
Username for IP cameras. Required unless in interactive mode.
58+
-p SERVER_PASSWORD, --server_password SERVER_PASSWORD
59+
Password for IP cameras. Required unless in interactive mode.
60+
-orch ORCHESTRATOR_NAME, --orchestrator_name ORCHESTRATOR_NAME
61+
Orchestrator client name. Required unless in interactive mode.
62+
-i, --interactive Run in interactive mode. Prompts for missing inputs.
63+
```
64+
65+
#### Interactive Example
66+
67+
```bash
68+
python csv2tfvars.py -i
69+
```
70+
71+
```text
72+
Enter the input CSV file path: example.csv
73+
Enter the server username: admin
74+
Enter the server password: admin
75+
Enter the orchestrator_name: my-uo-client-name
76+
Enter the output TFVARS file path (default is 'BoschIPCameraStores.tfvars'):
77+
TFVARS file generated: BoschIPCameraStores.tfvars
78+
79+
```
80+
81+
#### Non-Interactive Example
82+
83+
```bash
84+
python csv2tfvars.py -csv example.csv -orch my-uo-client-name -u camera_username -p camera_passwd
85+
```
86+
2987
<!-- BEGIN_TF_DOCS -->
3088

3189
## Requirements
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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('-u', '--server_username', type=str, required=False, help='Username for IP cameras. Required unless in interactive mode.')
26+
parser.add_argument('-p', '--server_password', type=str, required=False, help='Password for IP cameras. Required unless in interactive mode.')
27+
parser.add_argument('-orch', '--orchestrator_name', type=str, required=False, help='Orchestrator client name. Required unless in interactive mode.')
28+
parser.add_argument('output_tfvars_file', nargs='?', default=DEFAULT_OUTPUT_TFVARS_FILE, help='Output TFVARS file path. Optional, defaults to BoschIPCameraStores.tfvars.')
29+
parser.add_argument('-i', '--interactive', action='store_true', help='Run in interactive mode. Prompts for missing inputs.')
30+
31+
args = parser.parse_args()
32+
33+
if interactive:
34+
if not args.csv_file:
35+
args.csv_file = input("Enter the input CSV file path: ")
36+
if not args.server_username:
37+
args.server_username = input("Enter the server username: ")
38+
if not args.server_password:
39+
args.server_password = input("Enter the server password: ")
40+
if not args.orchestrator_name:
41+
args.orchestrator_name = input("Enter the orchestrator_name: ")
42+
if args.output_tfvars_file == DEFAULT_OUTPUT_TFVARS_FILE: # Default value
43+
args.output_tfvars_file = input("Enter the output TFVARS file path (default is 'BoschIPCameraStores.tfvars'): ") or DEFAULT_OUTPUT_TFVARS_FILE
44+
else:
45+
if not args.csv_file or not args.orchestrator_name:
46+
parser.print_help()
47+
sys.exit(1)
48+
49+
validate_file_exists(args.csv_file)
50+
return args
51+
52+
def main():
53+
args = get_args('-i' in sys.argv)
54+
55+
camera_map = {}
56+
with open(args.csv_file, mode='r', encoding='utf-8') as csvfile:
57+
reader = csv.DictReader(csvfile)
58+
for row in reader:
59+
camera_map[row['serial_number']] = {
60+
'ip': row['ip'],
61+
}
62+
63+
with open(args.output_tfvars_file, mode='w', encoding='utf-8') as tfvarsfile:
64+
tfvarsfile.write(f'orchestrator_name="{args.orchestrator_name}"\n')
65+
tfvarsfile.write(f'server_username="{args.server_username}"\n')
66+
tfvarsfile.write(f'server_password="{args.server_password}"\n')
67+
tfvarsfile.write('camera_map = {\n')
68+
for serial, details in camera_map.items():
69+
tfvarsfile.write(f' "{serial}" = "{details["ip"]}"\n')
70+
tfvarsfile.write('}\n')
71+
print(f"TFVARS file generated: {args.output_tfvars_file}")
72+
73+
if __name__ == "__main__":
74+
main()
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
serial_number,ip
2+
068745431065110091,192.168.0.1:4444
3+
068745431065110092,192.168.0.2:4444
4+
068745431065110093,192.168.0.3:4444
5+
068745431065110094,192.168.0.4:4444
6+
068745431065110095,192.168.0.5:4444
7+
068745431065110096,192.168.0.6:4444
8+
068745431065110097,192.168.0.7:4444
9+
068745431065110098,192.168.0.8:4444
10+
068745431065110099,192.168.0.8:4444
11+
068745431065110100,192.168.0.9:4444

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)