From 4edbcb87182a61af02d8263432186c57e9a64a2f Mon Sep 17 00:00:00 2001 From: Jack Leow Date: Wed, 14 Apr 2021 12:52:49 -0700 Subject: [PATCH 1/2] Added test to verify SchemaStateFunc is called on TypeMap values. --- helper/schema/schema_test.go | 107 +++++++++++++++++++++++++++++++++++ 1 file changed, 107 insertions(+) diff --git a/helper/schema/schema_test.go b/helper/schema/schema_test.go index 389d98559aa..c1b80bca1f2 100644 --- a/helper/schema/schema_test.go +++ b/helper/schema/schema_test.go @@ -1386,6 +1386,113 @@ func TestSchemaMap_Diff(t *testing.T) { Err: false, }, + { + Name: "Maps", + Schema: map[string]*Schema{ + "config_vars": { + Type: TypeMap, + }, + }, + + State: &terraform.InstanceState{ + Attributes: map[string]string{ + "config_vars.foo": "bar", + }, + }, + + Config: map[string]interface{}{ + "config_vars": []interface{}{ + map[string]interface{}{ + "foo": "baz", + }, + }, + }, + + Diff: &terraform.InstanceDiff{ + Attributes: map[string]*terraform.ResourceAttrDiff{ + "config_vars.foo": { + Old: "bar", + New: "baz", + }, + }, + }, + + Err: false, + }, + + { + Name: "Maps with StateFunc same", + Schema: map[string]*Schema{ + "config_vars": { + Type: TypeMap, + Elem: &Schema{ + Type: TypeString, + StateFunc: func(i interface{}) string { + return strings.ToUpper(i.(string)) + }, + }, + }, + }, + + State: &terraform.InstanceState{ + Attributes: map[string]string{ + "config_vars.foo": "BAR", + }, + }, + + Config: map[string]interface{}{ + "config_vars": []interface{}{ + map[string]interface{}{ + "foo": "bar", + }, + }, + }, + + Diff: nil, + + Err: false, + }, + + { + Name: "Maps with StateFunc diff", + Schema: map[string]*Schema{ + "config_vars": { + Type: TypeMap, + Elem: &Schema{ + Type: TypeString, + StateFunc: func(i interface{}) string { + return strings.ToUpper(i.(string)) + }, + }, + }, + }, + + State: &terraform.InstanceState{ + Attributes: map[string]string{ + "config_vars.foo": "bar", + }, + }, + + Config: map[string]interface{}{ + "config_vars": []interface{}{ + map[string]interface{}{ + "foo": "bar", + }, + }, + }, + + Diff: &terraform.InstanceDiff{ + Attributes: map[string]*terraform.ResourceAttrDiff{ + "config_vars.foo": { + Old: "bar", + New: "BAR", + }, + }, + }, + + Err: false, + }, + { Name: "Maps", Schema: map[string]*Schema{ From 8411172fcae58bb063e3e7c9271c98ba71ad811f Mon Sep 17 00:00:00 2001 From: Jack Leow Date: Wed, 14 Apr 2021 12:53:49 -0700 Subject: [PATCH 2/2] Modified Schema diffMap to apply SchemaStateFunc to config value if present. --- helper/schema/schema.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/helper/schema/schema.go b/helper/schema/schema.go index 7146bef766b..29cb00b8563 100644 --- a/helper/schema/schema.go +++ b/helper/schema/schema.go @@ -1192,11 +1192,20 @@ func (m schemaMap) diffMap( return nil } + var stateFunc *SchemaStateFunc + if schema.Elem != nil && ((schema.Elem).(*Schema)).StateFunc != nil { + stateFunc = &((schema.Elem).(*Schema)).StateFunc + } + // Now we compare, preferring values from the config map for k, v := range configMap { old, ok := stateMap[k] delete(stateMap, k) + if stateFunc != nil { + v = (*stateFunc)(v) + } + if old == v && ok && !all { continue }