Skip to content

Conversation

Guent4
Copy link

@Guent4 Guent4 commented Sep 6, 2025

Summary

Enhanced the getDelta function to support ignoreList functionality when comparing slices containing map elements. Previously, slice comparisons only supported ignoring the entire slice or performed simple deep equality checks. Now, specific fields within slice elements can be ignored using dot notation.

Key Changes

Core Implementation

  • Updated slice handling in getDelta: Modified slice comparison logic to support ignoreList for map elements within slices
  • Added compareSlicesWithIgnoreListReflection: New function using Go's reflection package to handle different slice types ([]map[string]interface{}, []interface{}, etc.) while preserving type information
  • Fixed _descendIgnoreList bug: Corrected array pre-allocation + append issue that was creating empty entries in ignore lists

Behavior & Rules

  • Dot notation support: Use list.field syntax to ignore field in all elements of list
  • Length changes always detected: Different slice lengths are always considered changes, ignoreList only applies to corresponding elements at same indices
  • Type preservation: Maintains original slice types in the modified result
  • Map-only ignoring: ignoreList only applies to slice elements that are maps; primitive elements use standard equality comparison

Enhanced Testing

  • Added comprehensive test cases: New test scenarios for slice ignoreList functionality including:
    • Ignored fields in slice elements (list.val ignores val field in all list elements)
    • Partial ignoring (some fields ignored, others updated)
    • Mixed slice types with maps and primitives
    • Different slice lengths (always detected as changes)
    • Type preservation validation
  • Enhanced TestHasDeltaModifiedResource: Added slice with ignoreList to existing test to validate complete functionality

Examples

Before (not supported)

o1 := {"users": [{"name": "alice", "secret": "old"}, {"name": "bob", "secret": "old"}]}
o2 := {"users": [{"name": "alice", "secret": "new"}, {"name": "bob", "secret": "new"}]}
ignoreList := []string{"users.secret"}
// Would detect changes even though only ignored fields changed

After (now supported)

o1 := {"users": [{"name": "alice", "secret": "old"}, {"name": "bob", "secret": "old"}]}  
o2 := {"users": [{"name": "alice", "secret": "new"}, {"name": "bob", "secret": "new"}]}
ignoreList := []string{"users.secret"}
// Returns: hasChanges = false, modified preserves original secret values

@Guent4 Guent4 changed the title #322: Add support for lists in ignore_changes_to Fixes #322: Add support for lists in ignore_changes_to Sep 6, 2025
@Guent4
Copy link
Author

Guent4 commented Sep 10, 2025

Published the build of the fork to https://registry.terraform.io/providers/Guent4/restapi/2.1.0 and verified that it works with my use case.

Data is in the format of:

{
  "prefixes": [
    {
      "prefix": "<CIDR>",
      "family": <FAMILY>,
      "status": "<STATUS>",
      "role": "<ROLE>",
      "description": "<DESCRIPTION>",
      "tags": [],
      "prefix_role": {
        ...
      },
      "custom_fields": {
        ...
      },
      "id": 61999,
      "created_at": "<TIME>",
      "updated_at": "<TIME>"
    }
  ],
  "total_results": 1
}

When I set

  ignore_changes_to = [
    "changed_by",
    "prefixes.created_at",
    "prefixes.custom_fields",
    "prefixes.family",
    "prefixes.id",
    "prefixes.prefix_role",
    "prefixes.updated_at",
    "total_results",
  ]

, changes corresponding to the fields, most of which are auto-generated/auto-updated, are properly ignored by the Terraform resource.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant