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

Add rules to enforce snake_case names #697

Merged
merged 23 commits into from
Apr 29, 2020
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
232cc22
Add rules to enforce snake_case
jgeurts Apr 2, 2020
9a74f48
Add documentation for snake_case rules
jgeurts Apr 2, 2020
85dbbee
Replace terraform_snake_case rules with terraform_naming_convention
jgeurts Apr 8, 2020
fd527e1
Fix some rule and code style issues
jgeurts Apr 10, 2020
660885f
Add tests for data block configurations
jgeurts Apr 10, 2020
0790e0c
Fix default format and custom properties
jgeurts Apr 12, 2020
b1e225e
Add tests for local values
jgeurts Apr 12, 2020
f7dcc9c
Add module tests
jgeurts Apr 12, 2020
c0f7091
Add tests for output blocks
jgeurts Apr 12, 2020
b73a824
Add tests for resource blocks
jgeurts Apr 12, 2020
8c16828
Add tests for variable blocks
jgeurts Apr 12, 2020
e7d2192
Add documentation for terraform_naming_convention
jgeurts Apr 12, 2020
206b111
Merge remote-tracking branch 'upstream/master' into snake_case_rules
jgeurts Apr 25, 2020
ca03eb2
Add naming convention rule
jgeurts Apr 25, 2020
b6f3b8e
Fix title
jgeurts Apr 25, 2020
fac3d80
Fix default value for enabled
jgeurts Apr 25, 2020
671d241
Fix description for how to fix
jgeurts Apr 25, 2020
19207de
Change severity to NOTICE
jgeurts Apr 25, 2020
5c57b53
Use “none” instead of “” to disable format
jgeurts Apr 25, 2020
3ac452f
Store known format regex as global variables
jgeurts Apr 25, 2020
4e48ab0
Return error instead of panic if custom regex does not compile
jgeurts Apr 25, 2020
5ab67e1
Fix formatting
jgeurts Apr 25, 2020
76be51f
Move block validation to separate methods
jgeurts Apr 27, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/rules/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,5 @@ These rules suggest to better ways.
|[terraform_documented_variables](terraform_documented_variables.md)||
|[terraform_typed_variables](terraform_typed_variables.md)||
|[terraform_module_pinned_source](terraform_module_pinned_source.md)|✔|
|[terraform_naming_convention](terraform_naming_convention.md)||
|[terraform_required_version](terraform_required_version.md)||
237 changes: 237 additions & 0 deletions docs/rules/terraform_naming_convention.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,237 @@
# terraform_naming_convention

Enforces naming conventions for the following blocks:

* Resources
* Input variables
* Output values
* Local values
* Modules
* Data sources

## Configuration

Name | Default | Value
--- | --- | ---
enabled | `false` | Boolean
format | `snake_case` | `snake_case`, `mixed_snake_case`, `""`
custom | `""` | String representation of a golang regular expression that the block name must match
data | | Block settings to override naming convention for data sources
locals | | Block settings to override naming convention for local values
module | | Block settings to override naming convention for modules
output | | Block settings to override naming convention for output values
resource | | Block settings to override naming convention for resources
variable | | Block settings to override naming convention for input variables


#### `format`

The `format` option defines the allowed formats for the block label.
This option accepts one of the following values:

* `snake_case` - standard snake_case format - all characters must be lower-case, and underscores are allowed.
* `mixed_snake_case` - modified snake_case format - characters may be upper or lower case, and underscores are allowed.
* `none` - signifies "this block shall not have its format checked". This can be useful if you want to enforce no particular format for a block.

#### `custom`

The `custom` option defines a custom regex that the identifier must match. This option allows you to have a
bit more finer-grained control over identifiers, letting you force certain patterns and substrings.

## Examples

### Default - enforce snake_case for all blocks

#### Rule configuration

```
rule "terraform_naming_convention" {
enabled = true
}
```

#### Sample terraform source file

```hcl
data "aws_eip" "camelCase" {
}

data "aws_eip" "valid_name" {
}
```

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

Notice: data name `camelCase` must match the following format: snake_case (terraform_naming_convention)

on template.tf line 1:
1: data "aws_eip" "camelCase" {

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

```


### Custom naming expression for all blocks

#### Rule configuration

```
rule "terraform_naming_convention" {
enabled = true

custom = "^[a-zA-Z]+([_-][a-zA-Z]+)*$"
}
```

#### Sample terraform source file

```hcl
resource "aws_eip" "Invalid_Name_With_Number123" {
}

resource "aws_eip" "Name-With_Dash" {
}
```

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

Notice: resource name `Invalid_Name_With_Number123` must match the following RegExp: ^[a-zA-Z]+([_-][a-zA-Z]+)*$ (terraform_naming_convention)

on template.tf line 1:
1: resource "aws_eip" "Invalid_Name_With_Number123" {

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

```


### Override default setting for specific block type

#### Rule configuration

```
rule "terraform_naming_convention" {
enabled = true

module {
custom = "^[a-zA-Z]+(_[a-zA-Z]+)*$"
}
}
```

#### Sample terraform source file

```hcl
// data name enforced with default snake_case
data "aws_eip" "eip_1a" {
}

module "valid_module" {
source = ""
}

module "invalid_module_with_number_1a" {
source = ""
}
```

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

Notice: module name `invalid_module_with_number_1a` must match the following RegExp: ^[a-zA-Z]+(_[a-zA-Z]+)*$ (terraform_naming_convention)

on template.tf line 9:
9: module "invalid_module_with_number_1a" {

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

```

### Disable for specific block type

#### Rule configuration

```
rule "terraform_naming_convention" {
enabled = true

module {
format = "none"
}
}
```

#### Sample terraform source file

```hcl
// data name enforced with default snake_case
data "aws_eip" "eip_1a" {
}

// module names will not be enforced
module "Valid_Name-Not-Enforced" {
source = ""
}
```


### Disable for all blocks but enforce a specific block type

#### Rule configuration

```
rule "terraform_naming_convention" {
enabled = true
format = "none"

local {
format = "snake_case"
}
}
```

#### Sample terraform source file

```hcl
// Data block name not enforced
data "aws_eip" "EIP_1a" {
}

// Resource block name not enforced
resource "aws_eip" "EIP_1b" {
}

// local variable names enforced
locals {
valid_name = "valid"
invalid-name = "dashes are not allowed with snake_case"
}
```

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

Notice: local value name `invalid-name` must match the following format: snake_case (terraform_naming_convention)

on template.tf line 12:
12: invalid-name = "dashes are not allowed with snake_case"

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

```

## Why

Naming conventions are optional, so it is not necessary to follow this.
But this rule is useful if you want to force the following naming conventions in line with the [Terraform Plugin Naming Best Practices](https://www.terraform.io/docs/extend/best-practices/naming.html).

## How To Fix

Update the block label according to the format or custom regular expression.
1 change: 1 addition & 0 deletions rules/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ var manualDefaultRules = []Rule{
terraformrules.NewTerraformDocumentedOutputsRule(),
terraformrules.NewTerraformDocumentedVariablesRule(),
terraformrules.NewTerraformModulePinnedSourceRule(),
terraformrules.NewTerraformNamingConventionRule(),
terraformrules.NewTerraformTypedVariablesRule(),
terraformrules.NewTerraformRequiredVersionRule(),
}
Expand Down