-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #91 from padok-team/feat/0.3.0
feat(0.3.0): new check
- Loading branch information
Showing
7 changed files
with
169 additions
and
167 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package checks | ||
|
||
import ( | ||
"github.com/padok-team/guacamole/data" | ||
|
||
"github.com/hashicorp/terraform-config-inspect/tfconfig" | ||
) | ||
|
||
func ResourceNaming(modules map[string]data.TerraformModule) (data.Check, error) { | ||
dataCheck := data.Check{ | ||
ID: "TF_NAM_005", | ||
Name: "Resources and data sources should not be named \"this\" or \"these\" if there are more than 1 of the same type", | ||
RelatedGuidelines: "https://padok-team.github.io/docs-terraform-guidelines/terraform/terraform_naming.html#resource-andor-data-source-naming", | ||
Status: "✅", | ||
} | ||
|
||
resourcesInError := []data.Error{} | ||
|
||
for _, module := range modules { | ||
moduleConf, diags := tfconfig.LoadModule(module.FullPath) | ||
if diags.HasErrors() { | ||
return data.Check{}, diags.Err() | ||
} | ||
// Check resources | ||
for _, resource := range moduleConf.ManagedResources { | ||
// Check if the type of the resource is unique within the module | ||
numberOfSameType := 0 | ||
for _, res := range moduleConf.ManagedResources { | ||
if res.Type == resource.Type { | ||
numberOfSameType++ | ||
} | ||
} | ||
// If there is only one instance of this type of resource, check if its named this or these (If they create more than 1 with a for each) | ||
if numberOfSameType > 1 { | ||
if resource.Name == "these" || resource.Name == "this" { | ||
resourcesInError = append(resourcesInError, data.Error{ | ||
Path: resource.Pos.Filename, | ||
LineNumber: resource.Pos.Line, | ||
Description: resource.Type + " - " + resource.Name, | ||
}) | ||
} | ||
} | ||
} | ||
// Check data sources | ||
for _, dataResource := range moduleConf.DataResources { | ||
// Check if the type of the resource is unique within the module | ||
numberOfSameType := 0 | ||
for _, res := range moduleConf.DataResources { | ||
if res.Type == dataResource.Type { | ||
numberOfSameType++ | ||
} | ||
} | ||
// If there is only one instance of this type of resource, check if its named this or these (If they create more than 1 with a for each) | ||
if numberOfSameType > 1 { | ||
if dataResource.Name == "these" || dataResource.Name == "this" { | ||
resourcesInError = append(resourcesInError, data.Error{ | ||
Path: dataResource.Pos.Filename, | ||
LineNumber: dataResource.Pos.Line, | ||
Description: dataResource.Type + " - " + dataResource.Name, | ||
}) | ||
} | ||
} | ||
} | ||
} | ||
|
||
dataCheck.Errors = resourcesInError | ||
|
||
if len(resourcesInError) > 0 { | ||
dataCheck.Status = "❌" | ||
} | ||
|
||
return dataCheck, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.