helper/schema: When having a StateFunc, make sure NewExtra contains

original
This commit is contained in:
Mitchell Hashimoto 2014-08-22 08:57:44 -07:00
parent ba68be5672
commit 50026a6d5c
4 changed files with 42 additions and 6 deletions

View File

@ -201,10 +201,6 @@ func (d *ResourceData) diffChange(k string) (interface{}, interface{}, bool) {
n.Value = nil n.Value = nil
} }
if n.Exists && n.Schema.StateFunc != nil {
n.Value = n.Schema.StateFunc(n.Value)
}
// Return the old, new, and whether there is a change // Return the old, new, and whether there is a change
return o.Value, n.Value, !reflect.DeepEqual(o.Value, n.Value) return o.Value, n.Value, !reflect.DeepEqual(o.Value, n.Value)
} }
@ -512,6 +508,13 @@ func (d *ResourceData) getPrimitive(
attrD, ok := d.diff.Attributes[k] attrD, ok := d.diff.Attributes[k]
if ok && !attrD.NewComputed { if ok && !attrD.NewComputed {
result = attrD.New result = attrD.New
if attrD.NewExtra != nil {
err := mapstructure.WeakDecode(attrD.NewExtra, &result)
if err != nil {
panic(err)
}
}
resultSet = true resultSet = true
} }
} }

View File

@ -68,6 +68,32 @@ func TestResourceDataGet(t *testing.T) {
Value: "foo", Value: "foo",
}, },
{
Schema: map[string]*Schema{
"availability_zone": &Schema{
Type: TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
},
State: nil,
Diff: &terraform.ResourceDiff{
Attributes: map[string]*terraform.ResourceAttrDiff{
"availability_zone": &terraform.ResourceAttrDiff{
Old: "",
New: "foo!",
NewExtra: "foo",
},
},
},
Key: "availability_zone",
Value: "foo",
},
{ {
Schema: map[string]*Schema{ Schema: map[string]*Schema{
"availability_zone": &Schema{ "availability_zone": &Schema{

View File

@ -386,8 +386,13 @@ func (m schemaMap) diffString(
schema *Schema, schema *Schema,
diff *terraform.ResourceDiff, diff *terraform.ResourceDiff,
d *ResourceData) error { d *ResourceData) error {
var originalN interface{}
var os, ns string var os, ns string
o, n, _ := d.diffChange(k) o, n, _ := d.diffChange(k)
if schema.StateFunc != nil {
originalN = n
n = schema.StateFunc(n)
}
if err := mapstructure.WeakDecode(o, &os); err != nil { if err := mapstructure.WeakDecode(o, &os); err != nil {
return fmt.Errorf("%s: %s", k, err) return fmt.Errorf("%s: %s", k, err)
} }
@ -411,6 +416,7 @@ func (m schemaMap) diffString(
diff.Attributes[k] = schema.finalizeDiff(&terraform.ResourceAttrDiff{ diff.Attributes[k] = schema.finalizeDiff(&terraform.ResourceAttrDiff{
Old: os, Old: os,
New: ns, New: ns,
NewExtra: originalN,
NewRemoved: removed, NewRemoved: removed,
}) })

View File

@ -100,6 +100,7 @@ func TestSchemaMap_Diff(t *testing.T) {
"availability_zone": &terraform.ResourceAttrDiff{ "availability_zone": &terraform.ResourceAttrDiff{
Old: "", Old: "",
New: "foo!", New: "foo!",
NewExtra: "foo",
}, },
}, },
}, },