Skip to content

Commit

Permalink
Ignore length of computed nested blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
dginty4 committed Sep 13, 2024
1 parent 39817f6 commit 15ceeda
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 3 deletions.
22 changes: 20 additions & 2 deletions genesyscloud/consistency_checker/consistency_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type ConsistencyCheck struct {
checks int
maxStateChecks int
resourceType string
computedBlocks []string
}

type consistencyError struct {
Expand Down Expand Up @@ -86,6 +87,11 @@ func NewConsistencyCheck(ctx context.Context, d *schema.ResourceData, meta inter
resourceType: resourceType,
}

// Find any computed nested blocks
var computedBlocks []string
populateComputedBlocks(cc.resource, &computedBlocks, "")
cc.computedBlocks = computedBlocks

mccMutex.Lock()
defer mccMutex.Unlock()
mcc[d.Id()] = cc
Expand Down Expand Up @@ -144,8 +150,20 @@ func (cc *ConsistencyCheck) CheckState(currentState *schema.ResourceData) *retry
continue
}

// Handle top level attributes and check we have the same number of nested blocks
if !strings.Contains(attribute, ".") || strings.HasSuffix(attribute, "#") {
// Check we have the same number of nested blocks
if strings.HasSuffix(attribute, "#") {
// Don't check the length of computed blocks
if isComputedBlock(cc.computedBlocks, strings.TrimSuffix(attribute, ".#")) {
continue
}

if cc.originalStateValues[attribute] != currentStateValues[attribute] {
return cc.handleError(attribute, cc.originalStateValues[attribute], currentStateValues[attribute])
}
}

// Handle top level attributes
if !strings.Contains(attribute, ".") {
if cc.originalStateValues[attribute] != currentStateValues[attribute] {
return cc.handleError(attribute, cc.originalStateValues[attribute], currentStateValues[attribute])
}
Expand Down
48 changes: 47 additions & 1 deletion genesyscloud/consistency_checker/consistency_checker_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,32 @@ func isEmptyState(d *schema.ResourceData) *bool {
return &isEmpty
}

// populateComputedBlocks will find any sets or lists in a resource marked as computed
func populateComputedBlocks(resource *schema.Resource, blocks *[]string, parent string) {
for attr, attrSchema := range resource.Schema {
if attrSchema.Type == schema.TypeSet || attrSchema.Type == schema.TypeList {
var fullName string
if parent == "" {
fullName = attr
} else {
fullName = parent + "." + attr
}

if attrSchema.Computed == true {
*blocks = append(*blocks, fullName)
}

switch elem := attrSchema.Elem.(type) {
case *schema.Resource:
populateComputedBlocks(elem, blocks, fullName)
default:
continue
}

}
}
}

// compareStateMaps compares two state maps and returns true if they are equal, accounting for re-ordering blocks
func compareStateMaps(originalState, currentState map[string]interface{}) bool {
return compareValues(originalState, currentState)
Expand Down Expand Up @@ -145,4 +171,24 @@ func compareSlices(slice1, slice2 []interface{}) bool {
})

return reflect.DeepEqual(sortedSlice1, sortedSlice2)
}
}

func isComputedBlock(computedBlocks []string, attribute string) bool {
// Convert attribute from <attr1>.x.<attr2> to <attr1>.<attr2> before comparing
attrParts := strings.Split(attribute, ".")
var cleanAttrName string
for i := range attrParts {
if i%2 == 0 {
cleanAttrName = cleanAttrName + "." + attrParts[i]
}
}
cleanAttrName = strings.TrimPrefix(cleanAttrName, ".")

for _, computedBlock := range computedBlocks {
if computedBlock == cleanAttrName {
return true
}
}

return false
}

0 comments on commit 15ceeda

Please sign in to comment.