Skip to content

Commit 827b6fe

Browse files
committed
Added a response-invalid-identity test to test invalid identity responses
1 parent f81326f commit 827b6fe

File tree

4 files changed

+51
-15
lines changed

4 files changed

+51
-15
lines changed

internal/fwserver/server_moveresourcestate.go

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -226,16 +226,6 @@ func (s *Server) MoveResourceState(ctx context.Context, req *MoveResourceStateRe
226226
return
227227
}
228228

229-
if resp.TargetIdentity != nil && req.IdentitySchema == nil {
230-
resp.Diagnostics.AddError(
231-
"Unexpected Move State Response",
232-
"An unexpected error was encountered when creating the move state response. New identity data was returned by the provider move state operation, but the resource does not indicate identity support.\n\n"+
233-
"This is always a problem with the provider and should be reported to the provider developer.",
234-
)
235-
236-
return
237-
}
238-
239229
// Set any write-only attributes in the move resource state to null
240230
modifiedState, err := tftypes.Transform(moveStateResp.TargetState.Raw, NullifyWriteOnlyAttributes(ctx, moveStateResp.TargetState.Schema))
241231
if err != nil {
@@ -271,8 +261,6 @@ func (s *Server) MoveResourceState(ctx context.Context, req *MoveResourceStateRe
271261
"Source Provider Address: "+req.SourceProviderAddress+"\n"+
272262
"Source Resource Type: "+req.SourceTypeName+"\n"+
273263
"Source Resource Schema Version: "+strconv.FormatInt(req.SourceSchemaVersion, 10)+"\n"+
274-
"Target Resource Type: "+req.TargetTypeName, //+"\n"+
275-
// "Source Identity Schema Version: "+strconv.FormatInt(req.SourceSchemaVersion, 10),
276-
264+
"Target Resource Type: "+req.TargetTypeName,
277265
)
278266
}

internal/fwserver/server_moveresourcestate_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -927,6 +927,45 @@ func TestServerMoveResourceState(t *testing.T) {
927927
},
928928
},
929929
},
930+
"response-invalid-identity": {
931+
server: &fwserver.Server{
932+
Provider: &testprovider.Provider{},
933+
},
934+
request: &fwserver.MoveResourceStateRequest{
935+
SourceRawState: testNewRawState(t, map[string]interface{}{
936+
"id": "test-id-value",
937+
"required_attribute": true,
938+
}),
939+
TargetResource: &testprovider.ResourceWithMoveState{
940+
MoveStateMethod: func(ctx context.Context) []resource.StateMover {
941+
return []resource.StateMover{
942+
{
943+
StateMover: func(_ context.Context, req resource.MoveStateRequest, resp *resource.MoveStateResponse) {
944+
// Try to set TargetIdentity even though req.IdentitySchema is nil
945+
resp.Diagnostics.Append(resp.TargetIdentity.SetAttribute(ctx, path.Root("test_id"), "should-not-be-set")...)
946+
947+
// Prevent missing implementation error, the values do not matter except for response assertion
948+
resp.Diagnostics.Append(resp.TargetState.SetAttribute(ctx, path.Root("id"), "test-id-value")...)
949+
resp.Diagnostics.Append(resp.TargetState.SetAttribute(ctx, path.Root("required_attribute"), "true")...)
950+
},
951+
},
952+
}
953+
},
954+
},
955+
TargetResourceSchema: testSchema,
956+
TargetTypeName: "test_resource",
957+
},
958+
expectedResponse: &fwserver.MoveResourceStateResponse{
959+
Diagnostics: diag.Diagnostics{
960+
diag.NewErrorDiagnostic(
961+
"Unexpected Move State Response",
962+
"An unexpected error was encountered when creating the move state response. Identity data was returned by the provider move state operation, but the resource does not indicate identity support.\n\n"+
963+
"This is always a problem with the provider and should be reported to the provider developer.",
964+
),
965+
},
966+
TargetPrivate: privatestate.EmptyData(ctx),
967+
},
968+
},
930969
}
931970

932971
for name, testCase := range testCases {

internal/toproto5/resource_identity.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414

1515
// ResourceIdentity returns the *tfprotov5.ResourceIdentityData for a *tfsdk.ResourceIdentity.
1616
func ResourceIdentity(ctx context.Context, fw *tfsdk.ResourceIdentity) (*tfprotov5.ResourceIdentityData, diag.Diagnostics) {
17-
if fw == nil || fw.Schema == nil {
17+
if fw == nil {
1818
return nil, nil
1919
}
2020

tfsdk/resource_identity.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ package tfsdk
55

66
import (
77
"context"
8-
98
"github.com/hashicorp/terraform-plugin-framework/diag"
109
"github.com/hashicorp/terraform-plugin-framework/internal/fwschema"
1110
"github.com/hashicorp/terraform-plugin-framework/internal/fwschemadata"
@@ -70,6 +69,16 @@ func (s *ResourceIdentity) Set(ctx context.Context, val interface{}) diag.Diagno
7069
//
7170
// Lists can only have the next element added according to the current length.
7271
func (s *ResourceIdentity) SetAttribute(ctx context.Context, path path.Path, val interface{}) diag.Diagnostics {
72+
// If s is nil, then calling s.data triggers a nil pointer error so we return the error diag here
73+
if s == nil {
74+
return diag.Diagnostics{
75+
diag.NewErrorDiagnostic(
76+
"Unexpected Move State Response",
77+
"An unexpected error was encountered when creating the move state response. Identity data was returned by the provider move state operation, but the resource does not indicate identity support.\n\n"+
78+
"This is always a problem with the provider and should be reported to the provider developer."),
79+
}
80+
}
81+
7382
data := s.data()
7483
diags := data.SetAtPath(ctx, path, val)
7584

0 commit comments

Comments
 (0)