hashicorp / hashicorp/terraform-plugin-sdk

In CustomizedDiff the 2nd return value of d.GetChange on a nested block (TypeSet) doesn't contain inner nested block

Open
#497 3 comments 3 reactions 0 assignees View on GitHub
bug shims/maybe subsystem/types
Dominant language
Go
Stars
485
Forks
244
Avg merge
19h 57m
Merged PRs (30d)
4

Description

### SDK version

```
v1.6.0 & v1.13.0
```

### Relevant provider source code

Schema definition:

```go
"foo": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"a": {
Type: schema.TypeString,
Required: true,
},

"b": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"x": {
Type: schema.TypeBool,
Required: true,
},
},
},
},
},
},
}
```

Then define a `CustomizeDiff`, which in turns call `d.GetChange("foo")`.

### Expected Behavior

The returned "new" value of `foo` should contain the new state defined in terraform configuration.

### Actual Behavior

The returned "new" value has `foo.b` with one element: `nil`.

### Addtional Findings

I have debugged a bit on v1.6.0, and find the cause is that in `readListField()` it will modify the `addrPadded` to change the hash index of Set into a list index, which in turns will change affect the out address (as slice is passed by pointer in Go). In later step when `d.GetChange` wants to get the value from diff for an address, it can't find the value, hence setting it to `nil`.

The fix is simply copy the `addrPadded` to a temporary list before calling:

```go
countResult, err := r.ReadField(addrPadded)
```

in file **field_reader.go** in `readListField()`.

I suspect the same fix should be applied to the v1-maint branch.

Contributor guide

Open the contributing guide

Research direction

Start in field_reader.go at readListField() and trace how addrPadded is passed to ReadField. Reproduce the nested TypeSet CustomizeDiff case from the issue, then verify that d.GetChange("foo") returns the configured nested b value rather than nil; also check whether the v1-maint branch has the same behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
go, terraform
Domain
backend-api-design, devtools
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.