Skip to content

Commit c6611a4

Browse files
authored
Handle diags from gRPC state upgrader (#2053)
Isolating this from an AWS test failure; handling diags turns a panic into a regular error. Looks like the error message is still unfortunate. The test is isolated from pulumi/pulumi-aws#4015
1 parent b5efa0d commit c6611a4

File tree

3 files changed

+70
-14
lines changed

3 files changed

+70
-14
lines changed

pkg/tfshim/sdk-v2/provider2.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ type grpcServer struct {
353353
// This will return an error if any of the diagnostics are error-level, or a given err is non-nil.
354354
// It will also logs the diagnostics into TF loggers, so they appear when debugging with the bridged
355355
// provider with TF_LOG=TRACE or similar.
356-
func (s *grpcServer) handle(ctx context.Context, diags []*tfprotov5.Diagnostic, err error) error {
356+
func handleDiagnostics(ctx context.Context, diags []*tfprotov5.Diagnostic, err error) error {
357357
var dd diag.Diagnostics
358358
for _, d := range diags {
359359
if d == nil {
@@ -430,7 +430,7 @@ func (s *grpcServer) PlanResourceChange(
430430
req.ProviderMeta = &tfprotov5.DynamicValue{MsgPack: providerMetaVal}
431431
}
432432
resp, err := s.gserver.PlanResourceChangeExtra(ctx, req)
433-
if err := s.handle(ctx, resp.Diagnostics, err); err != nil {
433+
if err := handleDiagnostics(ctx, resp.Diagnostics, err); err != nil {
434434
return nil, err
435435
}
436436
// Ignore resp.UnsafeToUseLegacyTypeSystem - does not matter for Pulumi bridged providers.
@@ -508,7 +508,7 @@ func (s *grpcServer) ApplyResourceChange(
508508
req.ProviderMeta = &tfprotov5.DynamicValue{MsgPack: providerMetaVal}
509509
}
510510
resp, err := s.gserver.ApplyResourceChange(ctx, req)
511-
if err := s.handle(ctx, resp.Diagnostics, err); err != nil {
511+
if err := handleDiagnostics(ctx, resp.Diagnostics, err); err != nil {
512512
return nil, err
513513
}
514514
newState, err := msgpack.Unmarshal(resp.NewState.MsgPack, ty)
@@ -559,7 +559,7 @@ func (s *grpcServer) ReadResource(
559559
req.ProviderMeta = &tfprotov5.DynamicValue{MsgPack: providerMetaVal}
560560
}
561561
resp, err := s.gserver.ReadResource(ctx, req)
562-
if err := s.handle(ctx, resp.Diagnostics, err); err != nil {
562+
if err := handleDiagnostics(ctx, resp.Diagnostics, err); err != nil {
563563
return nil, err
564564
}
565565
newState, err := msgpack.Unmarshal(resp.NewState.MsgPack, ty)
@@ -590,7 +590,7 @@ func (s *grpcServer) ImportResourceState(
590590
ID: id,
591591
}
592592
resp, err := s.gserver.ImportResourceState(ctx, req)
593-
if err := s.handle(ctx, resp.Diagnostics, err); err != nil {
593+
if err := handleDiagnostics(ctx, resp.Diagnostics, err); err != nil {
594594
return nil, err
595595
}
596596
out := []v2InstanceState2{}

pkg/tfshim/sdk-v2/provider2_test.go

Lines changed: 63 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@ import (
1414
func TestProvider2UpgradeResourceState(t *testing.T) {
1515
const tfToken = "test_token"
1616
for _, tc := range []struct {
17-
name string
18-
state cty.Value
19-
rSchema *schema.Resource
20-
expected cty.Value
17+
name string
18+
state cty.Value
19+
rSchema *schema.Resource
20+
expected cty.Value
21+
expectErr func(*testing.T, error)
2122
}{
2223
{
2324
name: "no upgrade",
@@ -213,6 +214,58 @@ func TestProvider2UpgradeResourceState(t *testing.T) {
213214
},
214215
},
215216
},
217+
{
218+
name: "handle errors",
219+
state: func() cty.Value {
220+
return cty.ObjectVal(map[string]cty.Value{
221+
"compute_resources": cty.ObjectVal(map[string]cty.Value{
222+
"ec2_configuration": cty.ObjectVal(map[string]cty.Value{
223+
"image_id_override": cty.StringVal("override"),
224+
}),
225+
}),
226+
"id": cty.StringVal("id"),
227+
})
228+
}(),
229+
expectErr: func(t *testing.T, err error) {
230+
require.Error(t, err)
231+
require.ErrorContains(t, err, "missing expected [")
232+
},
233+
rSchema: &schema.Resource{
234+
Schema: map[string]*schema.Schema{
235+
"compute_resources": {
236+
Type: schema.TypeList,
237+
Optional: true,
238+
ForceNew: true,
239+
MinItems: 0,
240+
MaxItems: 1,
241+
Elem: &schema.Resource{
242+
Schema: map[string]*schema.Schema{
243+
"ec2_configuration": {
244+
Type: schema.TypeList,
245+
Optional: true,
246+
Computed: true,
247+
ForceNew: true,
248+
MaxItems: 2,
249+
Elem: &schema.Resource{
250+
Schema: map[string]*schema.Schema{
251+
"image_id_override": {
252+
Type: schema.TypeString,
253+
Optional: true,
254+
Computed: true,
255+
},
256+
"image_type": {
257+
Type: schema.TypeString,
258+
Optional: true,
259+
},
260+
},
261+
},
262+
},
263+
},
264+
},
265+
},
266+
},
267+
},
268+
},
216269
} {
217270
t.Run(tc.name, func(t *testing.T) {
218271
tf := &schema.Provider{
@@ -227,9 +280,12 @@ func TestProvider2UpgradeResourceState(t *testing.T) {
227280
resourceType: tfToken,
228281
stateValue: tc.state,
229282
})
230-
require.NoError(t, err)
231-
232-
assert.Equal(t, tc.expected, actual.(*v2InstanceState2).stateValue)
283+
if tc.expectErr != nil {
284+
tc.expectErr(t, err)
285+
} else {
286+
require.NoError(t, err)
287+
assert.Equal(t, tc.expected, actual.(*v2InstanceState2).stateValue)
288+
}
233289
})
234290
}
235291
}

pkg/tfshim/sdk-v2/upgrade_state.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ func upgradeResourceStateGRPC(
4949
RawState: &tfprotov5.RawState{JSON: jsonBytes},
5050
Version: version,
5151
})
52-
if err != nil {
53-
return cty.Value{}, nil, err
52+
if uerr := handleDiagnostics(ctx, resp.Diagnostics, err); uerr != nil {
53+
return cty.Value{}, nil, uerr
5454
}
5555

5656
newState, err := msgpack.Unmarshal(resp.UpgradedState.MsgPack, res.CoreConfigSchema().ImpliedType())

0 commit comments

Comments
 (0)