From 12c178bc63005914dd3c22d47d78f99f10bf0310 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sun, 12 Oct 2014 17:37:52 -0700 Subject: [PATCH] helper/schema: don't ask for input if provider default would not be nil --- helper/schema/schema.go | 20 ++++++++++++ helper/schema/schema_test.go | 60 ++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) diff --git a/helper/schema/schema.go b/helper/schema/schema.go index 7b756689d..7e26741ff 100644 --- a/helper/schema/schema.go +++ b/helper/schema/schema.go @@ -72,6 +72,9 @@ type Schema struct { // If Required is true above, then Default cannot be set. DefaultFunc // can be set with Required. If the DefaultFunc returns nil, then there // will no default and the user will be asked to fill it in. + // + // If either of these is set, then the user won't be asked for input + // for this key if the default is not nil. Default interface{} DefaultFunc SchemaDefaultFunc @@ -81,6 +84,8 @@ type Schema struct { Description string // InputDefault is the default value to use for when inputs are requested. + // This differs from Default in that if Default is set, no input is + // asked for. If Input is asked, this will be the default value offered. InputDefault string // The fields below relate to diffs. @@ -308,6 +313,21 @@ func (m schemaMap) Input( continue } + // Skip if it has a default + if v.Default != nil { + continue + } + if f := v.DefaultFunc; f != nil { + value, err := f() + if err != nil { + return nil, fmt.Errorf( + "%s: error loading default: %s", k, err) + } + if value != nil { + continue + } + } + var value interface{} var err error switch v.Type { diff --git a/helper/schema/schema_test.go b/helper/schema/schema_test.go index 0819e45f3..d25ce32d0 100644 --- a/helper/schema/schema_test.go +++ b/helper/schema/schema_test.go @@ -1419,6 +1419,66 @@ func TestSchemaMap_Input(t *testing.T) { Err: false, }, + + { + Schema: map[string]*Schema{ + "availability_zone": &Schema{ + Type: TypeString, + Default: "foo", + Optional: true, + }, + }, + + Input: map[string]string{ + "availability_zone": "bar", + }, + + Result: map[string]interface{}{}, + + Err: false, + }, + + { + Schema: map[string]*Schema{ + "availability_zone": &Schema{ + Type: TypeString, + DefaultFunc: func() (interface{}, error) { + return "foo", nil + }, + Optional: true, + }, + }, + + Input: map[string]string{ + "availability_zone": "bar", + }, + + Result: map[string]interface{}{}, + + Err: false, + }, + + { + Schema: map[string]*Schema{ + "availability_zone": &Schema{ + Type: TypeString, + DefaultFunc: func() (interface{}, error) { + return nil, nil + }, + Optional: true, + }, + }, + + Input: map[string]string{ + "availability_zone": "bar", + }, + + Result: map[string]interface{}{ + "availability_zone": "bar", + }, + + Err: false, + }, } for i, tc := range cases {