github.com/kanishk98/terraform@v1.3.0-dev.0.20220917174235-661ca8088a6a/internal/terraform/version_required.go (about)

     1  package terraform
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/hashicorp/hcl/v2"
     7  	"github.com/hashicorp/terraform/internal/tfdiags"
     8  
     9  	"github.com/hashicorp/terraform/internal/configs"
    10  
    11  	tfversion "github.com/hashicorp/terraform/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  		// Before checking if the constraints are met, check that we are not using any prerelease fields as these
    31  		// are not currently supported.
    32  		var prereleaseDiags tfdiags.Diagnostics
    33  		for _, required := range constraint.Required {
    34  			if required.Prerelease() {
    35  				prereleaseDiags = prereleaseDiags.Append(&hcl.Diagnostic{
    36  					Severity: hcl.DiagError,
    37  					Summary:  "Invalid required_version constraint",
    38  					Detail: fmt.Sprintf(
    39  						"Prerelease version constraints are not supported: %s. Remove the prerelease information from the constraint. Prerelease versions of terraform will match constraints using their version core only.",
    40  						required.String()),
    41  					Subject: constraint.DeclRange.Ptr(),
    42  				})
    43  			}
    44  		}
    45  
    46  		if len(prereleaseDiags) > 0 {
    47  			// There were some prerelease fields in the constraints. Don't check the constraints as they will
    48  			// fail, and populate the diagnostics for these constraints with the prerelease diagnostics.
    49  			diags = diags.Append(prereleaseDiags)
    50  			continue
    51  		}
    52  
    53  		if !constraint.Required.Check(tfversion.SemVer) {
    54  			switch {
    55  			case len(config.Path) == 0:
    56  				diags = diags.Append(&hcl.Diagnostic{
    57  					Severity: hcl.DiagError,
    58  					Summary:  "Unsupported Terraform Core version",
    59  					Detail: fmt.Sprintf(
    60  						"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.",
    61  						tfversion.String(),
    62  					),
    63  					Subject: constraint.DeclRange.Ptr(),
    64  				})
    65  			default:
    66  				diags = diags.Append(&hcl.Diagnostic{
    67  					Severity: hcl.DiagError,
    68  					Summary:  "Unsupported Terraform Core version",
    69  					Detail: fmt.Sprintf(
    70  						"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.",
    71  						config.Path, config.SourceAddr, tfversion.String(),
    72  					),
    73  					Subject: constraint.DeclRange.Ptr(),
    74  				})
    75  			}
    76  		}
    77  	}
    78  
    79  	for _, c := range config.Children {
    80  		childDiags := CheckCoreVersionRequirements(c)
    81  		diags = diags.Append(childDiags)
    82  	}
    83  
    84  	return diags
    85  }