-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(docs): Add python script to generate
.tfvars
file from CSV in…
…put and add usage to `README.md`
- Loading branch information
1 parent
d3acc20
commit ace82bf
Showing
3 changed files
with
128 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import argparse | ||
import csv | ||
import os | ||
import sys | ||
|
||
DEFAULT_OUTPUT_TFVARS_FILE = 'BoschIPCameraStores.tfvars' | ||
|
||
def validate_file_exists(file_path): | ||
if not os.path.exists(file_path): | ||
print(f"Error: The file '{file_path}' does not exist.") | ||
sys.exit(1) | ||
|
||
def get_args(interactive): | ||
parser = argparse.ArgumentParser(description=""" | ||
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. | ||
Usage: | ||
csv2tfvars.py -csv <input_csv_file> -orch <orchestrator_name> [output_tfvars_file] [-i] | ||
csv2tfvars.py --help | ||
The -i flag enables interactive mode, prompting for any missing required inputs.""", | ||
formatter_class=argparse.RawTextHelpFormatter) | ||
|
||
parser.add_argument('-csv', '--csv_file', type=str, required=False, help='Path to the input CSV file. Required unless in interactive mode.') | ||
parser.add_argument('-orch', '--orchestrator_name', type=str, required=False, help='Orchestrator name. Required unless in interactive mode.') | ||
parser.add_argument('output_tfvars_file', nargs='?', default=DEFAULT_OUTPUT_TFVARS_FILE, help='Output TFVARS file path. Optional, defaults to BoschIPCameraStores.tfvars.') | ||
parser.add_argument('-i', '--interactive', action='store_true', help='Run in interactive mode. Prompts for missing inputs.') | ||
|
||
args = parser.parse_args() | ||
|
||
if interactive: | ||
if not args.csv_file: | ||
args.csv_file = input("Enter the input CSV file path: ") | ||
if not args.orchestrator_name: | ||
args.orchestrator_name = input("Enter the orchestrator_name: ") | ||
if args.output_tfvars_file == DEFAULT_OUTPUT_TFVARS_FILE: # Default value | ||
args.output_tfvars_file = input("Enter the output TFVARS file path (default is 'BoschIPCameraStores.tfvars'): ") or DEFAULT_OUTPUT_TFVARS_FILE | ||
else: | ||
if not args.csv_file or not args.orchestrator_name: | ||
parser.print_help() | ||
sys.exit(1) | ||
|
||
validate_file_exists(args.csv_file) | ||
return args | ||
|
||
def main(): | ||
args = get_args('-i' in sys.argv) | ||
|
||
camera_map = {} | ||
with open(args.csv_file, mode='r', encoding='utf-8') as csvfile: | ||
reader = csv.DictReader(csvfile) | ||
for row in reader: | ||
camera_map[row['serial_number']] = { | ||
'ip': row['ip'], | ||
'username': row['username'], | ||
'password': row['password'] | ||
} | ||
|
||
with open(args.output_tfvars_file, mode='w', encoding='utf-8') as tfvarsfile: | ||
tfvarsfile.write(f'orchestrator_name="{args.orchestrator_name}"\n') | ||
tfvarsfile.write('camera_map = {\n') | ||
for serial, details in camera_map.items(): | ||
tfvarsfile.write(f' "{serial}" = {{\n') | ||
tfvarsfile.write(f' ip = "{details["ip"]}"\n') | ||
tfvarsfile.write(f' username = "{details["username"]}"\n') | ||
tfvarsfile.write(f' password = "{details["password"]}"\n') | ||
tfvarsfile.write(' }\n') | ||
tfvarsfile.write('}\n') | ||
print(f"TFVARS file generated: {args.output_tfvars_file}") | ||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
serial_number,ip,username,password | ||
068745431065110091,192.168.0.1:4444,camera1_admin,camera1_password | ||
068745431065110092,192.168.0.2:4444,camera2_admin,camera2_password | ||
068745431065110093,192.168.0.3:4444,camera3_admin,camera3_password | ||
068745431065110094,192.168.0.4:4444,camera4_admin,camera4_password | ||
068745431065110095,192.168.0.5:4444,camera5_admin,camera5_password | ||
068745431065110096,192.168.0.6:4444,camera6_admin,camera6_password | ||
068745431065110097,192.168.0.7:4444,camera7_admin,camera7_password | ||
068745431065110098,192.168.0.8:4444,camera8_admin,camera8_password | ||
068745431065110099,192.168.0.8:4444,camera9_admin,camera9_password | ||
068745431065110100,192.168.0.9:4444,camera10_admin,camera10_password |