usage() { echo "Usage: $0 [-i ] [-v ]" 1>&2; exit 1; } x="no" p="no" f="no" v="no" i="no" #while getopts ":s:g:r:x:p:f:" o; do while getopts ":i:x:f:v:" o; do case "${o}" in i) i=${OPTARG} ;; v) v="yes" ;; *) usage ;; esac done shift $((OPTIND-1)) #if [ -z "${s}" ]; then # usage #fi if [ "$i" == "no" ]; then echo "VPC ID null exiting" exit fi export aws2tfmess="# File generated by aws2tf see https://github.com/awsandy/aws2tf" mysub=`aws sts get-caller-identity | jq .Account | tr -d '"'` if [ "$mysub" == "null" ]; then echo "Account is null exiting" exit fi rm -rf generated/tf.$mysub-vpc mkdir -p generated/tf.$mysub-vpc s=`echo $mysub` cd generated/tf.$mysub-vpc rm -rf .terraform if [ "$f" = "no" ]; then rm -f import.log resources*.txt rm -f processed.txt rm -f *.tf rm -f terraform.* rm -rf .terraform else sort -u processed.txt > pt.txt cp pt.txt processed.txt fi echo " " echo "Account ID = ${s}" echo "Terraform Resource Type Filter = ${r}" echo " " # cleanup from any previous runs rm -f terraform*.backup #rm -f terraform.tfstate rm -f tf*.sh cp ../../stub/*.tf . echo "terraform init" terraform init 2>&1 | tee -a import.log date lc=0 echo "loop through providers" pwd for com in `ls ../../scripts/1*-get-*.sh | cut -d'/' -f4 | sort -g`; do echo "$com" docomm="../../scripts/$com $i" if [ "$f" = "no" ]; then eval $docomm 2>&1 | tee -a import.log else grep "$docomm" processed.txt if [ $? -eq 0 ]; then echo "skipping $docomm" else eval $docomm 2>&1 | tee -a import.log fi fi lc=`expr $lc + 1` file="import.log" while IFS= read -r line do if [[ "${line}" == *"Error:"* ]];then if [[ "${line}" == *"Duplicate"* ]];then echo "Ignoring $line" else echo "Found Error: $line exiting .... (pass for now)" fi fi done <"$file" echo "$docomm" >> processed.txt rm -f terraform*.backup done date echo "---------------------------------------------------------------------------" echo "aws2tf output files are in generated/tf.$mysub-vppc" echo "---------------------------------------------------------------------------" echo "terraform fmt > /dev/null ..." terraform fmt > /dev/null echo "Terraform validate ..." terraform validate . if [ "$v" = "yes" ]; then exit fi echo "Terraform Plan ..." terraform plan . echo "---------------------------------------------------------------------------" echo "aws2tf output files are in generated/tf.$mysub-vpc" echo "---------------------------------------------------------------------------"