Skip to content

Commit 34423f6

Browse files
authored
unused_required_providers: provider functions (#214)
1 parent 9dcbac6 commit 34423f6

File tree

4 files changed

+71
-2
lines changed

4 files changed

+71
-2
lines changed

rules/terraform_required_providers_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,30 @@ resource "google_compute_instance" "foo" {
546546
},
547547
},
548548
},
549+
{
550+
Name: "provider-defined function",
551+
Content: `
552+
output "foo" {
553+
value = provider::time::rfc3339_parse("2023-07-25T23:43:16Z")
554+
}`,
555+
Expected: helper.Issues{
556+
{
557+
Rule: NewTerraformRequiredProvidersRule(),
558+
Message: "Missing version constraint for provider \"time\" in `required_providers`",
559+
Range: hcl.Range{
560+
Filename: "module.tf",
561+
Start: hcl.Pos{
562+
Line: 3,
563+
Column: 10,
564+
},
565+
End: hcl.Pos{
566+
Line: 3,
567+
Column: 63,
568+
},
569+
},
570+
},
571+
},
572+
},
549573
}
550574

551575
rule := NewTerraformRequiredProvidersRule()

rules/terraform_unused_required_providers_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,22 @@ func Test_TerraformUnusedRequiredProvidersRule(t *testing.T) {
244244
`,
245245
Expected: helper.Issues{},
246246
},
247+
{
248+
Name: "used - provider-defined function",
249+
Content: `
250+
terraform {
251+
required_providers {
252+
time = {
253+
source = "hashicorp/time"
254+
}
255+
}
256+
}
257+
output "foo" {
258+
value = provider::time::rfc3339_parse("2023-07-25T23:43:16Z")
259+
}
260+
`,
261+
Expected: helper.Issues{},
262+
},
247263
}
248264

249265
rule := NewTerraformUnusedRequiredProvidersRule()

terraform/runner.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"strings"
55

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

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

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

244-
return providerRefs, nil
245+
walkDiags := r.WalkExpressions(tflint.ExprWalkFunc(func(expr hcl.Expression) hcl.Diagnostics {
246+
if fce, ok := expr.(*hclsyntax.FunctionCallExpr); ok {
247+
parts := strings.Split(fce.Name, "::")
248+
if len(parts) < 2 || parts[0] != "provider" || parts[1] == "" {
249+
return nil
250+
}
251+
providerRefs[parts[1]] = &ProviderRef{
252+
Name: parts[1],
253+
DefRange: expr.Range(),
254+
}
255+
}
256+
return nil
257+
}))
258+
diags = diags.Extend(walkDiags)
259+
if walkDiags.HasErrors() {
260+
return providerRefs, diags
261+
}
262+
263+
return providerRefs, diags
245264
}

terraform/runner_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,16 @@ check "my_check" {
260260
"aws": {Name: "aws", DefRange: hcl.Range{Filename: "main.tf", Start: hcl.Pos{Line: 3, Column: 3}, End: hcl.Pos{Line: 3, Column: 24}}},
261261
},
262262
},
263+
{
264+
name: "provider-defined function",
265+
content: `
266+
output "foo" {
267+
value = provider::time::rfc3339_parse("2023-07-25T23:43:16Z")
268+
}`,
269+
want: map[string]*ProviderRef{
270+
"time": {Name: "time", DefRange: hcl.Range{Filename: "main.tf", Start: hcl.Pos{Line: 3, Column: 11}, End: hcl.Pos{Line: 3, Column: 64}}},
271+
},
272+
},
263273
}
264274

265275
for _, test := range tests {

0 commit comments

Comments
 (0)