config: validate that module variables can go to ints, convert [GH-624]

This commit is contained in:
Mitchell Hashimoto 2014-12-15 22:10:16 -08:00
parent 41dabd7abc
commit d24082da1e
5 changed files with 79 additions and 24 deletions

View File

@ -239,11 +239,14 @@ func (c *Config) Validate() error {
"%s: module repeated multiple times", "%s: module repeated multiple times",
m.Id())) m.Id()))
} }
// Already seen this module, just skip it
continue
} }
if _, ok := modules[m.Id()]; !ok { modules[m.Id()] = m
// If we haven't seen this module before, check that the
// source has no interpolations. // Check that the source has no interpolations
rc, err := NewRawConfig(map[string]interface{}{ rc, err := NewRawConfig(map[string]interface{}{
"root": m.Source, "root": m.Source,
}) })
@ -264,9 +267,26 @@ func (c *Config) Validate() error {
"dashes, and underscores", "dashes, and underscores",
m.Id())) m.Id()))
} }
// Check that the configuration can all be strings
raw := make(map[string]interface{})
for k, v := range m.RawConfig.Raw {
var strVal string
if err := mapstructure.WeakDecode(v, &strVal); err != nil {
errs = append(errs, fmt.Errorf(
"%s: variable %s must be a string value",
m.Id(), k))
}
raw[k] = strVal
} }
modules[m.Id()] = m // Update the raw configuration to only contain the string values
m.RawConfig, err = NewRawConfig(raw)
if err != nil {
errs = append(errs, fmt.Errorf(
"%s: can't initialize configuration: %s",
m.Id(), err))
}
} }
dupped = nil dupped = nil

View File

@ -137,6 +137,20 @@ func TestConfigValidate_moduleSourceVar(t *testing.T) {
} }
} }
func TestConfigValidate_moduleVarInt(t *testing.T) {
c := testConfig(t, "validate-module-var-int")
if err := c.Validate(); err != nil {
t.Fatalf("should be valid: %s", err)
}
}
func TestConfigValidate_moduleVarMap(t *testing.T) {
c := testConfig(t, "validate-module-var-map")
if err := c.Validate(); err == nil {
t.Fatal("should be invalid")
}
}
func TestConfigValidate_nil(t *testing.T) { func TestConfigValidate_nil(t *testing.T) {
var c Config var c Config
if err := c.Validate(); err != nil { if err := c.Validate(); err != nil {

View File

@ -0,0 +1,4 @@
module "foo" {
source = "./foo"
nodes = 3
}

View File

@ -0,0 +1,4 @@
module "foo" {
source = "./foo"
nodes = [1,2,3]
}

View File

@ -932,6 +932,19 @@ STATE:
<no state> <no state>
` `
const testTerraformPlanModuleVarIntStr = `
DIFF:
module.child:
CREATE: aws_instance.foo
num: "" => "2"
type: "" => "aws_instance"
STATE:
<no state>
`
const testTerraformPlanOrphanStr = ` const testTerraformPlanOrphanStr = `
DIFF: DIFF: