helper/schema: store state with processed properly

This commit is contained in:
Mitchell Hashimoto 2014-08-22 12:09:06 -07:00
parent 50026a6d5c
commit 9ed601d541
2 changed files with 32 additions and 15 deletions

View File

@ -27,6 +27,7 @@ const (
// is called that contains some extra data that might be used. // is called that contains some extra data that might be used.
type getResult struct { type getResult struct {
Value interface{} Value interface{}
ValueProcessed interface{}
Exists bool Exists bool
Schema *Schema Schema *Schema
} }
@ -71,13 +72,17 @@ func (d *ResourceData) GetChange(key string) (interface{}, interface{}) {
// existed or not in the configuration. The second boolean result will also // existed or not in the configuration. The second boolean result will also
// be false if a key is given that isn't in the schema at all. // be false if a key is given that isn't in the schema at all.
func (d *ResourceData) GetOk(key string) (interface{}, bool) { func (d *ResourceData) GetOk(key string) (interface{}, bool) {
r := d.getRaw(key)
return r.Value, r.Exists
}
func (d *ResourceData) getRaw(key string) getResult {
var parts []string var parts []string
if key != "" { if key != "" {
parts = strings.Split(key, ".") parts = strings.Split(key, ".")
} }
r := d.getObject("", parts, d.schema, getSourceSet) return d.getObject("", parts, d.schema, getSourceSet)
return r.Value, r.Exists
} }
// HasChange returns whether or not the given key has been changed. // HasChange returns whether or not the given key has been changed.
@ -484,6 +489,7 @@ func (d *ResourceData) getPrimitive(
schema *Schema, schema *Schema,
source getSource) getResult { source getSource) getResult {
var result string var result string
var resultProcessed interface{}
var resultSet bool var resultSet bool
if d.state != nil && source >= getSourceState { if d.state != nil && source >= getSourceState {
result, resultSet = d.state.Attributes[k] result, resultSet = d.state.Attributes[k]
@ -509,6 +515,10 @@ func (d *ResourceData) getPrimitive(
if ok && !attrD.NewComputed { if ok && !attrD.NewComputed {
result = attrD.New result = attrD.New
if attrD.NewExtra != nil { if attrD.NewExtra != nil {
// If NewExtra != nil, then we have processed data as the New,
// so we store that but decode the unprocessed data into result
resultProcessed = result
err := mapstructure.WeakDecode(attrD.NewExtra, &result) err := mapstructure.WeakDecode(attrD.NewExtra, &result)
if err != nil { if err != nil {
panic(err) panic(err)
@ -565,6 +575,7 @@ func (d *ResourceData) getPrimitive(
return getResult{ return getResult{
Value: resultValue, Value: resultValue,
ValueProcessed: resultProcessed,
Exists: resultSet, Exists: resultSet,
Schema: schema, Schema: schema,
} }
@ -858,11 +869,16 @@ func (d *ResourceData) stateObject(
func (d *ResourceData) statePrimitive( func (d *ResourceData) statePrimitive(
prefix string, prefix string,
schema *Schema) map[string]string { schema *Schema) map[string]string {
v := d.Get(prefix) raw := d.getRaw(prefix)
if v == nil { if !raw.Exists {
return nil return nil
} }
v := raw.Value
if raw.ValueProcessed != nil {
v = raw.ValueProcessed
}
var vs string var vs string
switch schema.Type { switch schema.Type {
case TypeBool: case TypeBool:

View File

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