helper/schema: list ForceNew

This commit is contained in:
Mitchell Hashimoto 2014-08-14 23:32:20 -07:00
parent 22e286ffd5
commit 7f14708e71
4 changed files with 97 additions and 2 deletions

View File

@ -50,6 +50,21 @@ func (r *Resource) InternalValidate() error {
if v.Required && v.Computed {
return fmt.Errorf("%s: Cannot be both Required and Computed", k)
}
if v.Type == TypeList {
if v.Elem == nil {
return fmt.Errorf("%s: Elem must be set for lists", k)
}
switch t := v.Elem.(type) {
case *Schema:
bad := t.Computed || t.Optional || t.Required
if bad {
return fmt.Errorf(
"%s: Elem must have only Type set", k)
}
}
}
}
return nil

View File

@ -66,6 +66,34 @@ func TestResourceInternalValidate(t *testing.T) {
},
false,
},
// List element not set
{
&Resource{
Schema: map[string]*Schema{
"foo": &Schema{
Type: TypeList,
},
},
},
true,
},
// List element computed
{
&Resource{
Schema: map[string]*Schema{
"foo": &Schema{
Type: TypeList,
Elem: &Schema{
Type: TypeInt,
Computed: true,
},
},
},
},
true,
},
}
for i, tc := range cases {

View File

@ -144,15 +144,23 @@ func (m schemaMap) diffList(
}
// Diff the count no matter what
m.diffString(k+".#", &Schema{Type: TypeInt}, diff, s, c)
countSchema := &Schema{
Type: TypeInt,
ForceNew: schema.ForceNew,
}
m.diffString(k+".#", countSchema, diff, s, c)
switch t := schema.Elem.(type) {
case *Schema:
t2 := *t
t2.Computed = schema.Computed
t2.ForceNew = schema.ForceNew
// This is just a primitive element, so go through each and
// just diff each.
for i, _ := range vs {
subK := fmt.Sprintf("%s.%d", k, i)
err := m.diff(subK, t, diff, s, c)
err := m.diff(subK, &t2, diff, s, c)
if err != nil {
return err
}

View File

@ -265,6 +265,50 @@ func TestSchemaMap_Diff(t *testing.T) {
Err: false,
},
{
Schema: map[string]*Schema{
"ports": &Schema{
Type: TypeList,
Required: true,
Elem: &Schema{Type: TypeInt},
ForceNew: true,
},
},
State: nil,
Config: map[string]interface{}{
"ports": []interface{}{1, 2, 5},
},
Diff: &terraform.ResourceDiff{
Attributes: map[string]*terraform.ResourceAttrDiff{
"ports.#": &terraform.ResourceAttrDiff{
Old: "",
New: "3",
RequiresNew: true,
},
"ports.0": &terraform.ResourceAttrDiff{
Old: "",
New: "1",
RequiresNew: true,
},
"ports.1": &terraform.ResourceAttrDiff{
Old: "",
New: "2",
RequiresNew: true,
},
"ports.2": &terraform.ResourceAttrDiff{
Old: "",
New: "5",
RequiresNew: true,
},
},
},
Err: false,
},
}
for i, tc := range cases {