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:
Sunny 2017-08-28 23:55:16 +05:30 committed by Martin Atkins
parent c12d64f340
commit 2d849f8650
5 changed files with 33 additions and 4 deletions

View File

@ -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

View File

@ -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)

View File

@ -0,0 +1,3 @@
terraform {
required_version = "~> 0.9.0"
}

View File

@ -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
} }
} }

View File

@ -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
} }
} }