Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backport of plan renderer: fix crash when updating a null attribute to unknown into v1.9 #35712

Merged
merged 1 commit into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions internal/command/jsonformat/computed/renderers/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func (renderer blockRenderer) RenderHuman(diff computed.Diff, indent int, opts c
sort.Strings(keys)

if renderer.blocks.UnknownBlocks[key] {
renderBlock(computed.NewDiff(Unknown(computed.Diff{}), plans.Create, false), "", opts)
renderBlock(computed.NewDiff(Unknown(computed.Diff{}), diff.Action, false), "", opts)
}

for _, innerKey := range keys {
Expand All @@ -170,7 +170,7 @@ func (renderer blockRenderer) RenderHuman(diff computed.Diff, indent int, opts c
setOpts.ForceForcesReplacement = diff.Replace

if renderer.blocks.UnknownBlocks[key] {
renderBlock(computed.NewDiff(Unknown(computed.Diff{}), plans.Create, false), "", opts)
renderBlock(computed.NewDiff(Unknown(computed.Diff{}), diff.Action, false), "", opts)
}

for _, block := range renderer.blocks.SetBlocks[key] {
Expand All @@ -179,7 +179,7 @@ func (renderer blockRenderer) RenderHuman(diff computed.Diff, indent int, opts c
case renderer.blocks.IsListBlock(key):

if renderer.blocks.UnknownBlocks[key] {
renderBlock(computed.NewDiff(Unknown(computed.Diff{}), plans.Create, false), "", opts)
renderBlock(computed.NewDiff(Unknown(computed.Diff{}), diff.Action, false), "", opts)
}

for _, block := range renderer.blocks.ListBlocks[key] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,13 @@ jsonencode(
},
expected: "0 -> (known after apply)",
},
"computed_update_from_null": {
diff: computed.Diff{
Renderer: Unknown(computed.Diff{}),
Action: plans.Update,
},
expected: "(known after apply)",
},
"computed_create_forces_replacement": {
diff: computed.Diff{
Renderer: Unknown(computed.Diff{}),
Expand Down
7 changes: 6 additions & 1 deletion internal/command/jsonformat/computed/renderers/unknown.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ type unknownRenderer struct {
}

func (renderer unknownRenderer) RenderHuman(diff computed.Diff, indent int, opts computed.RenderHumanOpts) string {
if diff.Action == plans.Create {

// the before renderer can be nil and not a create action when the provider
// previously returned a null value for the computed attribute and is now
// declaring they will recompute it as part of the next update.

if diff.Action == plans.Create || renderer.before.Renderer == nil {
return fmt.Sprintf("(known after apply)%s", forcesReplacement(diff.Replace, opts))
}

Expand Down
12 changes: 6 additions & 6 deletions internal/command/jsonformat/plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3474,7 +3474,7 @@ func TestResourceChange_nestedList(t *testing.T) {
id = "i-02ae66f368e8518a9"
# (1 unchanged attribute hidden)

+ root_block_device (known after apply)
~ root_block_device (known after apply)
- root_block_device {
- new_field = "new_value" -> null
- volume_type = "gp1" -> null
Expand Down Expand Up @@ -3533,7 +3533,7 @@ func TestResourceChange_nestedList(t *testing.T) {
id = "i-02ae66f368e8518a9"
# (1 unchanged attribute hidden)

+ root_block_device (known after apply)
~ root_block_device (known after apply)
- root_block_device {
- new_field = "new_value" -> null
- volume_type = "gp1" -> null # forces replacement
Expand Down Expand Up @@ -4201,7 +4201,7 @@ func TestResourceChange_nestedSet(t *testing.T) {
id = "i-02ae66f368e8518a9"
# (1 unchanged attribute hidden)

+ root_block_device (known after apply)
~ root_block_device (known after apply)
- root_block_device {
- new_field = "new_value" -> null
- volume_type = "gp1" -> null
Expand Down Expand Up @@ -4260,7 +4260,7 @@ func TestResourceChange_nestedSet(t *testing.T) {
id = "i-02ae66f368e8518a9"
# (1 unchanged attribute hidden)

+ root_block_device (known after apply)
~ root_block_device (known after apply)
- root_block_device {
- new_field = "new_value" -> null
- volume_type = "gp1" -> null # forces replacement
Expand Down Expand Up @@ -4731,7 +4731,7 @@ func TestResourceChange_nestedMap(t *testing.T) {
id = "i-02ae66f368e8518a9"
# (1 unchanged attribute hidden)

+ root_block_device (known after apply)
~ root_block_device (known after apply)
- root_block_device "gp1" {
- new_field = "new_value" -> null
- volume_type = "gp1" -> null
Expand Down Expand Up @@ -4790,7 +4790,7 @@ func TestResourceChange_nestedMap(t *testing.T) {
id = "i-02ae66f368e8518a9"
# (1 unchanged attribute hidden)

+ root_block_device (known after apply)
~ root_block_device (known after apply)
- root_block_device "gp1" {
- new_field = "new_value" -> null
- volume_type = "gp1" -> null # forces replacement
Expand Down
Loading