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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions rules/terraform_unused_declarations.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,17 +138,38 @@ func (r *TerraformUnusedDeclarationsRule) declarations(runner *terraform.Runner)
LabelNames: []string{"type", "name"},
Body: &hclext.BodySchema{},
},
{
Type: "check",
LabelNames: []string{"name"},
Body: &hclext.BodySchema{
Blocks: []hclext.BlockSchema{
{
Type: "data",
LabelNames: []string{"type", "name"},
Body: &hclext.BodySchema{},
},
},
},
},
},
}, &tflint.GetModuleContentOption{ExpandMode: tflint.ExpandModeNone})
if err != nil {
return decl, err
}

for _, block := range body.Blocks {
if block.Type == "variable" {
switch block.Type {
case "variable":
decl.Variables[block.Labels[0]] = block
} else {
case "data":
decl.DataResources[fmt.Sprintf("data.%s.%s", block.Labels[0], block.Labels[1])] = block
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.

}
default:
panic("unreachable")
}
}

Expand Down
23 changes: 23 additions & 0 deletions rules/terraform_unused_declarations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,29 @@ variable "unused" {
},
},
Fixed: `
`,
},
{
Name: "unused scoped data source",
Content: `
check "unused" {
data "null_data_source" "unused" {}
}
`,
Expected: helper.Issues{
{
Rule: NewTerraformUnusedDeclarationsRule(),
Message: `data "null_data_source" "unused" is declared but not used`,
Range: hcl.Range{
Filename: "config.tf",
Start: hcl.Pos{Line: 3, Column: 3},
End: hcl.Pos{Line: 3, Column: 35},
},
},
},
Fixed: `
check "unused" {
}
`,
},
{
Expand Down