github.com/LorbusChris/terraform@v0.11.12-beta1/terraform/version_required.go (about)

     1  package terraform
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/hashicorp/go-version"
     7  	"github.com/hashicorp/terraform/config"
     8  	"github.com/hashicorp/terraform/config/module"
     9  
    10  	tfversion "github.com/hashicorp/terraform/version"
    11  )
    12  
    13  // CheckRequiredVersion verifies that any version requirements specified by
    14  // the configuration are met.
    15  //
    16  // This checks the root module as well as any additional version requirements
    17  // from child modules.
    18  //
    19  // This is tested in context_test.go.
    20  func CheckRequiredVersion(m *module.Tree) error {
    21  	// Check any children
    22  	for _, c := range m.Children() {
    23  		if err := CheckRequiredVersion(c); err != nil {
    24  			return err
    25  		}
    26  	}
    27  
    28  	var tf *config.Terraform
    29  	if c := m.Config(); c != nil {
    30  		tf = c.Terraform
    31  	}
    32  
    33  	// If there is no Terraform config or the required version isn't set,
    34  	// we move on.
    35  	if tf == nil || tf.RequiredVersion == "" {
    36  		return nil
    37  	}
    38  
    39  	// Path for errors
    40  	module := "root"
    41  	if path := normalizeModulePath(m.Path()); len(path) > 1 {
    42  		module = modulePrefixStr(path)
    43  	}
    44  
    45  	// Check this version requirement of this module
    46  	cs, err := version.NewConstraint(tf.RequiredVersion)
    47  	if err != nil {
    48  		return fmt.Errorf(
    49  			"%s: terraform.required_version %q syntax error: %s",
    50  			module,
    51  			tf.RequiredVersion, err)
    52  	}
    53  
    54  	if !cs.Check(tfversion.SemVer) {
    55  		return fmt.Errorf(
    56  			"The currently running version of Terraform doesn't meet the\n"+
    57  				"version requirements explicitly specified by the configuration.\n"+
    58  				"Please use the required version or update the configuration.\n"+
    59  				"Note that version requirements are usually set for a reason, so\n"+
    60  				"we recommend verifying with whoever set the version requirements\n"+
    61  				"prior to making any manual changes.\n\n"+
    62  				"  Module: %s\n"+
    63  				"  Required version: %s\n"+
    64  				"  Current version: %s",
    65  			module,
    66  			tf.RequiredVersion,
    67  			tfversion.SemVer)
    68  	}
    69  
    70  	return nil
    71  }