apparentlymart / terraform-clean-syntax

A simple tool for Terraform language syntax cleanup, extending terraform fmt
Mozilla Public License 2.0
160 stars 24 forks source link

feat: add support for provider meta-arguments #14

Closed jasonwalsh closed 4 years ago

jasonwalsh commented 4 years ago

This pull request adds support for removing quoted references for provider meta-arguments.

For example, given the following Terraform configuration:

provider "aws" {
  alias = "prod"

  region = "us-east-1"
}

variable "owners" {
  default = ["self"]
  type    = "list"
}

provider "null" {
  alias = "foo"
}

resource "null_resource" "cat" {
  provider = "null.foo"
}

data "aws_ami" "example" {
  provider = "aws.prod"

  owners = "${var.owners}"
}

Invoking terraform-clean-syntax rewrites the file like so:

provider "aws" {
  alias = "prod"

  region = "us-east-1"
}

variable "owners" {
  default = ["self"]
  type    = list(string)
}

provider "null" {
  alias = "foo"
}

resource "null_resource" "cat" {
  provider = null.foo
}

data "aws_ami" "example" {
  provider = aws.prod

  owners = var.owners
}

Where quotes are removed from the provider = null.foo and provider = aws.prod meta-arguments.

Fixes #13

jasonwalsh commented 4 years ago

Thanks for reviewing @apparentlymart!

I authored a new commit (e2f307e) which includes the documentation from https://github.com/apparentlymart/terraform-clean-syntax/pull/14#discussion_r459113824.

apparentlymart commented 4 years ago

Hi @jasonwalsh,

I'm sorry for the delay in following up with you on this PR. I got distracted by some other work elsewhere. :confounded:

I'm going to merge this now. Thanks!

I'm actually about to archive this repository because I upstreamed the functionality from it into Terraform itself for the Terraform 0.14 release (see hashicorp/terraform#26390. Because I did that upstreaming before I merged your PR here, what I upstreamed unfortunately didn't include this new capability.

If you were interested in submitting it upstream I think it shouldn't require too much further work since what I submitted upstream was structured very similarly to terraform-clean-syntax... I think the main additional thing would be that upstream Terraform requires unit test coverage, whereas I didn't include any automated tests in terraform-clean-syntax. However, I think a unit test for the change you proposed here would just entail a couple new files in Terraform's command/testdata/fmt directory to reflect the before and after cases that this rule is aiming to implement.

Thanks again!