kubeform.dev/terraform-backend-sdk@v0.0.0-20220310143633-45f07fe731c5/terraform/version_required.go (about)

     1  package terraform
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/hashicorp/hcl/v2"
     7  	"kubeform.dev/terraform-backend-sdk/tfdiags"
     8  
     9  	"kubeform.dev/terraform-backend-sdk/configs"
    10  
    11  	tfversion "kubeform.dev/terraform-backend-sdk/version"
    12  )
    13  
    14  // CheckCoreVersionRequirements visits each of the modules in the given
    15  // configuration tree and verifies that any given Core version constraints
    16  // match with the version of Terraform Core that is being used.
    17  //
    18  // The returned diagnostics will contain errors if any constraints do not match.
    19  // The returned diagnostics might also return warnings, which should be
    20  // displayed to the user.
    21  func CheckCoreVersionRequirements(config *configs.Config) tfdiags.Diagnostics {
    22  	if config == nil {
    23  		return nil
    24  	}
    25  
    26  	var diags tfdiags.Diagnostics
    27  	module := config.Module
    28  
    29  	for _, constraint := range module.CoreVersionConstraints {
    30  		if !constraint.Required.Check(tfversion.SemVer) {
    31  			switch {
    32  			case len(config.Path) == 0:
    33  				diags = diags.Append(&hcl.Diagnostic{
    34  					Severity: hcl.DiagError,
    35  					Summary:  "Unsupported Terraform Core version",
    36  					Detail: fmt.Sprintf(
    37  						"This configuration does not support Terraform version %s. To proceed, either choose another supported Terraform version or update this version constraint. Version constraints are normally set for good reason, so updating the constraint may lead to other errors or unexpected behavior.",
    38  						tfversion.String(),
    39  					),
    40  					Subject: constraint.DeclRange.Ptr(),
    41  				})
    42  			default:
    43  				diags = diags.Append(&hcl.Diagnostic{
    44  					Severity: hcl.DiagError,
    45  					Summary:  "Unsupported Terraform Core version",
    46  					Detail: fmt.Sprintf(
    47  						"Module %s (from %s) does not support Terraform version %s. To proceed, either choose another supported Terraform version or update this version constraint. Version constraints are normally set for good reason, so updating the constraint may lead to other errors or unexpected behavior.",
    48  						config.Path, config.SourceAddr, tfversion.String(),
    49  					),
    50  					Subject: constraint.DeclRange.Ptr(),
    51  				})
    52  			}
    53  		}
    54  	}
    55  
    56  	for _, c := range config.Children {
    57  		childDiags := CheckCoreVersionRequirements(c)
    58  		diags = diags.Append(childDiags)
    59  	}
    60  
    61  	return diags
    62  }