schema: Make validation more strict

This commit is contained in:
Radek Simko 2015-09-19 19:11:49 +01:00 committed by Radek Simko
parent abe5189eb8
commit 641b701830
2 changed files with 43 additions and 0 deletions

View File

@ -244,7 +244,20 @@ func (r *Resource) InternalValidate(topSchemaMap schemaMap) error {
return fmt.Errorf(
"No Update defined, must set ForceNew on: %#v", nonForceNewAttrs)
}
} else {
nonUpdateableAttrs := make([]string, 0)
for k, v := range r.Schema {
if v.ForceNew || (v.Computed && !v.Optional) {
nonUpdateableAttrs = append(nonUpdateableAttrs, k)
}
}
updateableAttrs := len(r.Schema) - len(nonUpdateableAttrs)
if updateableAttrs == 0 {
return fmt.Errorf(
"All fields are ForceNew or Computed w/out Optional, Update is superfluous")
}
}
tsm = schemaMap(r.Schema)
}

View File

@ -335,6 +335,36 @@ func TestResourceInternalValidate(t *testing.T) {
},
true,
},
// Update undefined for non-ForceNew field
{
&Resource{
Create: func(d *ResourceData, meta interface{}) error { return nil },
Schema: map[string]*Schema{
"boo": &Schema{
Type: TypeInt,
Optional: true,
},
},
},
true,
},
// Update defined for ForceNew field
{
&Resource{
Create: func(d *ResourceData, meta interface{}) error { return nil },
Update: func(d *ResourceData, meta interface{}) error { return nil },
Schema: map[string]*Schema{
"goo": &Schema{
Type: TypeInt,
Optional: true,
ForceNew: true,
},
},
},
true,
},
}
for i, tc := range cases {