Skip to content

Introduce "preset" config #3

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

Merged
merged 1 commit into from
Aug 20, 2022
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ plugin "terraform" {
}
```

For more configuration about the plugin, see [Plugin Configuration](docs/configuration.md).

## Rules

See [Rules](docs/rules/README.md).
Expand Down
21 changes: 21 additions & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Configuration

This plugin can take advantage of additional features by configuring the plugin block. Currently, this configuration is only available for preset.

Here's an example:

```hcl
plugin "terraform" {
// Plugin common attributes

preset = "recommended"
}
```

## `preset`

Default: `all` (`recommended` for the bundled plugin)

Enable multiple rules at once. Please see [Rules](rules/README.md) for details. Possible values are `recommended` and `all`.

When using the bundled plugin built into TFLint, you can use this plugin without declaring a "plugin" block. In this case the default is `recommeneded`.
18 changes: 9 additions & 9 deletions docs/rules/README.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
# Rules

Terraform language rules implement recommendations from the [Terraform language documentation](https://www.terraform.io/language). If you want to enforce additional usage and style conventions in your configuration, you can author your own ruleset plugin.
Terraform Language rules implement recommendations from the [Terraform Language documentation](https://www.terraform.io/language).

Below is a list of available rules.
All rules are enabled by default, but by setting `preset = "recommended"`, you can enable only the rules marked "Recommended" among the following rules. See [Configuration](../configuration.md) for details.

|Rule|Description|Enabled|
|Rule|Description|Recommended|
| --- | --- | --- |
|[terraform_comment_syntax](terraform_comment_syntax.md)|Disallow `//` comments in favor of `#`||
|[terraform_deprecated_index](terraform_deprecated_index.md)|Disallow legacy dot index syntax||
|[terraform_deprecated_index](terraform_deprecated_index.md)|Disallow legacy dot index syntax||
|[terraform_deprecated_interpolation](terraform_deprecated_interpolation.md)|Disallow deprecated (0.11-style) interpolation|✔|
|[terraform_documented_outputs](terraform_documented_outputs.md)|Disallow `output` declarations without description||
|[terraform_documented_variables](terraform_documented_variables.md)|Disallow `variable` declarations without description||
|[terraform_empty_list_equality](terraform_empty_list_equality.md)|Disallow comparisons with `[]` when checking if a collection is empty||
|[terraform_empty_list_equality](terraform_empty_list_equality.md)|Disallow comparisons with `[]` when checking if a collection is empty||
|[terraform_module_pinned_source](terraform_module_pinned_source.md)|Disallow specifying a git or mercurial repository as a module source without pinning to a version|✔|
|[terraform_module_version](terraform_module_version.md)|Checks that Terraform modules sourced from a registry specify a version|✔|
|[terraform_naming_convention](terraform_naming_convention.md)|Enforces naming conventions for resources, data sources, etc||
|[terraform_required_providers](terraform_required_providers.md)|Require that all providers have version constraints through required_providers||
|[terraform_required_version](terraform_required_version.md)|Disallow `terraform` declarations without require_version||
|[terraform_required_providers](terraform_required_providers.md)|Require that all providers have version constraints through required_providers||
|[terraform_required_version](terraform_required_version.md)|Disallow `terraform` declarations without require_version||
|[terraform_standard_module_structure](terraform_standard_module_structure.md)|Ensure that a module complies with the Terraform Standard Module Structure||
|[terraform_typed_variables](terraform_typed_variables.md)|Disallow `variable` declarations without type||
|[terraform_unused_declarations](terraform_unused_declarations.md)|Disallow variables, data sources, and locals that are declared but never used||
|[terraform_typed_variables](terraform_typed_variables.md)|Disallow `variable` declarations without type||
|[terraform_unused_declarations](terraform_unused_declarations.md)|Disallow variables, data sources, and locals that are declared but never used||
|[terraform_unused_required_providers](terraform_unused_required_providers.md)|Check that all `required_providers` are used in the module||
|[terraform_workspace_remote](terraform_workspace_remote.md)|`terraform.workspace` should not be used with a "remote" backend with remote execution|✔|
2 changes: 2 additions & 0 deletions docs/rules/terraform_deprecated_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

Disallow legacy dot index syntax.

> This rule is enabled by "recommended" preset.

## Example

```hcl
Expand Down
2 changes: 2 additions & 0 deletions docs/rules/terraform_deprecated_interpolation.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

Disallow deprecated (0.11-style) interpolation

> This rule is enabled by "recommended" preset.

## Example

```hcl
Expand Down
2 changes: 2 additions & 0 deletions docs/rules/terraform_empty_list_equality.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

Disallow comparisons with `[]` when checking if a collection is empty.

> This rule is enabled by "recommended" preset.

## Example

