summaryrefslogtreecommitdiff
path: root/vendor/github.com/hashicorp/terraform/terraform/version_required.go
blob: 3cbbf5608543a339fb65e21e47ac5491584a508d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package terraform

import (
	"fmt"

	"github.com/hashicorp/go-version"
	"github.com/hashicorp/terraform/config"
	"github.com/hashicorp/terraform/config/module"
)

// checkRequiredVersion verifies that any version requirements specified by
// the configuration are met.
//
// This checks the root module as well as any additional version requirements
// from child modules.
//
// This is tested in context_test.go.
func checkRequiredVersion(m *module.Tree) error {
	// Check any children
	for _, c := range m.Children() {
		if err := checkRequiredVersion(c); err != nil {
			return err
		}
	}

	var tf *config.Terraform
	if c := m.Config(); c != nil {
		tf = c.Terraform
	}

	// If there is no Terraform config or the required version isn't set,
	// we move on.
	if tf == nil || tf.RequiredVersion == "" {
		return nil
	}

	// Path for errors
	module := "root"
	if path := normalizeModulePath(m.Path()); len(path) > 1 {
		module = modulePrefixStr(path)
	}

	// Check this version requirement of this module
	cs, err := version.NewConstraint(tf.RequiredVersion)
	if err != nil {
		return fmt.Errorf(
			"%s: terraform.required_version %q syntax error: %s",
			module,
			tf.RequiredVersion, err)
	}

	if !cs.Check(SemVersion) {
		return fmt.Errorf(
			"The currently running version of Terraform doesn't meet the\n"+
				"version requirements explicitly specified by the configuration.\n"+
				"Please use the required version or update the configuration.\n"+
				"Note that version requirements are usually set for a reason, so\n"+
				"we recommend verifying with whoever set the version requirements\n"+
				"prior to making any manual changes.\n\n"+
				"  Module: %s\n"+
				"  Required version: %s\n"+
				"  Current version: %s",
			module,
			tf.RequiredVersion,
			SemVersion)
	}

	return nil
}