#!/bin/bash if [ "$1" != "" ]; then cmd[0]="$AWS servicediscovery get-service --id $1" pref[0]="Service" else cmd[0]="$AWS servicediscovery list-services" pref[0]="Services" fi tft[0]="aws_service_discovery_service" idfilt[0]="Id" #rm -f ${tft[0]}.tf 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 if [[ "$1" != "" ]]; then count=1 else count=$(echo $awsout | jq ".${pref[(${c})]} | length") fi echo "$ttft count=$count" if [[ "$count" -gt "0" ]]; then count=$(expr $count - 1) for i in $(seq 0 $count); do if [[ "$1" != "" ]]; then cname=$(echo $awsout | jq ".${pref[(${c})]}.${idfilt[(${c})]}" | tr -d '"') else cname=$(echo $awsout | jq ".${pref[(${c})]}[(${i})].${idfilt[(${c})]}" | tr -d '"') fi echo "cname=$cname" rname=${cname//:/_} && rname=${rname//./_} && rname=${rname//\//_} fn=$(printf "%s__%s.tf" $ttft $rname) if [[ -f "$fn" ]]; then echo "$fn exists already skipping" && continue; fi echo "$ttft $cname Import" printf "resource \"%s\" \"%s\" {" $ttft $rname >$fn printf "}" >>$fn terraform import $ttft.$rname "$cname" | grep Importing terraform state show -no-color $ttft.$rname >t1.txt rm -f $fn file="t1.txt" 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} == "ipv6_cidr_block_association_id" ]]; then skip=1; fi #if [[ ${tt1} == "availability_zone" ]];then skip=1;fi if [[ ${tt1} == "availability_zone_id" ]]; then skip=1; fi if [[ ${tt1} == "state" ]]; then skip=1; fi if [[ ${tt1} == "dns_entry" ]]; then skip=1; fi if [[ ${tt1} == "requester_managed" ]]; then skip=1; fi if [[ ${tt1} == "prefix_list_id" ]]; then skip=1; fi if [[ ${tt1} == "cidr_blocks" ]]; then echo "matched cidr" skip=1 while [[ "$t1" != "]" ]]; do read line t1=$(echo "$line") echo $t1 done fi if [[ ${tt1} == "network_interface_ids" ]]; then skip=1; fi if [[ ${tt1} == "namespace_id" ]]; then nsid=$(echo $tt2 | tr -d '"') t1=$(printf "%s = aws_service_discovery_private_dns_namespace.%s.id" $tt1 $nsid) fi if [[ ${tt1} == "vpc_id" ]]; then tt2=$(echo $tt2 | tr -d '"') t1=$(printf "%s = aws_vpc.%s.id" $tt1 $tt2) fi if [[ ${tt1} == "type" ]]; then tt2=$(echo $tt2 | tr -d '"') if [[ ${tt2} == "DNS_HTTP" ]]; then skip=1 fi fi fi if [ "$skip" == "0" ]; then #echo $skip $t1 echo "$t1" >>$fn fi done <"$file" # get the namespace if [[ "$nsid" != "" ]]; then echo "calling get priv dns ns for $nsid" ../../scripts/get-sd-priv-dns-ns.sh $nsid fi done fi done rm -f t*.txt echo "exit $ttft"