Skip to content

Commit

Permalink
Merge pull request #4762 from hashicorp/phinze/validate-output-missin…
Browse files Browse the repository at this point in the history
…g-value

config: validation error when output is missing value field
  • Loading branch information
phinze committed Jan 20, 2016
2 parents 917102f + 87a9701 commit 944e6ee
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 944e6ee

Please sign in to comment.