Skip to content

Commit

Permalink
wip: updated migration script to run migration commands
Browse files Browse the repository at this point in the history
  • Loading branch information
tfhartmann committed Oct 9, 2019
1 parent 767a17f commit 86d2dad
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 8 deletions.
7 changes: 3 additions & 4 deletions docs/upgrading_to_v2.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -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\"]`
Expand Down Expand Up @@ -132,18 +132,17 @@ 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\"]
```
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).
```
Expand Down
42 changes: 38 additions & 4 deletions scripts/migrate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 86d2dad

Please sign in to comment.