minamijoyo / tfupdate

Update version constraints in your Terraform configurations
MIT License
542 stars 23 forks source link

Support a new provider source syntax in Terraform v0.13 #21

Closed minamijoyo closed 4 years ago

minamijoyo commented 4 years ago

Fixes #14

Terraform v0.13 will introduce a provider source to support downloading community providers from Terraform Registry. https://www.hashicorp.com/blog/adding-provider-source-to-hashicorp-terraform/

It extends a required_providers block and now we can set provider's source and version as an object type. So we check a type of value and switch implementations. (Strictly speaking, a new required_providers object syntax was introduced in 0.12.20.)

Implementation Notes: Updating the whole object loses original sort order and comments. At the time of writing, there is no way to update a value inside an object directly while preserving original tokens. Since we fully understand the valid syntax, we compromise and read the tokens in order, updating the bytes directly. It's apparently a fragile dirty hack, but I didn't come up with the better way to do this.

minamijoyo commented 4 years ago

FYI: I wrote it in tests, I'll put a complicated case which preserves the original sort order and comments.

[tfupdate@provider-source|✔]$ cat tmp/source.tf
terraform {
  required_providers {
    # foo
    aws = {
      # version = "2.65.0" # bar
      version = "2.65.0" # baz
      source  = "hashicorp/aws"
    }
  }
}

[tfupdate@provider-source|✔]$ bin/tfupdate provider aws -v "2.66.0" tmp/source.tf

[tfupdate@provider-source|✔]$ echo $?
0

[tfupdate@provider-source|✔]$ cat tmp/source.tf
terraform {
  required_providers {
    # foo
    aws = {
      # version = "2.65.0" # bar
      version = "2.66.0" # baz
      source  = "hashicorp/aws"
    }
  }
}