From 0b2860fafce52c8a4a7d36500a0a598c7f34e7d3 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 10 Jul 2014 17:05:40 -0700 Subject: [PATCH] helper/config: more correct logic with regards to nested things --- helper/config/validator.go | 2 +- helper/config/validator_test.go | 39 ++++++++++++++++++++++++++++++++- 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/helper/config/validator.go b/helper/config/validator.go index 2898c6faf..1d8616c79 100644 --- a/helper/config/validator.go +++ b/helper/config/validator.go @@ -145,7 +145,7 @@ func (v *nestedValidatorKey) Validate( m map[string]string) ([]string, []string, []error) { countStr, ok := m[v.Prefix+".#"] if !ok { - if !v.Required { + if !v.Required || v.Key != "" { // Not present, that is okay return nil, nil, nil } else { diff --git a/helper/config/validator_test.go b/helper/config/validator_test.go index 224c68896..3886c6b91 100644 --- a/helper/config/validator_test.go +++ b/helper/config/validator_test.go @@ -70,6 +70,39 @@ func TestValidator_complex(t *testing.T) { testInvalid(v, c) } +func TestValidator_complexDeepRequired(t *testing.T) { + v := &Validator{ + Required: []string{ + "foo", + "nested.*.foo", + }, + } + + var c *terraform.ResourceConfig + + // Valid + c = testConfig(t, map[string]interface{}{ + "foo": "bar", + "nested": []map[string]interface{}{ + map[string]interface{}{"foo": "bar"}, + }, + }) + testValid(v, c) + + // Valid + c = testConfig(t, map[string]interface{}{ + "foo": "bar", + }) + testValid(v, c) + + // Not a nested structure + c = testConfig(t, map[string]interface{}{ + "foo": "bar", + "nested": "baa", + }) + testInvalid(v, c) +} + func testConfig( t *testing.T, c map[string]interface{}) *terraform.ResourceConfig { @@ -97,6 +130,10 @@ func testValid(v *Validator, c *terraform.ResourceConfig) { panic(fmt.Sprintf("bad: %#v", ws)) } if len(es) > 0 { - panic(fmt.Sprintf("bad: %#v", es)) + estrs := make([]string, len(es)) + for i, e := range es { + estrs[i] = e.Error() + } + panic(fmt.Sprintf("bad: %#v", estrs)) } }