#!/bin/bash cmd[0]="$AWS ds describe-directories" pref[0]="DirectoryDescriptions" tft[0]="aws_directory_service_directory" for c in `seq 0 0`; do cm=${cmd[$c]} ttft=${tft[(${c})]} #echo $cm awsout=`eval $cm 2> /dev/null` if [ "$awsout" == "" ];then echo "$cm : You don't have access for this resource" exit fi count=`echo $awsout | jq ".${pref[(${c})]} | length"` if [ "$count" -gt "0" ]; then count=`expr $count - 1` for i in `seq 0 $count`; do #echo $i cname=`echo $awsout | jq ".${pref[(${c})]}[(${i})].DirectoryId" | tr -d '"'` rname=${cname//:/_} && rname=${rname//./_} && rname=${rname//\//_} echo "$ttft $cname import" fn=`printf "%s__%s.tf" $ttft $rname` if [ -f "$fn" ] ; then echo "$fn exists already skipping" && continue; fi printf "resource \"%s\" \"%s\" {}" $ttft $rname > $fn terraform import $ttft.$rname "$cname" | grep Importing terraform state show -no-color $ttft.$rname > t1.txt rm -f $fn file="t1.txt" subnets=() echo $aws2tfmess > $fn while IFS= read line do skip=0 # display $line or do something with $line t1=`echo "$line"` if [[ ${t1} == *"="* ]];then tt1=`echo "$line" | cut -f1 -d'=' | tr -d ' '` tt2=`echo "$line" | cut -f2- -d'='` if [[ ${tt1} == "arn" ]];then skip=1 fi if [[ ${tt1} == "id" ]];then skip=1; fi if [[ ${tt1} == "role_arn" ]];then skip=1;fi if [[ ${tt1} == "owner_id" ]];then skip=1;fi if [[ ${tt1} == "availability_zone" ]];then skip=1;fi if [[ ${tt1} == "availability_zone_id" ]];then skip=1;fi if [[ ${tt1} == "access_url" ]];then t1="password = \"set me\"" printf "lifecycle {\n" >>$fn printf " ignore_changes = [password]\n" >>$fn printf "}\n" >>$fn fi if [[ ${tt1} == "security_group_id" ]];then skip=1;fi if [[ ${tt1} == "dns_ip_addresses" ]];then tt2=`echo $tt2 | tr -d '"'` skip=1 while [ "$t1" != "]" ] && [ "$tt2" != "[]" ] ;do #while [[ "$t1" != "]" ]] ;do read line t1=`echo "$line"` #echo $t1 done fi if [[ ${tt1} == "availability_zones" ]];then #echo "dns block" tt2=`echo $tt2 | tr -d '"'` skip=1 while [ "$t1" != "]" ] && [ "$tt2" != "[]" ] ;do #while [[ "$t1" != "]" ]] ;do read line t1=`echo "$line"` #echo $t1 done fi #if [[ ${tt1} == "ipv6_association_id" ]];then skip=1;fi #if [[ ${tt1} == "ipv6_cidr_block" ]];then skip=1;fi if [[ ${tt1} == "vpc_id" ]]; then vpcid=`echo $tt2 | tr -d '"'` t1=`printf "%s = aws_vpc.%s.id" $tt1 $vpcid` fi else if [[ ${t1} == *"subnet-"* ]]; then #echo "-1-> $t1" tt2=`echo $t1 | tr -d '"|,'` t1=`printf "aws_subnet.%s.id," $tt2` subnets+=`printf "\"%s\" " $tt2` fi fi if [ "$skip" == "0" ]; then #echo $skip $t1 echo "$t1" >> $fn fi done <"$file" for sub in ${subnets[@]}; do #echo "therole=$therole" sub1=`echo $sub | tr -d '"'` echo "calling for $sub1" if [ "$sub1" != "" ]; then ../../scripts/105-get-subnet.sh $sub1 fi done done fi done rm -f t*.txt