diff --git a/docs/upgrading_to_v2.0.md b/docs/upgrading_to_v2.0.md index ea7bd714c..efa5285ff 100644 --- a/docs/upgrading_to_v2.0.md +++ b/docs/upgrading_to_v2.0.md @@ -93,7 +93,7 @@ can't guarantee that exactly these actions will be performed if ### Manual Migration Steps -In this example here are the two commands used migrate the subnets created by the `simple_project` in the examples directory. _please note the need to escape the quotes on the new resource_. +In this example here are the two commands used migrate the subnets created by the `simple_project` in the examples directory. _please note the need to escape the quotes on the new resource_. You may also use the migration script. - `terraform state mv module.example.module.test-vpc-module.google_compute_subnetwork.subnetwork[0] module.example.module.test-vpc-module.google_compute_subnetwork.subnetwork[\"us-west1/simple-project-timh-subnet-01\"]` @@ -132,7 +132,7 @@ actions need to be performed. 2. Run the script to output the migration commands: ```sh - $ MODULE_NAME="test-vpc-module" ./migrate.sh + $ MODULE_NAME="test-vpc-module" ./migrate.sh --dry-run terraform state mv module.example.module.test-vpc-module.google_compute_subnetwork.subnetwork[0] module.example.module.test-vpc-module.google_compute_subnetwork.subnetwork[\"us-west1/simple-project-timh-subnet-01\"] terraform state mv module.example.module.test-vpc-module.google_compute_subnetwork.subnetwork[1] module.example.module.test-vpc-module.google_compute_subnetwork.subnetwork[\"us-west1/simple-project-timh-subnet-02\"] ``` @@ -140,10 +140,9 @@ actions need to be performed. 3. Execute the migration command ```sh - $ terraform state mv module.example.module.test-vpc-module.google_compute_subnetwork.subnetwork[0] module.example.module.test-vpc-module.google_compute_subnetwork.subnetwork[\"us-west1/simple-project-timh-subnet-01\"] + $ MODULE_NAME="test-vpc-module" ./migrate.sh Move "module.example.module.test-vpc-module.google_compute_subnetwork.subnetwork[0]" to "module.example.module.test-vpc-module.google_compute_subnetwork.subnetwork[\"us-west1/simple-project-timh-subnet-01\"]" Successfully moved 1 object(s). - $ terraform state mv module.example.module.test-vpc-module.google_compute_subnetwork.subnetwork[1] module.example.module.test-vpc-module.google_compute_subnetwork.subnetwork[\"us-west1/simple-project-timh-subnet-02\"] Move "module.example.module.test-vpc-module.google_compute_subnetwork.subnetwork[1]" to "module.example.module.test-vpc-module.google_compute_subnetwork.subnetwork[\"us-west1/simple-project-timh-subnet-02\"]" Successfully moved 1 object(s). ``` diff --git a/scripts/migrate.sh b/scripts/migrate.sh index a36e2d8c2..ac4fb4345 100755 --- a/scripts/migrate.sh +++ b/scripts/migrate.sh @@ -16,12 +16,46 @@ # Output Terraform Commands to migrate to new subnet config set -e set -o pipefail +CMD="terraform state" + +while (( "$#" )); do + # shellcheck disable=SC2221,SC2222 + case "$1" in + -d|--dry-run) + DRY_RUN=true + shift 1 + ;; + --) # end argument parsing + shift + break + ;; + -*|--*=) # unsupported flags + echo "Error: Unsupported flag $1" >&2 + exit 1 + ;; + *) # preserve positional arguments + PARAMS="$PARAMS $1" + shift + ;; + esac +done + +eval set -- "$PARAMS" + +if [ ! -e "$(command -v terraform)" ]; then + echo "can not find terraform" + exit 1 +fi if [[ "$MODULE_NAME" ]]; then - NAME=$(terraform state list | grep "${MODULE_NAME}".google_compute_network.network | sed 's/.google_compute_network.network//') - for x in $(terraform state list | grep "${NAME}".google_compute_subnetwork.subnetwork); do - ID=$(terraform state show "$x" | grep id | grep -v ip_cidr_range | awk '{ print $3 }'| tr -d '"') - echo "terraform state mv $x ${NAME}.google_compute_subnetwork.subnetwork[\\\"${ID}\\\"]" + NAME=$(${CMD} list | grep "${MODULE_NAME}".google_compute_network.network | sed 's/.google_compute_network.network//') + for x in $($CMD list | grep "${NAME}".google_compute_subnetwork.subnetwork); do + ID=$(${CMD} show "$x" | grep id | grep -v ip_cidr_range | awk '{ print $3 }'| tr -d '"') + if [[ $DRY_RUN ]]; then + echo "${CMD} mv $x ${NAME}.google_compute_subnetwork.subnetwork[\\\"${ID}\\\"]" + else + ${CMD} mv "$x" "${NAME}".google_compute_subnetwork.subnetwork[\""${ID}"\"] + fi done else echo "MISSING MODULE_NAME: MODULE_NAME env var is required"