Skip to content

Commit

Permalink
add resource name option, look only for aws_s3 resources
Browse files Browse the repository at this point in the history
  • Loading branch information
badra001 committed Jul 18, 2022
1 parent da551ff commit ded0a71
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions bin/upgrade-s3-provider.sh
Original file line number Diff line number Diff line change
@@ -1,36 +1,45 @@
#!/bin/bash

VERSION="1.0.0"
VERSION="1.0.2"
THIS=$(basename $0 .sh)
STATUS=0
MODULE=$1
RNAME=$2
if [ -z $MODULE ]
then
echo "* missing module, expecting 'module.s3_name'"
exit 1
fi
if [ -z $RNAME ]
then
RNAME="this"
fi

LOGDIR="logs"
test -d $LOGDIR || mkdir -p $LOGDIR
YMDSTAMP=$(date +%Y%m%d)
start=$(date +%s)
STAMP="$YMDSTAMP.$start"
LOGFILE="$LOGDIR/$THIS.$STAMP.log"
if [ -z $TFCOMMAND ]
then
TFCOMMAND=terraform
fi

FILE=$(mktemp -t tfplan.XXXXX)
echo "* getting tf-plan for $MODULE to $FILE (logfile $LOGFILE)"
terraform plan -no-color -target=$MODULE > $FILE
echo "* getting tf-plan for $MODULE resource_name $RNAME to $FILE (logfile $LOGFILE)"
$TFCOMMAND plan -no-color -target=$MODULE > $FILE

echo "* checking that a bucket exists in $MODULE"
EXISTS=$(grep -c ^$MODULE.aws_s3_bucket.this: $FILE)
EXISTS=$(grep -c ^$MODULE.aws_s3_bucket.$RNAME: $FILE)
if [ $EXISTS == 0 ]
then
echo "* no S3 bucket at module $MODULE aws_s3_bucket.this"
echo "* no S3 bucket at module $MODULE aws_s3_bucket.$RNAME"
exit 1
fi

echo "* getting bucket ID from $MODULE"
BUCKETID=$(terraform state show -no-color $MODULE.aws_s3_bucket.this|grep -E 'id.* *='|awk '{print $1,$3}' |grep ^id|awk '{print $2}'|sed -e 's/"//g')
BUCKETID=$($TFCOMMAND state show -no-color $MODULE.aws_s3_bucket.$RNAME|grep -E 'id.* *='|awk '{print $1,$3}' |grep ^id|awk '{print $2}'|sed -e 's/"//g')
if [ -z $BUCKETID ]
then
echo "* cannot determine bucket id for $MODULE"
Expand All @@ -41,10 +50,10 @@ fi

COUNT=0
echo "* importing resources to be created"
for resource in $(grep ' created' $FILE | awk '{print $2}')
for resource in $(grep ' created' $FILE | awk '{print $2}' | grep aws_s3)
do
echo ". resource: tf-import $resource $BUCKETID"
terraform import -no-color $resource $BUCKETID > $LOGFILE
$TFCOMMAND import -no-color $resource $BUCKETID > $LOGFILE
if [ $? != 0 ]
then
echo "* error importing resource $resource"
Expand Down

0 comments on commit ded0a71

Please sign in to comment.