From dd9c0ab98e72e1dc83c8f9ee6804f72d494dc30e Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Wed, 18 Oct 2023 10:00:09 -0700 Subject: [PATCH] rpcapi(Stacks): don't send empty moved/import messages The "progress hooks" mechanism was unconditionally allocating messages to represent the moved and imported cases and then conditionally populating the fields inside. That causes an admittedly-small friction on the client side because they need to test whether the string fields inside are non-empty instead of just checking whether the overall message field was populated at all. To smooth that for clients, we'll instead leave these unset unless there's actually something to report. For clients written in Go, that means that the top-level fields will be nil when not relevant, which is easy to test for and relatively idiomatic. --- internal/rpcapi/stacks.go | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/internal/rpcapi/stacks.go b/internal/rpcapi/stacks.go index 5306a07307..01f5ca5c4e 100644 --- a/internal/rpcapi/stacks.go +++ b/internal/rpcapi/stacks.go @@ -614,17 +614,21 @@ func stackChangeHooks(send func(*terraform1.StackChangeProgress) error, mainStac attribute.String("resource_instance", ric.Addr.Item.String()), )) - moved := &terraform1.StackChangeProgress_ResourceInstancePlannedChange_Moved{} + var moved *terraform1.StackChangeProgress_ResourceInstancePlannedChange_Moved if !ric.Change.PrevRunAddr.Equal(ric.Change.Addr) { - moved.PrevAddr = &terraform1.ResourceInstanceInStackAddr{ - ComponentInstanceAddr: ric.Addr.Component.String(), - ResourceInstanceAddr: ric.Change.PrevRunAddr.String(), + moved = &terraform1.StackChangeProgress_ResourceInstancePlannedChange_Moved{ + PrevAddr: &terraform1.ResourceInstanceInStackAddr{ + ComponentInstanceAddr: ric.Addr.Component.String(), + ResourceInstanceAddr: ric.Change.PrevRunAddr.String(), + }, } } - imported := &terraform1.StackChangeProgress_ResourceInstancePlannedChange_Imported{} + var imported *terraform1.StackChangeProgress_ResourceInstancePlannedChange_Imported if ric.Change.Importing != nil { - imported.ImportId = ric.Change.Importing.ID + imported = &terraform1.StackChangeProgress_ResourceInstancePlannedChange_Imported{ + ImportId: ric.Change.Importing.ID, + } } send(&terraform1.StackChangeProgress{