Skip to content

Commit 3a6fd7e

Browse files
authored
chore: Refactors AttributeChanges (#3122)
* AttributeChanges as []string instead of struct * check if autoscaling * fix doc * fix linter * include related fields * Revert "include related fields" This reverts commit 07624e8. * Revert "fix linter" This reverts commit 5cddaec. * Revert "fix doc" This reverts commit 85db779. * Revert "check if autoscaling" This reverts commit 70042c3.
1 parent 289c7f0 commit 3a6fd7e

File tree

3 files changed

+30
-46
lines changed

3 files changed

+30
-46
lines changed

internal/common/schemafunc/find_changes.go

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,19 @@ import (
1010
"github.com/hashicorp/terraform-plugin-framework/types"
1111
)
1212

13-
type AttributeChanges struct {
14-
Changes []string
15-
}
13+
type AttributeChanges []string
1614

17-
func (a *AttributeChanges) LeafChanges() map[string]bool {
15+
func (a AttributeChanges) LeafChanges() map[string]bool {
1816
return a.leafChanges(true)
1917
}
2018

21-
func (a *AttributeChanges) AttributeChanged(name string) bool {
19+
func (a AttributeChanges) AttributeChanged(name string) bool {
2220
changes := a.LeafChanges()
2321
changed := changes[name]
2422
return changed
2523
}
2624

27-
func (a *AttributeChanges) KeepUnknown(attributeEffectedMapping map[string][]string) []string {
25+
func (a AttributeChanges) KeepUnknown(attributeEffectedMapping map[string][]string) []string {
2826
var keepUnknown []string
2927
for attrChanged, affectedAttributes := range attributeEffectedMapping {
3028
if a.AttributeChanged(attrChanged) {
@@ -36,25 +34,25 @@ func (a *AttributeChanges) KeepUnknown(attributeEffectedMapping map[string][]str
3634
}
3735

3836
// ListIndexChanged returns true if the list at the given index has changed, false if it was added or removed
39-
func (a *AttributeChanges) ListIndexChanged(name string, index int) bool {
37+
func (a AttributeChanges) ListIndexChanged(name string, index int) bool {
4038
leafChanges := a.leafChanges(false)
4139
indexPath := fmt.Sprintf("%s[%d]", name, index)
4240
return leafChanges[indexPath]
4341
}
4442

4543
// NestedListLenChanges accepts a fullPath, e.g., "replication_specs[0].region_configs" and returns true if the length of the nested list has changed
46-
func (a *AttributeChanges) NestedListLenChanges(fullPath string) bool {
44+
func (a AttributeChanges) NestedListLenChanges(fullPath string) bool {
4745
addPrefix := fmt.Sprintf("%s[+", fullPath)
4846
removePrefix := fmt.Sprintf("%s[-", fullPath)
49-
for _, change := range a.Changes {
47+
for _, change := range a {
5048
if strings.HasPrefix(change, addPrefix) || strings.HasPrefix(change, removePrefix) {
5149
return true
5250
}
5351
}
5452
return false
5553
}
5654

57-
func (a *AttributeChanges) ListLenChanges(name string) bool {
55+
func (a AttributeChanges) ListLenChanges(name string) bool {
5856
leafChanges := a.leafChanges(false)
5957
addPrefix := fmt.Sprintf("%s[+", name)
6058
removePrefix := fmt.Sprintf("%s[-", name)
@@ -66,9 +64,9 @@ func (a *AttributeChanges) ListLenChanges(name string) bool {
6664
return false
6765
}
6866

69-
func (a *AttributeChanges) leafChanges(removeIndex bool) map[string]bool {
67+
func (a AttributeChanges) leafChanges(removeIndex bool) map[string]bool {
7068
leafChanges := map[string]bool{}
71-
for _, change := range a.Changes {
69+
for _, change := range a {
7270
var leaf string
7371
parts := strings.Split(change, ".")
7472
leaf = parts[len(parts)-1]
@@ -80,17 +78,12 @@ func (a *AttributeChanges) leafChanges(removeIndex bool) map[string]bool {
8078
return leafChanges
8179
}
8280

83-
// FindAttributeChanges: Iterates through TFModel of state+plan and returns AttributeChanges for querying changed attributes
84-
// The implementation is similar to KeepUnknown, no support for types.Set or types.Tuple yet
85-
func FindAttributeChanges(ctx context.Context, src, dest any) AttributeChanges {
86-
changes := FindChanges(ctx, src, dest)
87-
return AttributeChanges{changes}
88-
}
89-
90-
func FindChanges(ctx context.Context, src, dest any) []string {
81+
// NewAttributeChanges iterates through TFModel of state+plan and returns AttributeChanges for querying changed attributes.
82+
// The implementation is similar to KeepUnknown, it doesn't support types.Set or types.Tuple yet.
83+
func NewAttributeChanges(ctx context.Context, src, dest any) AttributeChanges {
9184
valSrc, valDest := validateStructPointers(src, dest)
9285
typeDest := valDest.Type()
93-
changes := []string{} // Always return an empty list, as nested attributes might be added and then removed, which make the test cases fail on nil vs []
86+
changes := []string{} // Always return an empty list so AttributeChanges is not nil, also nested attributes might be added and then removed, which make the test cases fail on nil vs []
9487
for i := range typeDest.NumField() {
9588
fieldDest := typeDest.Field(i)
9689
name, tfName := fieldNameTFName(&fieldDest)

internal/common/schemafunc/find_changes_test.go

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ func TestFindChanges(t *testing.T) {
1212
tests := map[string]struct {
1313
src any
1414
dest any
15-
expected []string
15+
expected schemafunc.AttributeChanges
1616
}{
1717
"no changes": {
1818
src: &TFSimpleModel{Name: types.StringValue("name")},
@@ -118,15 +118,15 @@ func TestFindChanges(t *testing.T) {
118118
}
119119
for name, tc := range tests {
120120
t.Run(name, func(t *testing.T) {
121-
actual := schemafunc.FindChanges(ctx, tc.src, tc.dest)
121+
actual := schemafunc.NewAttributeChanges(ctx, tc.src, tc.dest)
122122
assert.Equal(t, tc.expected, actual)
123123
})
124124
}
125125
}
126126
func TestAttributeChanges_LeafChanges(t *testing.T) {
127127
tests := map[string]struct {
128128
expected map[string]bool
129-
changes []string
129+
changes schemafunc.AttributeChanges
130130
}{
131131
"empty changes": {
132132
changes: []string{},
@@ -173,8 +173,7 @@ func TestAttributeChanges_LeafChanges(t *testing.T) {
173173

174174
for name, tc := range tests {
175175
t.Run(name, func(t *testing.T) {
176-
ac := schemafunc.AttributeChanges{Changes: tc.changes}
177-
actual := ac.LeafChanges()
176+
actual := tc.changes.LeafChanges()
178177
assert.Equal(t, tc.expected, actual)
179178
})
180179
}
@@ -183,7 +182,7 @@ func TestAttributeChanges_LeafChanges(t *testing.T) {
183182
func TestAttributeChanges_AttributeChanged(t *testing.T) {
184183
tests := map[string]struct {
185184
attr string
186-
changes []string
185+
changes schemafunc.AttributeChanges
187186
expected bool
188187
}{
189188
"match found": {
@@ -210,15 +209,14 @@ func TestAttributeChanges_AttributeChanged(t *testing.T) {
210209

211210
for name, tc := range tests {
212211
t.Run(name, func(t *testing.T) {
213-
ac := schemafunc.AttributeChanges{Changes: tc.changes}
214-
actual := ac.AttributeChanged(tc.attr)
212+
actual := tc.changes.AttributeChanged(tc.attr)
215213
assert.Equal(t, tc.expected, actual)
216214
})
217215
}
218216
}
219217
func TestAttributeChanges_KeepUnknown(t *testing.T) {
220218
tests := map[string]struct {
221-
changes []string
219+
changes schemafunc.AttributeChanges
222220
attributeEffectedMapping map[string][]string
223221
expectedKeepUnknownAttrs []string
224222
}{
@@ -269,16 +267,15 @@ func TestAttributeChanges_KeepUnknown(t *testing.T) {
269267

270268
for name, tc := range tests {
271269
t.Run(name, func(t *testing.T) {
272-
ac := schemafunc.AttributeChanges{Changes: tc.changes}
273-
actual := ac.KeepUnknown(tc.attributeEffectedMapping)
270+
actual := tc.changes.KeepUnknown(tc.attributeEffectedMapping)
274271
assert.ElementsMatch(t, tc.expectedKeepUnknownAttrs, actual)
275272
})
276273
}
277274
}
278275
func TestAttributeChanges_ListLenChanges(t *testing.T) {
279276
tests := map[string]struct {
280277
name string
281-
changes []string
278+
changes schemafunc.AttributeChanges
282279
expected bool
283280
}{
284281
"empty changes": {
@@ -325,16 +322,15 @@ func TestAttributeChanges_ListLenChanges(t *testing.T) {
325322

326323
for name, tc := range tests {
327324
t.Run(name, func(t *testing.T) {
328-
ac := schemafunc.AttributeChanges{Changes: tc.changes}
329-
actual := ac.ListLenChanges(tc.name)
325+
actual := tc.changes.ListLenChanges(tc.name)
330326
assert.Equal(t, tc.expected, actual)
331327
})
332328
}
333329
}
334330
func TestAttributeChanges_ListIndexChanged(t *testing.T) {
335331
tests := map[string]struct {
336332
name string
337-
changes []string
333+
changes schemafunc.AttributeChanges
338334
index int
339335
expected bool
340336
}{
@@ -396,16 +392,15 @@ func TestAttributeChanges_ListIndexChanged(t *testing.T) {
396392

397393
for name, tc := range tests {
398394
t.Run(name, func(t *testing.T) {
399-
ac := schemafunc.AttributeChanges{Changes: tc.changes}
400-
actual := ac.ListIndexChanged(tc.name, tc.index)
395+
actual := tc.changes.ListIndexChanged(tc.name, tc.index)
401396
assert.Equal(t, tc.expected, actual)
402397
})
403398
}
404399
}
405400
func TestAttributeChanges_NestedListLenChanges(t *testing.T) {
406401
tests := map[string]struct {
407402
fullPath string
408-
changes []string
403+
changes schemafunc.AttributeChanges
409404
expected bool
410405
}{
411406
"empty changes": {
@@ -456,8 +451,7 @@ func TestAttributeChanges_NestedListLenChanges(t *testing.T) {
456451

457452
for name, tc := range tests {
458453
t.Run(name, func(t *testing.T) {
459-
ac := schemafunc.AttributeChanges{Changes: tc.changes}
460-
actual := ac.NestedListLenChanges(tc.fullPath)
454+
actual := tc.changes.NestedListLenChanges(tc.fullPath)
461455
assert.Equal(t, tc.expected, actual)
462456
})
463457
}

internal/service/advancedclustertpf/plan_modifier.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,10 @@ var (
3131
// useStateForUnknowns should be called only in Update, because of findClusterDiff
3232
func useStateForUnknowns(ctx context.Context, diags *diag.Diagnostics, state, plan *TFModel) {
3333
diff := findClusterDiff(ctx, state, plan, diags)
34-
if diags.HasError() {
35-
return
36-
}
37-
if diff.isAnyUpgrade() { // Don't do anything in upgrades
34+
if diags.HasError() || diff.isAnyUpgrade() { // Don't do anything in upgrades
3835
return
3936
}
40-
attributeChanges := schemafunc.FindAttributeChanges(ctx, state, plan)
37+
attributeChanges := schemafunc.NewAttributeChanges(ctx, state, plan)
4138
keepUnknown := []string{"connection_strings", "state_name"} // Volatile attributes, should not be copied from state
4239
keepUnknown = append(keepUnknown, attributeChanges.KeepUnknown(attributeRootChangeMapping)...)
4340
schemafunc.CopyUnknowns(ctx, state, plan, keepUnknown)

0 commit comments

Comments
 (0)