Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse a state action string like a shell #6

Merged
merged 1 commit into from
Nov 5, 2020
Merged

Conversation

minamijoyo
Copy link
Owner

Fixes #5
We cannot simply split it by space because the address of resource can contain spaces.
We should parse it like a shell with go-shellwords.

Fixes #5
We cannot simply split it by space because the address of resource can contain spaces.
We should parse it like a shell with go-shellwords.
@minamijoyo
Copy link
Owner Author

Given the following configuration:

terraform {
  required_version = "0.13.5"

  required_providers {
    docker = {
      source  = "terraform-providers/docker"
      version = "2.7.2"
    }
  }
}

resource "docker_image" "nginx" {
  name         = "nginx:latest"
  keep_locally = false
}

# resource "docker_container" "nginx" {
#   image = docker_image.nginx.latest
#   name  = "example"
# }

resource "docker_container" "nginx" {
  for_each = {
    "This is an example" = "example"
  }

  image = docker_image.nginx.latest
  name  = each.value
}

A migration file is the following. Note that we need to escape double-quote ( \" ). It's a HCL limitation.

migration "state" "test" {
  dir = "tmp/test5"
  actions = [
    "mv docker_container.nginx 'docker_container.nginx[\"This is an example\"]'",
  ]
}

Run the tfmigrate plan command:

[tfmigrate@fix-iss-5|✔]$ TFMIGRATE_LOG=DEBUG bin/tfmigrate plan tmp/test5/state_mv.hcl
2020/11/05 22:11:51 [DEBUG] [main] start: bin/tfmigrate plan tmp/test5/state_mv.hcl
2020/11/05 22:11:51 [DEBUG] [main] tfmigrate version: 0.1.0
2020/11/05 22:11:51 [DEBUG] [command] option: &tfmigrate.MigratorOption{ExecPath:""}
2020/11/05 22:11:51 [INFO] [command] read migration file: tmp/test5/state_mv.hcl
2020/11/05 22:11:51 [DEBUG] [command] parse migration file: "migration \"state\" \"test\" {\n  dir = \"tmp/test5\"\n  actions = [\n    \"mv docker_container.nginx 'docker_container.nginx[\"This is an example\"]'\",\n  ]\n}\n"
failed to decode migration file: tmp/test5/state_mv.hcl, err: tmp/test5/state_mv.hcl:4,57-61: Missing item separator; Expected a comma to mark the beginning of the next item.
[tfmigrate@fix-iss-5|✔]$ TFMIGRATE_LOG=DEBUG bin/tfmigrate plan tmp/test5/state_mv.hcl
2020/11/05 22:12:13 [DEBUG] [main] start: bin/tfmigrate plan tmp/test5/state_mv.hcl
2020/11/05 22:12:13 [DEBUG] [main] tfmigrate version: 0.1.0
2020/11/05 22:12:13 [DEBUG] [command] option: &tfmigrate.MigratorOption{ExecPath:""}
2020/11/05 22:12:13 [INFO] [command] read migration file: tmp/test5/state_mv.hcl
2020/11/05 22:12:13 [DEBUG] [command] parse migration file: "migration \"state\" \"test\" {\n  dir = \"tmp/test5\"\n  actions = [\n    \"mv docker_container.nginx 'docker_container.nginx[\\\"This is an example\\\"]'\",\n  ]\n}\n"
2020/11/05 22:12:13 [INFO] [command] new migrator: &config.StateMigratorConfig{Dir:"tmp/test5", Actions:[]string{"mv docker_container.nginx 'docker_container.nginx[\"This is an example\"]'"}}
2020/11/05 22:12:13 [INFO] [migrator] start state migrator plan
2020/11/05 22:12:14 [DEBUG] [executor@tmp/test5]$ terraform version
2020/11/05 22:12:14 [INFO] [migrator@tmp/test5] terraform version: 0.13.5
2020/11/05 22:12:14 [INFO] [migrator@tmp/test5] initialize work dir
2020/11/05 22:12:15 [DEBUG] [executor@tmp/test5]$ terraform init -input=false -no-color
2020/11/05 22:12:15 [INFO] [migrator@tmp/test5] get the current remote state
2020/11/05 22:12:16 [DEBUG] [executor@tmp/test5]$ terraform state pull
2020/11/05 22:12:16 [INFO] [migrator@tmp/test5] override backend to local
2020/11/05 22:12:16 [INFO] [executor@tmp/test5] create an override file
2020/11/05 22:12:16 [INFO] [executor@tmp/test5] switch backend to local
2020/11/05 22:12:17 [DEBUG] [executor@tmp/test5]$ terraform init -input=false -no-color -reconfigure
2020/11/05 22:12:17 [INFO] [migrator@tmp/test5] compute a new state
2020/11/05 22:12:17 [DEBUG] [executor@tmp/test5]$ terraform state mv -state=/var/folders/zh/37j0y_pn2pzc295c19rpw2tw0000gp/T/tmp756235542 -backup=/dev/null docker_container.nginx docker_container.nginx["This is an example"]
2020/11/05 22:12:17 [INFO] [migrator@tmp/test5] check diffs
2020/11/05 22:12:19 [DEBUG] [executor@tmp/test5]$ terraform plan -state=/var/folders/zh/37j0y_pn2pzc295c19rpw2tw0000gp/T/tmp877907325 -out=/var/folders/zh/37j0y_pn2pzc295c19rpw2tw0000gp/T/tfplan340595896 -input=false -no-color -detailed-exitcode
2020/11/05 22:12:19 [INFO] [executor@tmp/test5] remove the override file
2020/11/05 22:12:19 [INFO] [executor@tmp/test5] switch back to remote
2020/11/05 22:12:19 [DEBUG] [executor@tmp/test5]$ terraform init -input=false -no-color -reconfigure
2020/11/05 22:12:19 [INFO] [migrator] state migrator plan success!

Looks good to me.

@minamijoyo minamijoyo merged commit 22a5a00 into master Nov 5, 2020
@minamijoyo minamijoyo deleted the fix-iss-5 branch November 5, 2020 13:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow migrations with resources including a space
1 participant