-
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 #20 from padok-team/feat/required-provider-version
feat(static): add required provider version operator check
- Loading branch information
Showing
2 changed files
with
63 additions
and
7 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
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 | ||
} |
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