helper/schema: Defaults [GH-245]

This commit is contained in:
Mitchell Hashimoto 2014-09-09 21:17:29 -07:00
parent 397cf82c96
commit 5919637456
2 changed files with 66 additions and 0 deletions

View File

@ -62,6 +62,10 @@ type Schema struct {
Optional bool
Required bool
// If this is non-nil, then this will be a default value that is used
// when this item is not set in the configuration/state.
Default interface{}
// The fields below relate to diffs.
//
// If Computed is true, then the result of this value is computed
@ -280,6 +284,10 @@ func (m schemaMap) InternalValidate() error {
return fmt.Errorf("%s: One of optional, required, or computed must be set", k)
}
if v.Computed && v.Default != nil {
return fmt.Errorf("%s: Default must be nil if computed", k)
}
if len(v.ComputedWhen) > 0 && !v.Computed {
return fmt.Errorf("%s: ComputedWhen can only be set with Computed", k)
}
@ -289,6 +297,10 @@ func (m schemaMap) InternalValidate() error {
return fmt.Errorf("%s: Elem must be set for lists", k)
}
if v.Default != nil {
return fmt.Errorf("%s: Default is not valid for lists or sets", k)
}
if v.Type == TypeList && v.Set != nil {
return fmt.Errorf("%s: Set can only be set for TypeSet", k)
} else if v.Type == TypeSet && v.Set == nil {
@ -492,6 +504,9 @@ func (m schemaMap) diffString(
var originalN interface{}
var os, ns string
o, n, _ := d.diffChange(k)
if n == nil {
n = schema.Default
}
if schema.StateFunc != nil {
originalN = n
n = schema.StateFunc(n)

View File

@ -98,6 +98,32 @@ func TestSchemaMap_Diff(t *testing.T) {
Err: false,
},
// Default
{
Schema: map[string]*Schema{
"availability_zone": &Schema{
Type: TypeString,
Optional: true,
Default: "foo",
},
},
State: nil,
Config: nil,
Diff: &terraform.ResourceDiff{
Attributes: map[string]*terraform.ResourceAttrDiff{
"availability_zone": &terraform.ResourceAttrDiff{
Old: "",
New: "foo",
},
},
},
Err: false,
},
// String with StateFunc
{
Schema: map[string]*Schema{
@ -1016,6 +1042,19 @@ func TestSchemaMap_InternalValidate(t *testing.T) {
false,
},
// Computed but has default
{
map[string]*Schema{
"foo": &Schema{
Type: TypeInt,
Optional: true,
Computed: true,
Default: "foo",
},
},
true,
},
// List element not set
{
map[string]*Schema{
@ -1026,6 +1065,18 @@ func TestSchemaMap_InternalValidate(t *testing.T) {
true,
},
// List default
{
map[string]*Schema{
"foo": &Schema{
Type: TypeList,
Elem: &Schema{Type: TypeInt},
Default: "foo",
},
},
true,
},
// List element computed
{
map[string]*Schema{