Skip to content

Commit

Permalink
fix(terraform_required_version_range): terraform block (#2529)
Browse files Browse the repository at this point in the history
  • Loading branch information
apeabody committed Aug 15, 2024
1 parent c7bc101 commit f5dce52
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
2 changes: 1 addition & 1 deletion tflint-ruleset-blueprint/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func main() {
plugin.Serve(&plugin.ServeOpts{
RuleSet: &tflint.BuiltinRuleSet{
Name: "blueprint",
Version: "0.2.3",
Version: "0.2.4",
Rules: []tflint.Rule{
rules.NewTerraformDocSamplesRestrictedBlocks(),
rules.NewTerraformDocSamplesRestrictedResources(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,30 +147,28 @@ func (r *TerraformRequiredVersionRange) Check(runner tflint.Runner) error {
for _, block := range content.Blocks {
requiredVersion, exists := block.Body.Attributes["required_version"]
if !exists {
logger.Info(fmt.Sprintf("required_version does not exist for %s", block.Labels[0]))
logger.Info(fmt.Sprintf("terraform block does not contain required_version: %s", block.DefRange))
continue
}

var raw_terraform_required_version string
diags := gohcl.DecodeExpression(requiredVersion.Expr, nil, &raw_terraform_required_version)
if diags.HasErrors() {
return fmt.Errorf("failed to decode terraform required_version %q: %v", block.Labels[0], diags.Error())
return fmt.Errorf("failed to decode terraform block required_version: %v", diags.Error())
}

constraints, err := version.NewConstraint(raw_terraform_required_version)
if err != nil {
return err
}

//TODO: add option for repository exemptions
if !((constraints.Check(minimum_required_version) || constraints.Check(maximum_required_version)) && !constraints.Check(below_required_version)) {
//TODO: use EmitIssueWithFix()
err := runner.EmitIssue(r, fmt.Sprintf("required_version is not inclusive of the the minimum %q and maximum %q terraform required_version: %q", minVersion, maxVersion, constraints.String()), block.DefRange)
if err != nil {
return err
}
}

}

return nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,13 @@ terraform {
terraform {
required_version = "~>1.9"
}

terraform {
backend "gcs" {
bucket = "UPDATE_ME"
prefix = "UPDATE_ME"
}
}

terraform {
}

0 comments on commit f5dce52

Please sign in to comment.