command/init: check required_version
Previously we were checking required_version only during "real" operations, and not during initialization. Catching it during init is better because that's the first command users run on a new working directory.
This commit is contained in:
parent
c12d64f340
commit
2d849f8650
|
@ -288,6 +288,11 @@ func (c *InitCommand) getProviders(path string, state *terraform.State, upgrade
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err := terraform.CheckRequiredVersion(mod); err != nil {
|
||||||
|
c.Ui.Error(err.Error())
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
var available discovery.PluginMetaSet
|
var available discovery.PluginMetaSet
|
||||||
if upgrade {
|
if upgrade {
|
||||||
// If we're in upgrade mode, we ignore any auto-installed plugins
|
// If we're in upgrade mode, we ignore any auto-installed plugins
|
||||||
|
|
|
@ -855,6 +855,27 @@ func TestInit_getProviderHaveLegacyVersion(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestInit_getProviderCheckRequiredVersion(t *testing.T) {
|
||||||
|
// Create a temporary working directory that is empty
|
||||||
|
td := tempDir(t)
|
||||||
|
copy.CopyDir(testFixturePath("init-check-required-version"), td)
|
||||||
|
defer os.RemoveAll(td)
|
||||||
|
defer testChdir(t, td)()
|
||||||
|
|
||||||
|
ui := new(cli.MockUi)
|
||||||
|
c := &InitCommand{
|
||||||
|
Meta: Meta{
|
||||||
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
||||||
|
Ui: ui,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
args := []string{}
|
||||||
|
if code := c.Run(args); code != 1 {
|
||||||
|
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestInit_providerLockFile(t *testing.T) {
|
func TestInit_providerLockFile(t *testing.T) {
|
||||||
// Create a temporary working directory that is empty
|
// Create a temporary working directory that is empty
|
||||||
td := tempDir(t)
|
td := tempDir(t)
|
||||||
|
|
|
@ -0,0 +1,3 @@
|
||||||
|
terraform {
|
||||||
|
required_version = "~> 0.9.0"
|
||||||
|
}
|
|
@ -122,7 +122,7 @@ type Context struct {
|
||||||
func NewContext(opts *ContextOpts) (*Context, error) {
|
func NewContext(opts *ContextOpts) (*Context, error) {
|
||||||
// Validate the version requirement if it is given
|
// Validate the version requirement if it is given
|
||||||
if opts.Module != nil {
|
if opts.Module != nil {
|
||||||
if err := checkRequiredVersion(opts.Module); err != nil {
|
if err := CheckRequiredVersion(opts.Module); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,17 +8,17 @@ import (
|
||||||
"github.com/hashicorp/terraform/config/module"
|
"github.com/hashicorp/terraform/config/module"
|
||||||
)
|
)
|
||||||
|
|
||||||
// checkRequiredVersion verifies that any version requirements specified by
|
// CheckRequiredVersion verifies that any version requirements specified by
|
||||||
// the configuration are met.
|
// the configuration are met.
|
||||||
//
|
//
|
||||||
// This checks the root module as well as any additional version requirements
|
// This checks the root module as well as any additional version requirements
|
||||||
// from child modules.
|
// from child modules.
|
||||||
//
|
//
|
||||||
// This is tested in context_test.go.
|
// This is tested in context_test.go.
|
||||||
func checkRequiredVersion(m *module.Tree) error {
|
func CheckRequiredVersion(m *module.Tree) error {
|
||||||
// Check any children
|
// Check any children
|
||||||
for _, c := range m.Children() {
|
for _, c := range m.Children() {
|
||||||
if err := checkRequiredVersion(c); err != nil {
|
if err := CheckRequiredVersion(c); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue