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 more file structure rules for locals and init #15

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
9 changes: 5 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ import (
* - Resources should be named `this` where possible.
* - No providers in modules (this can be ignored on a module by module basis if needed)
* For all terraform
* - `terraform_remote_state {}` in _init.tf
* - `provider {}` in _init.tf
* - `terraform {}` in _init.tf
* - Do we want to restrict where locals can be put?
* - The following checks need tests:
* - checkProviders
* - checkTerraformBlock
* - checkLocals
* - checkTerraformRemoteState
*/

//go:embed VERSION
Expand Down
126 changes: 121 additions & 5 deletions rules/terraform_kb4_file_structure.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/terraform-linters/tflint-plugin-sdk/tflint"
)

var EXPECTED_FILES []string = []string{"_init.tf", "_variables.tf", "_outputs.tf"}
var EXPECTED_FILES []string = []string{"_init.tf", "_variables.tf", "_outputs.tf", "_locals.tf"}

// TerraformKb4FileStructureRule checks whether modules adhere to Terraform's standard module structure
type TerraformKb4FileStructureRule struct {
Expand Down Expand Up @@ -48,6 +48,10 @@ func (r *TerraformKb4FileStructureRule) Check(runner tflint.Runner) error {
r.checkFiles(runner)
r.checkVariables(runner)
r.checkOutputs(runner)
r.checkProviders(runner)
r.checkTerraformBlock(runner)
r.checkLocals(runner)
r.checkTerraformRemoteState(runner)

return nil
}
Expand Down Expand Up @@ -118,15 +122,127 @@ func (r *TerraformKb4FileStructureRule) checkOutputs(runner tflint.Runner) error
return err
}

for _, variable := range content.Blocks {
if variable.DefRange.Filename != "_outputs.tf" {
for _, output := range content.Blocks {
if output.DefRange.Filename != "_outputs.tf" {
runner.EmitIssue(
r,
fmt.Sprintf("variable %q should be moved from %s to %s", variable.Labels[0], variable.DefRange.Filename, "_outputs.tf"),
variable.DefRange,
fmt.Sprintf("output %q should be moved from %s to %s", output.Labels[0], output.DefRange.Filename, "_outputs.tf"),
output.DefRange,
)
}
}

return nil
}

func (r *TerraformKb4FileStructureRule) checkProviders(runner tflint.Runner) error {

content, err := runner.GetModuleContent(&hclext.BodySchema{
Blocks: []hclext.BlockSchema{
{
Type: "provider",
LabelNames: []string{"name"},
},
},
}, nil)

if err != nil {
return err
}

for _, provider := range content.Blocks {
if provider.DefRange.Filename != "_init.tf" {
runner.EmitIssue(
r,
fmt.Sprintf("provider %q should be moved from %s to %s", provider.Labels[0], provider.DefRange.Filename, "_init.tf"),
provider.DefRange,
)
}
}

return nil
}

func (r *TerraformKb4FileStructureRule) checkTerraformBlock(runner tflint.Runner) error {

content, err := runner.GetModuleContent(&hclext.BodySchema{
Blocks: []hclext.BlockSchema{
{
Type: "terraform",
},
},
}, nil)

if err != nil {
return err
}

for _, terraformBlock := range content.Blocks {
if terraformBlock.DefRange.Filename != "_init.tf" {
runner.EmitIssue(
r,
fmt.Sprintf("terraform block %q should be moved from %s to %s", terraformBlock.Labels[0], terraformBlock.DefRange.Filename, "_init.tf"),
terraformBlock.DefRange,
)
}
}

return nil
}

func (r *TerraformKb4FileStructureRule) checkLocals(runner tflint.Runner) error {

content, err := runner.GetModuleContent(&hclext.BodySchema{
Blocks: []hclext.BlockSchema{
{
Type: "locals",
},
},
}, nil)

if err != nil {
return err
}

for _, locals := range content.Blocks {
if locals.DefRange.Filename != "_init.tf" {
runner.EmitIssue(
r,
fmt.Sprintf("locals block %q should be moved from %s to %s", locals.Labels[0], locals.DefRange.Filename, "_init.tf"),
locals.DefRange,
)
}
}

return nil
}

func (r *TerraformKb4FileStructureRule) checkTerraformRemoteState(runner tflint.Runner) error {

content, err := runner.GetModuleContent(&hclext.BodySchema{
Blocks: []hclext.BlockSchema{
{
Type: "data",
LabelNames: []string{"name"},
},
},
}, nil)

if err != nil {
return err
}

for _, data := range content.Blocks {
if data.DefRange.Filename != "_init.tf" {
if data.Type == "terraform_remote_state" {
runner.EmitIssue(
r,
fmt.Sprintf("data terraform_remote_state %q should be moved from %s to %s", data.Name, data.DefRange.Filename, "_init.tf"),
data.DefRange,
)
}
}
}

return nil
}
32 changes: 32 additions & 0 deletions rules/terraform_kb4_file_structure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ func Test_TerraformKb4ModuleStructureRule(t *testing.T) {
Start: hcl.InitialPos,
},
},
{
Rule: NewTerraformKb4FileStructureRule(),
Message: "Module should include a _locals.tf file.",
Range: hcl.Range{
Filename: "_locals.tf",
Start: hcl.InitialPos,
},
},
},
},
{
Expand All @@ -50,6 +58,7 @@ func Test_TerraformKb4ModuleStructureRule(t *testing.T) {
"_init.tf": `terraform {}`,
"_variables.tf": `variable "v" {}`,
// "_outputs.tf": `variable "v" {}`,
"_locals.tf": `locals {}`,
},
Expected: helper.Issues{
{
Expand All @@ -68,6 +77,7 @@ func Test_TerraformKb4ModuleStructureRule(t *testing.T) {
"_init.tf": `terraform {}`,
// "_variables.tf": `variable "some_variable" {}`,
"_outputs.tf": `output "some_output" {}`,
"_locals.tf": `locals {}`,
},
Expected: helper.Issues{
{
Expand All @@ -86,6 +96,7 @@ func Test_TerraformKb4ModuleStructureRule(t *testing.T) {
// "_init.tf": `terraform {}`,
"_variables.tf": `variable "some_variable" {}`,
"_outputs.tf": `output "some_output" {}`,
"_locals.tf": `locals {}`,
},
Expected: helper.Issues{
{
Expand All @@ -98,12 +109,32 @@ func Test_TerraformKb4ModuleStructureRule(t *testing.T) {
},
},
},
{
Name: "missing locals file",
Content: map[string]string{
"_init.tf": `terraform {}`,
"_variables.tf": `variable "some_variable" {}`,
"_outputs.tf": `output "some_output" {}`,
// "_locals.tf": `locals {}`,
},
Expected: helper.Issues{
{
Rule: NewTerraformKb4FileStructureRule(),
Message: "Module should include a _locals.tf file.",
Range: hcl.Range{
Filename: filepath.Join("_locals.tf"),
Start: hcl.InitialPos,
},
},
},
},
{
Name: "no missing files",
Content: map[string]string{
"_init.tf": `terraform {}`,
"_variables.tf": `variable "some_variable" {}`,
"_outputs.tf": `output "some_output" {}`,
"_locals.tf": `locals {}`,
},
Expected: helper.Issues{},
},
Expand All @@ -113,6 +144,7 @@ func Test_TerraformKb4ModuleStructureRule(t *testing.T) {
"_init.tf": `variable "misplace_variable" {}`,
"_variables.tf": `variable "some_variable" {}`,
"_outputs.tf": `output "some_output" {}`,
"_locals.tf": `locals {}`,
},
Expected: helper.Issues{
{
Expand Down