helper/schema: don't ask for input if provider default would not be nil

This commit is contained in:
Mitchell Hashimoto 2014-10-12 17:37:52 -07:00
parent c6a165f8c6
commit 12c178bc63
2 changed files with 80 additions and 0 deletions

View File

@ -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 {

View File

@ -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 {