Skip to content

Commit

Permalink
unused_required_providers: provider functions (#214)
Browse files Browse the repository at this point in the history
  • Loading branch information
bgpat authored Sep 27, 2024
1 parent 9dcbac6 commit 34423f6
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 2 deletions.
24 changes: 24 additions & 0 deletions rules/terraform_required_providers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,30 @@ resource "google_compute_instance" "foo" {
},
},
},
{
Name: "provider-defined function",
Content: `
output "foo" {
value = provider::time::rfc3339_parse("2023-07-25T23:43:16Z")
}`,
Expected: helper.Issues{
{
Rule: NewTerraformRequiredProvidersRule(),
Message: "Missing version constraint for provider \"time\" in `required_providers`",
Range: hcl.Range{
Filename: "module.tf",
Start: hcl.Pos{
Line: 3,
Column: 10,
},
End: hcl.Pos{
Line: 3,
Column: 63,
},
},
},
},
},
}

rule := NewTerraformRequiredProvidersRule()
Expand Down
16 changes: 16 additions & 0 deletions rules/terraform_unused_required_providers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,22 @@ func Test_TerraformUnusedRequiredProvidersRule(t *testing.T) {
`,
Expected: helper.Issues{},
},
{
Name: "used - provider-defined function",
Content: `
terraform {
required_providers {
time = {
source = "hashicorp/time"
}
}
}
output "foo" {
value = provider::time::rfc3339_parse("2023-07-25T23:43:16Z")
}
`,
Expected: helper.Issues{},
},
}

rule := NewTerraformUnusedRequiredProvidersRule()
Expand Down
23 changes: 21 additions & 2 deletions terraform/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"strings"

"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclsyntax"
"github.com/terraform-linters/tflint-plugin-sdk/hclext"
"github.com/terraform-linters/tflint-plugin-sdk/tflint"
)
Expand Down Expand Up @@ -104,7 +105,7 @@ func (r *Runner) GetLocals() (map[string]*Local, hcl.Diagnostics) {
return locals, diags
}

// GetProviderRefs returns all references to providers in resources, data, provider declarations, and module calls.
// GetProviderRefs returns all references to providers in resources, data, provider declarations, module calls, and provider-defined functinos.
func (r *Runner) GetProviderRefs() (map[string]*ProviderRef, hcl.Diagnostics) {
providerRefs := map[string]*ProviderRef{}

Expand Down Expand Up @@ -241,5 +242,23 @@ func (r *Runner) GetProviderRefs() (map[string]*ProviderRef, hcl.Diagnostics) {
}
}

return providerRefs, nil
walkDiags := r.WalkExpressions(tflint.ExprWalkFunc(func(expr hcl.Expression) hcl.Diagnostics {
if fce, ok := expr.(*hclsyntax.FunctionCallExpr); ok {
parts := strings.Split(fce.Name, "::")
if len(parts) < 2 || parts[0] != "provider" || parts[1] == "" {
return nil
}
providerRefs[parts[1]] = &ProviderRef{
Name: parts[1],
DefRange: expr.Range(),
}
}
return nil
}))
diags = diags.Extend(walkDiags)
if walkDiags.HasErrors() {
return providerRefs, diags
}

return providerRefs, diags
}
10 changes: 10 additions & 0 deletions terraform/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,16 @@ check "my_check" {
"aws": {Name: "aws", DefRange: hcl.Range{Filename: "main.tf", Start: hcl.Pos{Line: 3, Column: 3}, End: hcl.Pos{Line: 3, Column: 24}}},
},
},
{
name: "provider-defined function",
content: `
output "foo" {
value = provider::time::rfc3339_parse("2023-07-25T23:43:16Z")
}`,
want: map[string]*ProviderRef{
"time": {Name: "time", DefRange: hcl.Range{Filename: "main.tf", Start: hcl.Pos{Line: 3, Column: 11}, End: hcl.Pos{Line: 3, Column: 64}}},
},
},
}

for _, test := range tests {
Expand Down

0 comments on commit 34423f6

Please sign in to comment.