```hcl
Expand Down
2 changes: 2 additions & 0 deletions docs/rules/terraform_module_pinned_source.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

Disallow specifying a git or mercurial repository as a module source without pinning to a version.

> This rule is enabled by "recommended" preset.

## Configuration

Name | Default | Value
Expand Down
2 changes: 2 additions & 0 deletions docs/rules/terraform_module_version.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

Ensure that all modules sourced from a [Terraform Registry](https://www.terraform.io/docs/language/modules/sources.html#terraform-registry) specify a `version`.

> This rule is enabled by "recommended" preset.

## Configuration

Name | Description | Default | Type
Expand Down
2 changes: 2 additions & 0 deletions docs/rules/terraform_required_providers.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

Require that all providers have version constraints through `required_providers`.

> This rule is enabled by "recommended" preset.

## Configuration

```hcl
Expand Down
2 changes: 2 additions & 0 deletions docs/rules/terraform_required_version.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

Disallow `terraform` declarations without `required_version`.

> This rule is enabled by "recommended" preset.

## Configuration

```hcl
Expand Down
2 changes: 2 additions & 0 deletions docs/rules/terraform_typed_variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

Disallow `variable` declarations without type.

> This rule is enabled by "recommended" preset.

## Example

```hcl
Expand Down
2 changes: 2 additions & 0 deletions docs/rules/terraform_unused_declarations.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

Disallow variables, data sources, and locals that are declared but never used.

> This rule is enabled by "recommended" preset.

## Example

```hcl
Expand Down
2 changes: 2 additions & 0 deletions docs/rules/terraform_workspace_remote.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ rule "terraform_workspace_remote" {
}
```

> This rule is enabled by "recommended" preset.

## Example

