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_required_providers: warn on provider.version #850

Merged
merged 2 commits into from
Jul 21, 2020
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
40 changes: 35 additions & 5 deletions docs/rules/terraform_required_providers.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ rule "terraform_required_providers" {
}
```

## Example
## Examples

```hcl
provider "template" {}
Copy link
Member

Choose a reason for hiding this comment

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

The issue message has been updated, so it would be nice to reflect it in the example output as well.

Expand All @@ -20,9 +20,39 @@ provider "template" {}
$ tflint
1 issue(s) found:

Warning: Provider "template" should have a version constraint in required_providers
Warning: Missing version constraint for provider "template" in "required_providers" (terraform_required_providers)

Reference: https://github.com/terraform-linters/tflint/blob/v0.11.0/docs/rules/terraform_required_providers.md
on main.tf line 1:
1: provider "template" {}

Reference: https://github.com/terraform-linters/tflint/blob/v0.18.0/docs/rules/terraform_required_providers.md
```

<hr>

```hcl
provider "template" {
version = "2"
}
```

```
$ tflint
2 issue(s) found:

Warning: provider.template: version constraint should be specified via "required_providers" (terraform_required_providers)

on main.tf line 1:
1: provider "template" {

Reference: https://github.com/terraform-linters/tflint/blob/v0.18.0/docs/rules/terraform_required_providers.md

Warning: Missing version constraint for provider "template" in "required_providers" (terraform_required_providers)

on main.tf line 1:
1: provider "template" {

Reference: https://github.com/terraform-linters/tflint/blob/v0.18.0/docs/rules/terraform_required_providers.md
```

## Why
Expand All @@ -31,7 +61,7 @@ Providers are plugins released on a separate rhythm from Terraform itself, and s

## How To Fix

Add the `required_providers` attribute to the `terraform` configuration block and include current versions for all providers. For example:
Add the [`required_providers`](https://www.terraform.io/docs/configuration/terraform.html#specifying-required-provider-versions) block to the `terraform` configuration block and include current versions for all providers. For example:

```tf
terraform {
Expand All @@ -41,4 +71,4 @@ terraform {
}
```

Provider version constraints can also be specified using a version argument within a provider block but this is not recommend, particularly for child modules.
Provider version constraints can be specified using a [version argument within a provider block](https://www.terraform.io/docs/configuration/providers.html#provider-versions) for backwards compatability. This approach is now discouraged, particularly for child modules.
25 changes: 19 additions & 6 deletions rules/terraformrules/terraform_required_providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"log"

"github.com/hashicorp/hcl/v2"
"github.com/terraform-linters/tflint/tflint"
)

Expand Down Expand Up @@ -39,14 +40,26 @@ func (r *TerraformRequiredProvidersRule) Link() string {
func (r *TerraformRequiredProvidersRule) Check(runner *tflint.Runner) error {
log.Printf("[TRACE] Check `%s` rule for `%s` runner", r.Name(), runner.TFConfigPath())

providers := make(map[string]hcl.Range)
module := runner.TFConfig.Module

for _, provider := range module.ProviderConfigs {
if _, ok := module.ProviderRequirements[provider.Name]; !ok && provider.Version.Required == nil {
message := fmt.Sprintf(`Provider "%s" should have a version constraint in required_providers`, provider.Name)
if provider.Alias != "" {
message += fmt.Sprintf(" (%s.%s)", provider.Name, provider.Alias)
}
runner.EmitIssue(r, message, provider.DeclRange)
if _, ok := providers[provider.Name]; !ok {
providers[provider.Name] = provider.DeclRange
}

if provider.Version.Required != nil {
runner.EmitIssue(
r,
fmt.Sprintf(`%s: version constraint should be specified via "required_providers"`, provider.Addr().String()),
provider.DeclRange,
)
}
}

for name, decl := range providers {
if _, ok := module.ProviderRequirements[name]; !ok {
runner.EmitIssue(r, fmt.Sprintf(`Missing version constraint for provider "%s" in "required_providers"`, name), decl)
}
}

Expand Down
82 changes: 68 additions & 14 deletions rules/terraformrules/terraform_required_providers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ provider "template" {}
Expected: tflint.Issues{
{
Rule: NewTerraformRequiredProvidersRule(),
Message: `Provider "template" should have a version constraint in required_providers`,
Message: `Missing version constraint for provider "template" in "required_providers"`,
Range: hcl.Range{
Filename: "module.tf",
Start: hcl.Pos{
Expand Down Expand Up @@ -50,28 +50,55 @@ provider "template" {}
Expected: tflint.Issues{},
},
{
Name: "provider alias",
Name: "single provider with alias",
Content: `
provider "template" {
version = "~> 2"
alias = "b"
}
`,
Expected: tflint.Issues{
{
Rule: NewTerraformRequiredProvidersRule(),
Message: `Missing version constraint for provider "template" in "required_providers"`,
Range: hcl.Range{
Filename: "module.tf",
Start: hcl.Pos{
Line: 2,
Column: 1,
},
End: hcl.Pos{
Line: 2,
Column: 20,
},
},
},
},
},
{
Name: "version set with alias",
Content: `
terraform {
required_providers {
template = "~> 2"
}
}

provider "template" {
alias = "b"
}
version = "~> 2"
}
`,
Expected: tflint.Issues{
{
Rule: NewTerraformRequiredProvidersRule(),
Message: `Provider "template" should have a version constraint in required_providers (template.b)`,
Message: `provider.template: version constraint should be specified via "required_providers"`,
Range: hcl.Range{
Filename: "module.tf",
Start: hcl.Pos{
Line: 6,
Line: 8,
Column: 1,
},
End: hcl.Pos{
Line: 6,
Line: 8,
Column: 20,
},
},
Expand All @@ -81,23 +108,50 @@ provider "template" {
{
Name: "version set",
Content: `
terraform {
required_providers {
template = "~> 2"
}
}

provider "template" {
alias = "foo"
version = "~> 2"
}
`,
Expected: tflint.Issues{},
Expected: tflint.Issues{
{
Rule: NewTerraformRequiredProvidersRule(),
Message: `provider.template.foo: version constraint should be specified via "required_providers"`,
Range: hcl.Range{
Filename: "module.tf",
Start: hcl.Pos{
Line: 8,
Column: 1,
},
End: hcl.Pos{
Line: 8,
Column: 20,
},
},
},
},
},
}

rule := NewTerraformRequiredProvidersRule()

for _, tc := range cases {
runner := tflint.TestRunner(t, map[string]string{"module.tf": tc.Content})
tc := tc

t.Run(tc.Name, func(t *testing.T) {
runner := tflint.TestRunner(t, map[string]string{"module.tf": tc.Content})

if err := rule.Check(runner); err != nil {
t.Fatalf("Unexpected error occurred: %s", err)
}
if err := rule.Check(runner); err != nil {
t.Fatalf("Unexpected error occurred: %s", err)
}

tflint.AssertIssues(t, tc.Expected, runner.Issues)
tflint.AssertIssues(t, tc.Expected, runner.Issues)
})
}
}