Skip to content

Commit

Permalink
config: validation error when output is missing value field
Browse files Browse the repository at this point in the history
Also lists out invalid keys in errmsg when they are present

Closes #4398
  • Loading branch information
phinze committed Jan 20, 2016
1 parent 2dff310 commit 87a9701
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 8 deletions.
21 changes: 14 additions & 7 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -500,16 +500,23 @@ func (c *Config) Validate() error {

// Check that all outputs are valid
for _, o := range c.Outputs {
invalid := false
for k, _ := range o.RawConfig.Raw {
if k != "value" {
invalid = true
break
var invalidKeys []string
valueKeyFound := false
for k := range o.RawConfig.Raw {
if k == "value" {
valueKeyFound = true
} else {
invalidKeys = append(invalidKeys, k)
}
}
if invalid {
if len(invalidKeys) > 0 {
errs = append(errs, fmt.Errorf(
"%s: output should only have 'value' field", o.Name))
"%s: output has invalid keys: %s",
o.Name, strings.Join(invalidKeys, ", ")))
}
if !valueKeyFound {
errs = append(errs, fmt.Errorf(
"%s: output is missing required 'value' key", o.Name))
}

for _, v := range o.RawConfig.Variables {
Expand Down
7 changes: 7 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,13 @@ func TestConfigValidate_outputBadField(t *testing.T) {
}
}

func TestConfigValidate_outputMissingEquals(t *testing.T) {
c := testConfig(t, "validate-output-missing-equals")
if err := c.Validate(); err == nil {
t.Fatal("should not be valid")
}
}

func TestConfigValidate_pathVar(t *testing.T) {
c := testConfig(t, "validate-path-var")
if err := c.Validate(); err != nil {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
variable "memory" {}

output "result" {}
output "result" { value = "foo" }
3 changes: 3 additions & 0 deletions config/test-fixtures/validate-output-missing-equals/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
output "whoops" {
value "wheresmyequals"
}

0 comments on commit 87a9701

Please sign in to comment.