```hcl
Expand Down
57 changes: 37 additions & 20 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,47 @@ import (
"github.com/terraform-linters/tflint-plugin-sdk/plugin"
"github.com/terraform-linters/tflint-plugin-sdk/tflint"
"github.com/terraform-linters/tflint-ruleset-terraform/rules"
"github.com/terraform-linters/tflint-ruleset-terraform/terraform"
)

func main() {
plugin.Serve(&plugin.ServeOpts{
RuleSet: &tflint.BuiltinRuleSet{
Name: "terraform",
Version: "0.1.0",
Rules: []tflint.Rule{
rules.NewTerraformCommentSyntaxRule(),
rules.NewTerraformDeprecatedIndexRule(),
rules.NewTerraformDeprecatedInterpolationRule(),
rules.NewTerraformDocumentedOutputsRule(),
rules.NewTerraformDocumentedVariablesRule(),
rules.NewTerraformEmptyListEqualityRule(),
rules.NewTerraformModulePinnedSourceRule(),
rules.NewTerraformModuleVersionRule(),
rules.NewTerraformNamingConventionRule(),
rules.NewTerraformRequiredProvidersRule(),
rules.NewTerraformRequiredVersionRule(),
rules.NewTerraformStandardModuleStructureRule(),
rules.NewTerraformTypedVariablesRule(),
rules.NewTerraformUnusedDeclarationsRule(),
rules.NewTerraformUnusedRequiredProvidersRule(),
rules.NewTerraformWorkspaceRemoteRule(),
RuleSet: &terraform.RuleSet{
BuiltinRuleSet: tflint.BuiltinRuleSet{
Name: "terraform",
Version: "0.1.0",
},
PresetRules: map[string][]tflint.Rule{
"all": {
rules.NewTerraformCommentSyntaxRule(),
rules.NewTerraformDeprecatedIndexRule(),
rules.NewTerraformDeprecatedInterpolationRule(),
rules.NewTerraformDocumentedOutputsRule(),
rules.NewTerraformDocumentedVariablesRule(),
rules.NewTerraformEmptyListEqualityRule(),
rules.NewTerraformModulePinnedSourceRule(),
rules.NewTerraformModuleVersionRule(),
rules.NewTerraformNamingConventionRule(),
rules.NewTerraformRequiredProvidersRule(),
rules.NewTerraformRequiredVersionRule(),
rules.NewTerraformStandardModuleStructureRule(),
rules.NewTerraformTypedVariablesRule(),
rules.NewTerraformUnusedDeclarationsRule(),
rules.NewTerraformUnusedRequiredProvidersRule(),
rules.NewTerraformWorkspaceRemoteRule(),
},
"recommended": {
rules.NewTerraformDeprecatedIndexRule(),
rules.NewTerraformDeprecatedInterpolationRule(),
rules.NewTerraformEmptyListEqualityRule(),
rules.NewTerraformModulePinnedSourceRule(),
rules.NewTerraformModuleVersionRule(),
rules.NewTerraformRequiredProvidersRule(),
rules.NewTerraformRequiredVersionRule(),
rules.NewTerraformTypedVariablesRule(),
rules.NewTerraformUnusedDeclarationsRule(),
rules.NewTerraformWorkspaceRemoteRule(),
Copy link
Member Author

Choose a reason for hiding this comment

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

Unlike the current settings, the following are enabled by default:

  • terraform_deprecated_index
  • terraform_required_providers
  • terraform_required_version
  • terraform_typed_variables
  • terraform_unused_declarations
  • terraform_unused_required_providers

These are the rules that I consider to be recommended even for senior engineers by my standards.

However, there are issues with https://github.com/terraform-linters/tflint/issues/1328 and https://github.com/terraform-linters/tflint/blob/v0.39.3/docs/rules/terraform_unused_declarations.md#how-to-fix, and the checks aren't perfect, so I'm not sure if these should be the default.

@bendrucker What do you think? If you have any other thoughts on the default rules, please let me know.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah I'd say terraform_unused_required_providers needs to have the issue of implicit provider passing into modules addressed before it comes on by default. The others seem like good defaults for a "recommended" config.

Copy link
Member Author

Choose a reason for hiding this comment

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

OK, we will not include terraform_unused_required_providers in the default for now.

},
},
},
})
Expand Down
2 changes: 1 addition & 1 deletion rules/terraform_comment_syntax.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (r *TerraformCommentSyntaxRule) Name() string {

// Enabled returns whether the rule is enabled by default
func (r *TerraformCommentSyntaxRule) Enabled() bool {
return false
return true
}

// Severity returns the rule severity
Expand Down
2 changes: 1 addition & 1 deletion rules/terraform_deprecated_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (r *TerraformDeprecatedIndexRule) Name() string {

// Enabled returns whether the rule is enabled by default
func (r *TerraformDeprecatedIndexRule) Enabled() bool {
return false
return true
}

// Severity returns the rule severity
Expand Down
2 changes: 1 addition & 1 deletion rules/terraform_documented_outputs.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (r *TerraformDocumentedOutputsRule) Name() string {

// Enabled returns whether the rule is enabled by default
func (r *TerraformDocumentedOutputsRule) Enabled() bool {
return false
return true
}

// Severity returns the rule severity
Expand Down
2 changes: 1 addition & 1 deletion rules/terraform_documented_variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (r *TerraformDocumentedVariablesRule) Name() string {

// Enabled returns whether the rule is enabled by default
func (r *TerraformDocumentedVariablesRule) Enabled() bool {
return false
return true
}

// Severity returns the rule severity
Expand Down
2 changes: 1 addition & 1 deletion rules/terraform_naming_convention.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (r *TerraformNamingConventionRule) Name() string {

// Enabled returns whether the rule is enabled by default
func (r *TerraformNamingConventionRule) Enabled() bool {
return false
return true
}

// Severity returns the rule severity
Expand Down
2 changes: 1 addition & 1 deletion rules/terraform_required_providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (r *TerraformRequiredProvidersRule) Name() string {

// Enabled returns whether the rule is enabled by default
func (r *TerraformRequiredProvidersRule) Enabled() bool {
return false
return true
}

// Severity returns the rule severity
Expand Down
2 changes: 1 addition & 1 deletion rules/terraform_required_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (r *TerraformRequiredVersionRule) Name() string {

// Enabled returns whether the rule is enabled by default
func (r *TerraformRequiredVersionRule) Enabled() bool {
return false
return true
}

// Severity returns the rule severity
Expand Down
2 changes: 1 addition & 1 deletion rules/terraform_standard_module_structure.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (r *TerraformStandardModuleStructureRule) Name() string {

// Enabled returns whether the rule is enabled by default
func (r *TerraformStandardModuleStructureRule) Enabled() bool {
return false
return true
}

// Severity returns the rule severity
Expand Down
2 changes: 1 addition & 1 deletion rules/terraform_typed_variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (r *TerraformTypedVariablesRule) Name() string {

// Enabled returns whether the rule is enabled by default
func (r *TerraformTypedVariablesRule) Enabled() bool {
return false
return true
}

// Severity returns the rule severity
Expand Down
2 changes: 1 addition & 1 deletion rules/terraform_unused_declaration.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (r *TerraformUnusedDeclarationsRule) Name() string {

// Enabled returns whether the rule is enabled by default
func (r *TerraformUnusedDeclarationsRule) Enabled() bool {
return false
return true
}

// Severity returns the rule severity
Expand Down
2 changes: 1 addition & 1 deletion rules/terraform_unused_required_providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (r *TerraformUnusedRequiredProvidersRule) Name() string {

// Enabled returns whether the rule is enabled by default
func (r *TerraformUnusedRequiredProvidersRule) Enabled() bool {
return false
return true
}

// Severity returns the rule severity
Expand Down
6 changes: 6 additions & 0 deletions terraform/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package terraform

// Config is the configuration for the ruleset.
type Config struct {
Preset string `hclext:"preset,optional"`
}
Loading