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