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

terraform_unused_declarations: Add support for scoped data sources #135

Merged
merged 1 commit into from
Oct 9, 2023

Conversation

wata727
Copy link
Member

@wata727 wata727 commented Oct 8, 2023

See also https://developer.hashicorp.com/terraform/language/checks

Terraform v1.5 now supports check blocks and scoped (nested) data sources.
This PR adds support for them to the terraform_unused_declarations rule.

case "check":
for _, data := range block.Body.Blocks {
// Scoped data source addresses are unique in the module
decl.DataResources[fmt.Sprintf("data.%s.%s", data.Labels[0], data.Labels[1])] = data
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: This logic is broken if scoped data source addresses are unique in check blocks.

As far as I've looked, this logic is valid because in v1.6.0 it's unique in the module.

data "http" "terraform_io" {
  url = "https://www.terraform.io"
}

check "health_check" {
  data "http" "terraform_io" {
    url = "https://www.terraform.io"
  }

  assert {
    condition = data.http.terraform_io.status_code == 200
    error_message = "${data.http.terraform_io.url} returned an unhealthy status code"
  }
}
@wata727 ➜ /tmp $ terraform validate
╷
│ Error: Duplicate data "http" configuration
│
│   on main.tf line 6, in check "health_check":
│    6:   data "http" "terraform_io" {
│
│ A http data resource named "terraform_io" was already declared at main.tf:1,1-27. Resource names must be unique per type in each module, including within check
│ blocks.

@wata727 wata727 merged commit 231568f into main Oct 9, 2023
2 checks passed
@wata727 wata727 deleted the unused_scoped_data_sources branch October 9, 2023 08:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

1 participant