Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

Commit ced4c53

Browse files
authored
Merge pull request hashicorp#11757 from hashicorp/jbardin/hashicorpGH-11588
Make sure to diff all nested schema.Set elements
2 parents 34227e5 + 7359a18 commit ced4c53

2 files changed

Lines changed: 101 additions & 0 deletions

File tree

helper/schema/schema.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -919,6 +919,7 @@ func (m schemaMap) diffSet(
919919
diff *terraform.InstanceDiff,
920920
d *ResourceData,
921921
all bool) error {
922+
922923
o, n, _, computedSet := d.diffChange(k)
923924
if computedSet {
924925
n = nil
@@ -1003,14 +1004,25 @@ func (m schemaMap) diffSet(
10031004
for _, code := range list {
10041005
switch t := schema.Elem.(type) {
10051006
case *Resource:
1007+
countDiff, cOk := diff.GetAttribute(k + ".#")
10061008
// This is a complex resource
10071009
for k2, schema := range t.Schema {
10081010
subK := fmt.Sprintf("%s.%s.%s", k, code, k2)
10091011
err := m.diff(subK, schema, diff, d, true)
10101012
if err != nil {
10111013
return err
10121014
}
1015+
1016+
// If parent set is being removed
1017+
// remove all subfields which were missed by the diff func
1018+
// We process these separately because type-specific diff functions
1019+
// lack the context (hierarchy of fields)
1020+
subKeyIsCount := strings.HasSuffix(subK, ".#")
1021+
if cOk && countDiff.New == "0" && !subKeyIsCount {
1022+
m.markAsRemoved(subK, schema, diff)
1023+
}
10131024
}
1025+
10141026
case *Schema:
10151027
// Copy the schema so that we can set Computed/ForceNew from
10161028
// the parent schema (the TypeSet).

helper/schema/schema_test.go

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2985,6 +2985,95 @@ func TestSchemaMap_Diff(t *testing.T) {
29852985
},
29862986
},
29872987
},
2988+
2989+
{
2990+
Name: "Removal of TypeSet should cause computed fields to be removed",
2991+
Schema: map[string]*Schema{
2992+
"type_set": &Schema{
2993+
Type: TypeSet,
2994+
Optional: true,
2995+
Elem: &Resource{
2996+
Schema: map[string]*Schema{
2997+
"name": &Schema{
2998+
Type: TypeString,
2999+
Optional: true,
3000+
},
3001+
"required": &Schema{
3002+
Type: TypeString,
3003+
Required: true,
3004+
},
3005+
"value": &Schema{
3006+
Type: TypeInt,
3007+
Optional: true,
3008+
},
3009+
"required_value": &Schema{
3010+
Type: TypeInt,
3011+
Required: true,
3012+
},
3013+
"computed_value": &Schema{
3014+
Type: TypeString,
3015+
Optional: true,
3016+
Computed: true,
3017+
},
3018+
},
3019+
},
3020+
Set: func(i interface{}) int {
3021+
if i != nil {
3022+
return 12345
3023+
}
3024+
return 0
3025+
},
3026+
},
3027+
},
3028+
3029+
State: &terraform.InstanceState{
3030+
Attributes: map[string]string{
3031+
"type_set.#": "1",
3032+
"type_set.12345.name": "Name",
3033+
"type_set.12345.required": "Required",
3034+
"type_set.12345.value": "0",
3035+
"type_set.12345.required_value": "5",
3036+
"type_set.12345.computed_value": "COMPUTED",
3037+
},
3038+
},
3039+
3040+
Config: map[string]interface{}{
3041+
"type_set": []interface{}{},
3042+
},
3043+
3044+
Diff: &terraform.InstanceDiff{
3045+
Attributes: map[string]*terraform.ResourceAttrDiff{
3046+
"type_set.#": &terraform.ResourceAttrDiff{
3047+
Old: "1",
3048+
New: "0",
3049+
NewRemoved: false,
3050+
},
3051+
"type_set.12345.name": &terraform.ResourceAttrDiff{
3052+
Old: "Name",
3053+
New: "",
3054+
NewRemoved: true,
3055+
},
3056+
"type_set.12345.required": &terraform.ResourceAttrDiff{
3057+
Old: "Required",
3058+
New: "",
3059+
NewRemoved: true,
3060+
},
3061+
"type_set.12345.value": &terraform.ResourceAttrDiff{
3062+
Old: "0",
3063+
New: "0",
3064+
NewRemoved: true,
3065+
},
3066+
"type_set.12345.required_value": &terraform.ResourceAttrDiff{
3067+
Old: "5",
3068+
New: "0",
3069+
NewRemoved: true,
3070+
},
3071+
"type_set.12345.computed_value": &terraform.ResourceAttrDiff{
3072+
NewRemoved: true,
3073+
},
3074+
},
3075+
},
3076+
},
29883077
}
29893078

29903079
for i, tc := range cases {

0 commit comments

Comments
 (0)