Skip to content

Add resource identity to Move RPC #1123

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

Merged
merged 14 commits into from
Apr 21, 2025
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
20 changes: 11 additions & 9 deletions internal/fromproto5/moveresourcestate.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package fromproto5

import (
"context"

"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/internal/fwschema"
"github.com/hashicorp/terraform-plugin-framework/internal/fwserver"
Expand All @@ -17,7 +16,7 @@ import (

// MoveResourceStateRequest returns the *fwserver.MoveResourceStateRequest
// equivalent of a *tfprotov5.MoveResourceStateRequest.
func MoveResourceStateRequest(ctx context.Context, proto5 *tfprotov5.MoveResourceStateRequest, resource resource.Resource, resourceSchema fwschema.Schema) (*fwserver.MoveResourceStateRequest, diag.Diagnostics) {
func MoveResourceStateRequest(ctx context.Context, proto5 *tfprotov5.MoveResourceStateRequest, resource resource.Resource, resourceSchema fwschema.Schema, identitySchema fwschema.Schema) (*fwserver.MoveResourceStateRequest, diag.Diagnostics) {
if proto5 == nil {
return nil, nil
}
Expand All @@ -38,13 +37,16 @@ func MoveResourceStateRequest(ctx context.Context, proto5 *tfprotov5.MoveResourc
}

fw := &fwserver.MoveResourceStateRequest{
SourceProviderAddress: proto5.SourceProviderAddress,
SourceRawState: (*tfprotov6.RawState)(proto5.SourceState),
SourceSchemaVersion: proto5.SourceSchemaVersion,
SourceTypeName: proto5.SourceTypeName,
TargetResource: resource,
TargetResourceSchema: resourceSchema,
TargetTypeName: proto5.TargetTypeName,
SourceProviderAddress: proto5.SourceProviderAddress,
SourceRawState: (*tfprotov6.RawState)(proto5.SourceState),
SourceSchemaVersion: proto5.SourceSchemaVersion,
SourceTypeName: proto5.SourceTypeName,
TargetResource: resource,
TargetResourceSchema: resourceSchema,
TargetTypeName: proto5.TargetTypeName,
SourceIdentity: (*tfprotov6.RawState)(proto5.SourceIdentity),
SourceIdentitySchemaVersion: proto5.SourceIdentitySchemaVersion,
IdentitySchema: identitySchema,
}

sourcePrivate, sourcePrivateDiags := privatestate.NewData(ctx, proto5.SourcePrivate)
Expand Down
27 changes: 26 additions & 1 deletion internal/fromproto5/moveresourcestate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func TestMoveResourceStateRequest(t *testing.T) {
testCases := map[string]struct {
input *tfprotov5.MoveResourceStateRequest
resourceSchema fwschema.Schema
identitySchema fwschema.Schema
resource resource.Resource
expected *fwserver.MoveResourceStateRequest
expectedDiagnostics diag.Diagnostics
Expand Down Expand Up @@ -162,13 +163,37 @@ func TestMoveResourceStateRequest(t *testing.T) {
TargetTypeName: "examplecloud_thing",
},
},
"SourceIdentity": {
input: &tfprotov5.MoveResourceStateRequest{
SourceIdentity: testNewTfprotov5RawState(t, map[string]interface{}{
"test_identity_attribute": "test-value",
}),
},
resourceSchema: testFwSchema,
expected: &fwserver.MoveResourceStateRequest{
SourceIdentity: testNewTfprotov6RawState(t, map[string]interface{}{
"test_identity_attribute": "test-value",
}),
TargetResourceSchema: testFwSchema,
},
},
"SourceIdentitySchemaVersion": {
input: &tfprotov5.MoveResourceStateRequest{
SourceIdentitySchemaVersion: 123,
},
resourceSchema: testFwSchema,
expected: &fwserver.MoveResourceStateRequest{
SourceIdentitySchemaVersion: 123,
TargetResourceSchema: testFwSchema,
},
},
}

for name, testCase := range testCases {
t.Run(name, func(t *testing.T) {
t.Parallel()

got, diags := fromproto5.MoveResourceStateRequest(context.Background(), testCase.input, testCase.resource, testCase.resourceSchema)
got, diags := fromproto5.MoveResourceStateRequest(context.Background(), testCase.input, testCase.resource, testCase.resourceSchema, testCase.identitySchema)

if diff := cmp.Diff(got, testCase.expected); diff != "" {
t.Errorf("unexpected difference: %s", diff)
Expand Down
20 changes: 11 additions & 9 deletions internal/fromproto6/moveresourcestate.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package fromproto6

import (
"context"

"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/internal/fwschema"
"github.com/hashicorp/terraform-plugin-framework/internal/fwserver"
Expand All @@ -16,7 +15,7 @@ import (

// MoveResourceStateRequest returns the *fwserver.MoveResourceStateRequest
// equivalent of a *tfprotov6.MoveResourceStateRequest.
func MoveResourceStateRequest(ctx context.Context, proto6 *tfprotov6.MoveResourceStateRequest, resource resource.Resource, resourceSchema fwschema.Schema) (*fwserver.MoveResourceStateRequest, diag.Diagnostics) {
func MoveResourceStateRequest(ctx context.Context, proto6 *tfprotov6.MoveResourceStateRequest, resource resource.Resource, resourceSchema fwschema.Schema, identitySchema fwschema.Schema) (*fwserver.MoveResourceStateRequest, diag.Diagnostics) {
if proto6 == nil {
return nil, nil
}
Expand All @@ -37,13 +36,16 @@ func MoveResourceStateRequest(ctx context.Context, proto6 *tfprotov6.MoveResourc
}

fw := &fwserver.MoveResourceStateRequest{
SourceProviderAddress: proto6.SourceProviderAddress,
SourceRawState: proto6.SourceState,
SourceSchemaVersion: proto6.SourceSchemaVersion,
SourceTypeName: proto6.SourceTypeName,
TargetResource: resource,
TargetResourceSchema: resourceSchema,
TargetTypeName: proto6.TargetTypeName,
SourceProviderAddress: proto6.SourceProviderAddress,
SourceRawState: proto6.SourceState,
SourceSchemaVersion: proto6.SourceSchemaVersion,
SourceTypeName: proto6.SourceTypeName,
TargetResource: resource,
TargetResourceSchema: resourceSchema,
TargetTypeName: proto6.TargetTypeName,
SourceIdentity: proto6.SourceIdentity,
SourceIdentitySchemaVersion: proto6.SourceIdentitySchemaVersion,
IdentitySchema: identitySchema,
}

sourcePrivate, sourcePrivateDiags := privatestate.NewData(ctx, proto6.SourcePrivate)
Expand Down
27 changes: 26 additions & 1 deletion internal/fromproto6/moveresourcestate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func TestMoveResourceStateRequest(t *testing.T) {
testCases := map[string]struct {
input *tfprotov6.MoveResourceStateRequest
resourceSchema fwschema.Schema
identitySchema fwschema.Schema
resource resource.Resource
expected *fwserver.MoveResourceStateRequest
expectedDiagnostics diag.Diagnostics
Expand Down Expand Up @@ -162,13 +163,37 @@ func TestMoveResourceStateRequest(t *testing.T) {
TargetTypeName: "examplecloud_thing",
},
},
"SourceIdentity": {
input: &tfprotov6.MoveResourceStateRequest{
SourceIdentity: testNewRawState(t, map[string]interface{}{
"test_identity_attribute": "test-value",
}),
},
resourceSchema: testFwSchema,
expected: &fwserver.MoveResourceStateRequest{
SourceIdentity: testNewRawState(t, map[string]interface{}{
"test_identity_attribute": "test-value",
}),
TargetResourceSchema: testFwSchema,
},
},
"SourceIdentitySchemaVersion": {
input: &tfprotov6.MoveResourceStateRequest{
SourceIdentitySchemaVersion: 123,
},
resourceSchema: testFwSchema,
expected: &fwserver.MoveResourceStateRequest{
SourceIdentitySchemaVersion: 123,
TargetResourceSchema: testFwSchema,
},
},
}

for name, testCase := range testCases {
t.Run(name, func(t *testing.T) {
t.Parallel()

got, diags := fromproto6.MoveResourceStateRequest(context.Background(), testCase.input, testCase.resource, testCase.resourceSchema)
got, diags := fromproto6.MoveResourceStateRequest(context.Background(), testCase.input, testCase.resource, testCase.resourceSchema, testCase.identitySchema)

if diff := cmp.Diff(got, testCase.expected); diff != "" {
t.Errorf("unexpected difference: %s", diff)
Expand Down
50 changes: 42 additions & 8 deletions internal/fwserver/server_moveresourcestate.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,25 @@ type MoveResourceStateRequest struct {
// TargetTypeName is the type name of the target resource as given by
// Terraform across the protocol.
TargetTypeName string

// SourceIdentity is the identity of the source resource.
//
// Only the underlying JSON field is populated.
SourceIdentity *tfprotov6.RawState

// SourceIdentitySchemaVersion is the version of the source resource state.
SourceIdentitySchemaVersion int64

IdentitySchema fwschema.Schema
}

// MoveResourceStateResponse is the framework server response for the
// MoveResourceState RPC.
type MoveResourceStateResponse struct {
Diagnostics diag.Diagnostics
TargetPrivate *privatestate.Data
TargetState *tfsdk.State
Diagnostics diag.Diagnostics
TargetPrivate *privatestate.Data
TargetState *tfsdk.State
TargetIdentity *tfsdk.ResourceIdentity
}

// MoveResourceState implements the framework server MoveResourceState RPC.
Expand Down Expand Up @@ -125,12 +136,15 @@ func (s *Server) MoveResourceState(ctx context.Context, req *MoveResourceStateRe

for _, resourceStateMover := range resourceStateMovers {
moveStateReq := resource.MoveStateRequest{
SourcePrivate: sourcePrivate,
SourceProviderAddress: req.SourceProviderAddress,
SourceRawState: req.SourceRawState,
SourceSchemaVersion: req.SourceSchemaVersion,
SourceTypeName: req.SourceTypeName,
SourcePrivate: sourcePrivate,
SourceProviderAddress: req.SourceProviderAddress,
SourceRawState: req.SourceRawState,
SourceSchemaVersion: req.SourceSchemaVersion,
SourceTypeName: req.SourceTypeName,
SourceIdentity: req.SourceIdentity,
SourceIdentitySchemaVersion: req.SourceIdentitySchemaVersion,
}

moveStateResp := resource.MoveStateResponse{
TargetPrivate: privatestate.EmptyProviderData(ctx),
TargetState: tfsdk.State{
Expand All @@ -139,6 +153,13 @@ func (s *Server) MoveResourceState(ctx context.Context, req *MoveResourceStateRe
},
}

if req.IdentitySchema != nil {
moveStateResp.TargetIdentity = &tfsdk.ResourceIdentity{
Raw: tftypes.NewValue(req.IdentitySchema.Type().TerraformType(ctx), nil),
Schema: req.IdentitySchema,
}
}

if resourceStateMover.SourceSchema != nil {
logging.FrameworkTrace(ctx, "Attempting to populate MoveResourceStateRequest SourceState from provider defined SourceSchema")

Expand Down Expand Up @@ -221,6 +242,19 @@ func (s *Server) MoveResourceState(ctx context.Context, req *MoveResourceStateRe
if !moveStateResp.TargetState.Raw.Equal(tftypes.NewValue(req.TargetResourceSchema.Type().TerraformType(ctx), nil)) {
resp.Diagnostics = moveStateResp.Diagnostics
resp.TargetState = &moveStateResp.TargetState
if moveStateResp.TargetIdentity != nil {
resp.TargetIdentity = moveStateResp.TargetIdentity
}

if resp.TargetIdentity != nil && req.IdentitySchema == nil {
resp.Diagnostics.AddError(
"Unexpected Move State Response",
"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"+
"This is always a problem with the provider and should be reported to the provider developer.",
)

return
}

if moveStateResp.TargetPrivate != nil {
resp.TargetPrivate.Provider = moveStateResp.TargetPrivate
Expand Down
Loading