Skip to content

Commit

Permalink
Merge pull request #20 from padok-team/feat/required-provider-version
Browse files Browse the repository at this point in the history
feat(static): add required provider version operator check
  • Loading branch information
cterence authored Oct 13, 2023
2 parents bb498e0 + cfc1ea5 commit 2e5fd93
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 7 deletions.
55 changes: 55 additions & 0 deletions checks/required_provider_version_operator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package checks

import (
"guacamole/data"
"guacamole/helpers"
"regexp"

"github.com/hashicorp/terraform-config-inspect/tfconfig"
)

func RequiredProviderVersionOperatorInModules() (data.Check, error) {
name := "Required provider versions in modules should be set with ~> operator"
relatedGuidelines := "https://t.ly/0bxoy"
modules, err := helpers.GetModules()
if err != nil {
return data.Check{}, err
}
requiredProvidersInError := []string{}

pattern := `~>`
matcher, err := regexp.Compile(pattern)
if err != nil {
return data.Check{}, err
}

// For each module, check if the provider is defined
for _, module := range modules {
moduleConf, diags := tfconfig.LoadModule(module.FullPath)
if diags.HasErrors() {
return data.Check{}, diags.Err()
}

for _, requiredProvider := range moduleConf.RequiredProviders {
for _, versionConstraint := range requiredProvider.VersionConstraints {
matched := matcher.MatchString(versionConstraint)
if !matched {
requiredProvidersInError = append(requiredProvidersInError, requiredProvider.Source+" --> "+versionConstraint)
}
}
}
}

dataCheck := data.Check{
Name: name,
RelatedGuidelines: relatedGuidelines,
Status: "✅",
Errors: requiredProvidersInError,
}

if len(requiredProvidersInError) > 0 {
dataCheck.Status = "❌"
}

return dataCheck, nil
}
15 changes: 8 additions & 7 deletions checks/static_checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ import (
func StaticChecks() []data.Check {
// Add static checks here
checks := map[string]func() (data.Check, error){
"ProviderInModule": ProviderInModule,
"Stuttering": Stuttering,
"SnakeCase": SnakeCase,
"MissingVarDescription": MissingVarDescription,
"VarNumberMatchesType": VarNumberMatchesType,
"VariableTypeAny": VariableTypeAny,
"RemoteModuleVersion": RemoteModuleVersion,
"ProviderInModule": ProviderInModule,
"Stuttering": Stuttering,
"SnakeCase": SnakeCase,
"MissingVarDescription": MissingVarDescription,
"VarNumberMatchesType": VarNumberMatchesType,
"VariableTypeAny": VariableTypeAny,
"RemoteModuleVersion": RemoteModuleVersion,
"RequiredProviderVersionOperatorInModules": RequiredProviderVersionOperatorInModules,
}

var checkResults []data.Check
Expand Down

0 comments on commit 2e5fd93

Please sign in to comment.