Skip to content

Commit 847897f

Browse files
committed
update: disallow null resource identities
1 parent 0a6415e commit 847897f

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

internal/fwserver/server_updateresource.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,17 @@ func (s *Server) UpdateResource(ctx context.Context, req *UpdateResourceRequest,
199199
}
200200
}
201201

202+
if req.IdentitySchema != nil {
203+
if resp.NewIdentity.Raw.IsFullyNull() {
204+
resp.Diagnostics.AddError(
205+
"Missing Resource Identity After Update",
206+
"The Terraform Provider unexpectedly returned no resource identity data after having no errors in the resource update. "+
207+
"This is always an issue in the Terraform Provider and should be reported to the provider developers.",
208+
)
209+
return
210+
}
211+
}
212+
202213
semanticEqualityReq := SchemaSemanticEqualityRequest{
203214
PriorData: fwschemadata.Data{
204215
Description: fwschemadata.DataDescriptionPlan,

internal/fwserver/server_updateresource_test.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -927,6 +927,58 @@ func TestServerUpdateResource(t *testing.T) {
927927
Private: testEmptyPrivate,
928928
},
929929
},
930+
"response-new-identity-nil": {
931+
server: &fwserver.Server{
932+
Provider: &testprovider.Provider{},
933+
},
934+
request: &fwserver.UpdateResourceRequest{
935+
PlannedState: &tfsdk.Plan{
936+
Raw: tftypes.NewValue(testSchemaType, map[string]tftypes.Value{
937+
"test_computed": tftypes.NewValue(tftypes.String, nil),
938+
"test_required": tftypes.NewValue(tftypes.String, "test-plannedstate-value"),
939+
}),
940+
Schema: testSchema,
941+
},
942+
PlannedIdentity: &tfsdk.ResourceIdentity{
943+
Raw: tftypes.NewValue(testIdentityType, nil),
944+
Schema: testIdentitySchema,
945+
},
946+
IdentitySchema: testIdentitySchema,
947+
ResourceSchema: testSchema,
948+
Resource: &testprovider.ResourceWithIdentity{
949+
Resource: &testprovider.Resource{
950+
UpdateMethod: func(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
951+
var data testSchemaData
952+
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
953+
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
954+
955+
resp.Identity.Raw = tftypes.NewValue(testIdentityType, nil)
956+
957+
},
958+
},
959+
},
960+
},
961+
expectedResponse: &fwserver.UpdateResourceResponse{
962+
Diagnostics: []diag.Diagnostic{
963+
diag.NewErrorDiagnostic(
964+
"Missing Resource Identity After Update",
965+
"The Terraform Provider unexpectedly returned no resource identity data after having no errors in the resource update. This is always an issue in the Terraform Provider and should be reported to the provider developers.",
966+
),
967+
},
968+
NewIdentity: &tfsdk.ResourceIdentity{
969+
Raw: tftypes.NewValue(testIdentityType, nil),
970+
Schema: testIdentitySchema,
971+
},
972+
NewState: &tfsdk.State{
973+
Raw: tftypes.NewValue(testSchemaType, map[string]tftypes.Value{
974+
"test_computed": tftypes.NewValue(tftypes.String, nil),
975+
"test_required": tftypes.NewValue(tftypes.String, "test-plannedstate-value"),
976+
}),
977+
Schema: testSchema,
978+
},
979+
Private: testEmptyPrivate,
980+
},
981+
},
930982
"response-new-identity-null": {
931983
server: &fwserver.Server{
932984
Provider: &testprovider.Provider{},
@@ -963,6 +1015,12 @@ func TestServerUpdateResource(t *testing.T) {
9631015
},
9641016
},
9651017
expectedResponse: &fwserver.UpdateResourceResponse{
1018+
Diagnostics: []diag.Diagnostic{
1019+
diag.NewErrorDiagnostic(
1020+
"Missing Resource Identity After Update",
1021+
"The Terraform Provider unexpectedly returned no resource identity data after having no errors in the resource update. This is always an issue in the Terraform Provider and should be reported to the provider developers.",
1022+
),
1023+
},
9661024
NewIdentity: &tfsdk.ResourceIdentity{
9671025
Raw: tftypes.NewValue(testIdentityType, map[string]tftypes.Value{
9681026
"test_id": tftypes.NewValue(tftypes.String, nil),

0 commit comments

Comments
 (0)