Skip to content
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

Switch upgradeResourceState to use SDKv2's gRPC method #1735

Closed
wants to merge 8 commits into from
Closed
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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ tidy::
# on windows.
pin_upstream_sdk: UpstreamPluginSDK=github.com/hashicorp/terraform-plugin-sdk/v2
pin_upstream_sdk: OurPluginSDK=github.com/pulumi/terraform-plugin-sdk/v2
pin_upstream_sdk: PluginSDKVersion=v2.0.0-20230912190043-e6d96b3b8f7e
pin_upstream_sdk: PluginSDKVersion=v2.0.0-20240520223432-0c0bf0d65f10
pin_upstream_sdk:
# /x/muxer doesn't depend on the rest of the bridge or any TF libraries, so it
# doesn't need this replace.
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -254,4 +254,4 @@ require (
lukechampine.com/frand v1.4.2 // indirect
)

replace github.com/hashicorp/terraform-plugin-sdk/v2 => github.com/pulumi/terraform-plugin-sdk/v2 v2.0.0-20240229143312-4f60ee4e2975
replace github.com/hashicorp/terraform-plugin-sdk/v2 => github.com/pulumi/terraform-plugin-sdk/v2 v2.0.0-20240520223432-0c0bf0d65f10
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2814,8 +2814,8 @@ github.com/pulumi/schema-tools v0.1.2 h1:Fd9xvUjgck4NA+7/jSk7InqCUT4Kj940+EcnbQK
github.com/pulumi/schema-tools v0.1.2/go.mod h1:62lgj52Tzq11eqWTIaKd+EVyYAu5dEcDJxMhTjvMO/k=
github.com/pulumi/terraform-diff-reader v0.0.2 h1:kTE4nEXU3/SYXESvAIem+wyHMI3abqkI3OhJ0G04LLI=
github.com/pulumi/terraform-diff-reader v0.0.2/go.mod h1:sZ9FUzGO+yM41hsQHs/yIcj/Y993qMdBxBU5mpDmAfQ=
github.com/pulumi/terraform-plugin-sdk/v2 v2.0.0-20240229143312-4f60ee4e2975 h1:1WBy43K/lHEdS5Hliwf3ylVSfAu5s0KhhEs6wNeP11Y=
github.com/pulumi/terraform-plugin-sdk/v2 v2.0.0-20240229143312-4f60ee4e2975/go.mod h1:H+8tjs9TjV2w57QFVSMBQacf8k/E1XwLXGCARgViC6A=
github.com/pulumi/terraform-plugin-sdk/v2 v2.0.0-20240520223432-0c0bf0d65f10 h1:M6Ceq1o6gGeEKxTZgJ2CKLye3p67ZZjnsAwxr5VC32A=
github.com/pulumi/terraform-plugin-sdk/v2 v2.0.0-20240520223432-0c0bf0d65f10/go.mod h1:H+8tjs9TjV2w57QFVSMBQacf8k/E1XwLXGCARgViC6A=
github.com/rakyll/embedmd v0.0.0-20171029212350-c8060a0752a2/go.mod h1:7jOTMgqac46PZcF54q6l2hkLEG8op93fZu61KmxWDV4=
github.com/rcrowley/go-metrics v0.0.0-20181016184325-3113b8401b8a/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4=
github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
Expand Down
2 changes: 1 addition & 1 deletion pf/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -251,4 +251,4 @@ require (
lukechampine.com/frand v1.4.2 // indirect
)

replace github.com/hashicorp/terraform-plugin-sdk/v2 => github.com/pulumi/terraform-plugin-sdk/v2 v2.0.0-20240229143312-4f60ee4e2975
replace github.com/hashicorp/terraform-plugin-sdk/v2 => github.com/pulumi/terraform-plugin-sdk/v2 v2.0.0-20240520223432-0c0bf0d65f10
4 changes: 2 additions & 2 deletions pf/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2813,8 +2813,8 @@ github.com/pulumi/schema-tools v0.1.2 h1:Fd9xvUjgck4NA+7/jSk7InqCUT4Kj940+EcnbQK
github.com/pulumi/schema-tools v0.1.2/go.mod h1:62lgj52Tzq11eqWTIaKd+EVyYAu5dEcDJxMhTjvMO/k=
github.com/pulumi/terraform-diff-reader v0.0.2 h1:kTE4nEXU3/SYXESvAIem+wyHMI3abqkI3OhJ0G04LLI=
github.com/pulumi/terraform-diff-reader v0.0.2/go.mod h1:sZ9FUzGO+yM41hsQHs/yIcj/Y993qMdBxBU5mpDmAfQ=
github.com/pulumi/terraform-plugin-sdk/v2 v2.0.0-20240229143312-4f60ee4e2975 h1:1WBy43K/lHEdS5Hliwf3ylVSfAu5s0KhhEs6wNeP11Y=
github.com/pulumi/terraform-plugin-sdk/v2 v2.0.0-20240229143312-4f60ee4e2975/go.mod h1:H+8tjs9TjV2w57QFVSMBQacf8k/E1XwLXGCARgViC6A=
github.com/pulumi/terraform-plugin-sdk/v2 v2.0.0-20240520223432-0c0bf0d65f10 h1:M6Ceq1o6gGeEKxTZgJ2CKLye3p67ZZjnsAwxr5VC32A=
github.com/pulumi/terraform-plugin-sdk/v2 v2.0.0-20240520223432-0c0bf0d65f10/go.mod h1:H+8tjs9TjV2w57QFVSMBQacf8k/E1XwLXGCARgViC6A=
github.com/rakyll/embedmd v0.0.0-20171029212350-c8060a0752a2/go.mod h1:7jOTMgqac46PZcF54q6l2hkLEG8op93fZu61KmxWDV4=
github.com/rcrowley/go-metrics v0.0.0-20181016184325-3113b8401b8a/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4=
github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
Expand Down
2 changes: 1 addition & 1 deletion pf/tests/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -270,4 +270,4 @@ require (
sourcegraph.com/sourcegraph/appdash v0.0.0-20211028080628-e2786a622600
)

replace github.com/hashicorp/terraform-plugin-sdk/v2 => github.com/pulumi/terraform-plugin-sdk/v2 v2.0.0-20240229143312-4f60ee4e2975
replace github.com/hashicorp/terraform-plugin-sdk/v2 => github.com/pulumi/terraform-plugin-sdk/v2 v2.0.0-20240520223432-0c0bf0d65f10
4 changes: 2 additions & 2 deletions pf/tests/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2840,8 +2840,8 @@ github.com/pulumi/schema-tools v0.1.2 h1:Fd9xvUjgck4NA+7/jSk7InqCUT4Kj940+EcnbQK
github.com/pulumi/schema-tools v0.1.2/go.mod h1:62lgj52Tzq11eqWTIaKd+EVyYAu5dEcDJxMhTjvMO/k=
github.com/pulumi/terraform-diff-reader v0.0.2 h1:kTE4nEXU3/SYXESvAIem+wyHMI3abqkI3OhJ0G04LLI=
github.com/pulumi/terraform-diff-reader v0.0.2/go.mod h1:sZ9FUzGO+yM41hsQHs/yIcj/Y993qMdBxBU5mpDmAfQ=
github.com/pulumi/terraform-plugin-sdk/v2 v2.0.0-20240229143312-4f60ee4e2975 h1:1WBy43K/lHEdS5Hliwf3ylVSfAu5s0KhhEs6wNeP11Y=
github.com/pulumi/terraform-plugin-sdk/v2 v2.0.0-20240229143312-4f60ee4e2975/go.mod h1:H+8tjs9TjV2w57QFVSMBQacf8k/E1XwLXGCARgViC6A=
github.com/pulumi/terraform-plugin-sdk/v2 v2.0.0-20240520223432-0c0bf0d65f10 h1:M6Ceq1o6gGeEKxTZgJ2CKLye3p67ZZjnsAwxr5VC32A=
github.com/pulumi/terraform-plugin-sdk/v2 v2.0.0-20240520223432-0c0bf0d65f10/go.mod h1:H+8tjs9TjV2w57QFVSMBQacf8k/E1XwLXGCARgViC6A=
github.com/rakyll/embedmd v0.0.0-20171029212350-c8060a0752a2/go.mod h1:7jOTMgqac46PZcF54q6l2hkLEG8op93fZu61KmxWDV4=
github.com/rcrowley/go-metrics v0.0.0-20181016184325-3113b8401b8a/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4=
github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
Expand Down
2 changes: 1 addition & 1 deletion pf/tests/internal/randomshim/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ require (
google.golang.org/appengine v1.6.8 // indirect
)

replace github.com/hashicorp/terraform-plugin-sdk/v2 => github.com/pulumi/terraform-plugin-sdk/v2 v2.0.0-20240229143312-4f60ee4e2975
replace github.com/hashicorp/terraform-plugin-sdk/v2 => github.com/pulumi/terraform-plugin-sdk/v2 v2.0.0-20240520223432-0c0bf0d65f10
4 changes: 2 additions & 2 deletions pf/tests/internal/randomshim/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ github.com/oklog/run v1.1.0 h1:GEenZ1cK0+q0+wsJew9qUg/DyD8k3JzYsZAi5gYi2mA=
github.com/oklog/run v1.1.0/go.mod h1:sVPdnTZT1zYwAJeCMu2Th4T21pA3FPOQRfWjQlk7DVU=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pulumi/terraform-plugin-sdk/v2 v2.0.0-20240229143312-4f60ee4e2975 h1:1WBy43K/lHEdS5Hliwf3ylVSfAu5s0KhhEs6wNeP11Y=
github.com/pulumi/terraform-plugin-sdk/v2 v2.0.0-20240229143312-4f60ee4e2975/go.mod h1:H+8tjs9TjV2w57QFVSMBQacf8k/E1XwLXGCARgViC6A=
github.com/pulumi/terraform-plugin-sdk/v2 v2.0.0-20240520223432-0c0bf0d65f10 h1:M6Ceq1o6gGeEKxTZgJ2CKLye3p67ZZjnsAwxr5VC32A=
github.com/pulumi/terraform-plugin-sdk/v2 v2.0.0-20240520223432-0c0bf0d65f10/go.mod h1:H+8tjs9TjV2w57QFVSMBQacf8k/E1XwLXGCARgViC6A=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals=
github.com/stretchr/testify v1.8.3 h1:RP3t2pwF7cMEbC1dqtB6poj3niw/9gnV4Cjg5oW5gtY=
Expand Down
2 changes: 1 addition & 1 deletion pf/tests/internal/tlsshim/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ require (

replace github.com/hashicorp/terraform-provider-tls => github.com/hashicorp/terraform-provider-tls v1.2.1-0.20230117062332-afdd54107aba

replace github.com/hashicorp/terraform-plugin-sdk/v2 => github.com/pulumi/terraform-plugin-sdk/v2 v2.0.0-20240229143312-4f60ee4e2975
replace github.com/hashicorp/terraform-plugin-sdk/v2 => github.com/pulumi/terraform-plugin-sdk/v2 v2.0.0-20240520223432-0c0bf0d65f10
4 changes: 2 additions & 2 deletions pf/tests/internal/tlsshim/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ github.com/oklog/run v1.1.0 h1:GEenZ1cK0+q0+wsJew9qUg/DyD8k3JzYsZAi5gYi2mA=
github.com/oklog/run v1.1.0/go.mod h1:sVPdnTZT1zYwAJeCMu2Th4T21pA3FPOQRfWjQlk7DVU=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pulumi/terraform-plugin-sdk/v2 v2.0.0-20240229143312-4f60ee4e2975 h1:1WBy43K/lHEdS5Hliwf3ylVSfAu5s0KhhEs6wNeP11Y=
github.com/pulumi/terraform-plugin-sdk/v2 v2.0.0-20240229143312-4f60ee4e2975/go.mod h1:H+8tjs9TjV2w57QFVSMBQacf8k/E1XwLXGCARgViC6A=
github.com/pulumi/terraform-plugin-sdk/v2 v2.0.0-20240520223432-0c0bf0d65f10 h1:M6Ceq1o6gGeEKxTZgJ2CKLye3p67ZZjnsAwxr5VC32A=
github.com/pulumi/terraform-plugin-sdk/v2 v2.0.0-20240520223432-0c0bf0d65f10/go.mod h1:H+8tjs9TjV2w57QFVSMBQacf8k/E1XwLXGCARgViC6A=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals=
github.com/stretchr/testify v1.8.3 h1:RP3t2pwF7cMEbC1dqtB6poj3niw/9gnV4Cjg5oW5gtY=
Expand Down
2 changes: 1 addition & 1 deletion pf/tests/testdatagen/genrandom/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,4 @@ require (
lukechampine.com/frand v1.4.2 // indirect
)

replace github.com/hashicorp/terraform-plugin-sdk/v2 => github.com/pulumi/terraform-plugin-sdk/v2 v2.0.0-20240229143312-4f60ee4e2975
replace github.com/hashicorp/terraform-plugin-sdk/v2 => github.com/pulumi/terraform-plugin-sdk/v2 v2.0.0-20240520223432-0c0bf0d65f10
2 changes: 1 addition & 1 deletion pkg/tests/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -257,4 +257,4 @@ require (
lukechampine.com/frand v1.4.2 // indirect
)

replace github.com/hashicorp/terraform-plugin-sdk/v2 => github.com/pulumi/terraform-plugin-sdk/v2 v2.0.0-20240229143312-4f60ee4e2975
replace github.com/hashicorp/terraform-plugin-sdk/v2 => github.com/pulumi/terraform-plugin-sdk/v2 v2.0.0-20240520223432-0c0bf0d65f10
4 changes: 2 additions & 2 deletions pkg/tests/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2824,8 +2824,8 @@ github.com/pulumi/schema-tools v0.1.2 h1:Fd9xvUjgck4NA+7/jSk7InqCUT4Kj940+EcnbQK
github.com/pulumi/schema-tools v0.1.2/go.mod h1:62lgj52Tzq11eqWTIaKd+EVyYAu5dEcDJxMhTjvMO/k=
github.com/pulumi/terraform-diff-reader v0.0.2 h1:kTE4nEXU3/SYXESvAIem+wyHMI3abqkI3OhJ0G04LLI=
github.com/pulumi/terraform-diff-reader v0.0.2/go.mod h1:sZ9FUzGO+yM41hsQHs/yIcj/Y993qMdBxBU5mpDmAfQ=
github.com/pulumi/terraform-plugin-sdk/v2 v2.0.0-20240229143312-4f60ee4e2975 h1:1WBy43K/lHEdS5Hliwf3ylVSfAu5s0KhhEs6wNeP11Y=
github.com/pulumi/terraform-plugin-sdk/v2 v2.0.0-20240229143312-4f60ee4e2975/go.mod h1:H+8tjs9TjV2w57QFVSMBQacf8k/E1XwLXGCARgViC6A=
github.com/pulumi/terraform-plugin-sdk/v2 v2.0.0-20240520223432-0c0bf0d65f10 h1:M6Ceq1o6gGeEKxTZgJ2CKLye3p67ZZjnsAwxr5VC32A=
github.com/pulumi/terraform-plugin-sdk/v2 v2.0.0-20240520223432-0c0bf0d65f10/go.mod h1:H+8tjs9TjV2w57QFVSMBQacf8k/E1XwLXGCARgViC6A=
github.com/rakyll/embedmd v0.0.0-20171029212350-c8060a0752a2/go.mod h1:7jOTMgqac46PZcF54q6l2hkLEG8op93fZu61KmxWDV4=
github.com/rcrowley/go-metrics v0.0.0-20181016184325-3113b8401b8a/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4=
github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
Expand Down
2 changes: 1 addition & 1 deletion pkg/tfshim/sdk-v2/diagnostics.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func warningsAndErrors(diags diag.Diagnostics) ([]string, []error) {
return warnings, errors
}

func errors(diags diag.Diagnostics) error {
func diagToError(diags diag.Diagnostics) error {
var err error
for _, d := range diags {
if d.Severity == diag.Error {
Expand Down
12 changes: 6 additions & 6 deletions pkg/tfshim/sdk-v2/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func (p v2Provider) Configure(ctx context.Context, c shim.ResourceConfig) error
// See ConfigureProvider e.g.
// https://github.com/hashicorp/terraform-plugin-sdk/blob/main/helper/schema/grpc_provider.go#L564
ctxHack := context.WithValue(ctx, schema.StopContextKey, p.stopContext(context.Background()))
return errors(p.tf.Configure(ctxHack, configFromShim(c)))
return diagToError(p.tf.Configure(ctxHack, configFromShim(c)))
}

func (p v2Provider) stopContext(ctx context.Context) context.Context {
Expand All @@ -124,12 +124,12 @@ func (p v2Provider) Apply(
if !ok {
return nil, fmt.Errorf("unknown resource %v", t)
}
state, err := upgradeResourceState(ctx, p.tf, r, stateFromShim(s))
state, err := upgradeResourceState(ctx, t, p.tf, r, stateFromShim(s))
if err != nil {
return nil, fmt.Errorf("failed to upgrade resource state: %w", err)
}
state, diags := r.Apply(ctx, state, diffFromShim(d), p.tf.Meta())
return stateToShim(r, state), errors(diags)
return stateToShim(r, state), diagToError(diags)
}

func (p v2Provider) Refresh(
Expand All @@ -148,7 +148,7 @@ func (p v2Provider) Refresh(
return nil, fmt.Errorf("unknown resource %v", t)
}

state, err := upgradeResourceState(ctx, p.tf, r, stateFromShim(s))
state, err := upgradeResourceState(ctx, t, p.tf, r, stateFromShim(s))
if err != nil {
return nil, fmt.Errorf("failed to upgrade resource state: %w", err)
}
Expand All @@ -158,7 +158,7 @@ func (p v2Provider) Refresh(
}

state, diags := r.RefreshWithoutUpgrade(context.TODO(), state, p.tf.Meta())
return stateToShim(r, state), errors(diags)
return stateToShim(r, state), diagToError(diags)
}

func (p v2Provider) ReadDataDiff(
Expand Down Expand Up @@ -195,7 +195,7 @@ func (p v2Provider) ReadDataApply(
return nil, fmt.Errorf("unknown resource %v", t)
}
state, diags := r.ReadDataApply(ctx, diffFromShim(d), p.tf.Meta())
return stateToShim(r, state), errors(diags)
return stateToShim(r, state), diagToError(diags)
}

func (p v2Provider) Meta(_ context.Context) interface{} {
Expand Down
4 changes: 2 additions & 2 deletions pkg/tfshim/sdk-v2/provider2.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ func (p *planResourceChangeImpl) upgradeState(
instanceState.Attributes = map[string]string{}
}
instanceState.Meta = state.meta
newInstanceState, err := upgradeResourceState(ctx, p.tf, res, instanceState)
newInstanceState, err := upgradeResourceState(ctx, t, p.tf, res, instanceState)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -368,7 +368,7 @@ func (s *grpcServer) handle(ctx context.Context, diags []*tfprotov5.Diagnostic,
dd = append(dd, rd)
logDiag(ctx, rd)
}
derr := errors(dd)
derr := diagToError(dd)
if derr != nil && err != nil {
return multierror.Append(derr, err)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/tfshim/sdk-v2/provider_diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (p v2Provider) Diff(
}
} else {
// Upgrades are needed only if we have non-empty prior state.
state, err = upgradeResourceState(ctx, p.tf, r, state)
state, err = upgradeResourceState(ctx, t, p.tf, r, state)
if err != nil {
return nil, fmt.Errorf("failed to upgrade resource state: %w", err)
}
Expand Down
118 changes: 80 additions & 38 deletions pkg/tfshim/sdk-v2/upgrade_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,82 +6,118 @@ import (
"strconv"

"github.com/hashicorp/go-cty/cty"
ctyjson "github.com/hashicorp/go-cty/cty/json"
"github.com/hashicorp/go-cty/cty/msgpack"
"github.com/hashicorp/terraform-plugin-go/tfprotov5"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)

func upgradeResourceState(ctx context.Context, p *schema.Provider, res *schema.Resource,
func upgradeResourceState(ctx context.Context, typeName string, p *schema.Provider, res *schema.Resource,
instanceState *terraform.InstanceState) (*terraform.InstanceState, error) {

if instanceState == nil {
return nil, nil
}

m := instanceState.Attributes

// Ensure that we have an ID in the attributes.
m["id"] = instanceState.ID

version, hasVersion := 0, false
version, hasVersion := int64(0), false
if versionValue, ok := instanceState.Meta["schema_version"]; ok {
versionString, ok := versionValue.(string)
if !ok {
return nil, fmt.Errorf("unexpected type %T for schema_version", versionValue)
}
v, err := strconv.ParseInt(versionString, 0, 32)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to parse schema_version: %w", err)
}
version, hasVersion = int(v), true
version, hasVersion = v, true
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see a check for downgrades here - pretty sure TF doesn't allow downgrades, see #1998 (comment)


// First, build a JSON state from the InstanceState.
json, version, err := schema.UpgradeFlatmapState(ctx, version, m, res, p.Meta())
if err != nil {
return nil, err
// If RawState is not set but attributes is, we need to hydrate RawState
// from attributes.
if instanceState.RawState.IsNull() {
// We default to assuming that the old state has the same shape as the new
// resource.
typ := res.CoreConfigSchema().ImpliedType()

// If we have a version, we use the schema shape that matches the version
// specified.
if hasVersion {
for _, t := range res.StateUpgraders {
if t.Version == int(version) {
typ = t.Type
break
}
}
}

state, err := instanceState.AttrsAsObjectValue(typ)
if err != nil {
return nil, fmt.Errorf("state from attributes: %w", err)
}
instanceState.RawState = state
}

// Next, migrate the JSON state up to the current version.
json, err = schema.UpgradeJSONState(ctx, version, json, res, p.Meta())
if err != nil {
return nil, err
// Ensure that we have an ID in the attributes.
if state := instanceState.RawState.AsValueMap(); !has(state, "id") {
state["id"] = cty.StringVal(instanceState.ID)
instanceState.RawState = cty.ObjectVal(state)
}

configBlock := res.CoreConfigSchema()
json, err := ctyjson.Marshal(instanceState.RawState, instanceState.RawState.Type())
if err != nil {
return nil, fmt.Errorf("failed to marshal RawState: %w", err)
}

// Strip out removed fields.
schema.RemoveAttributes(ctx, json, configBlock.ImpliedType())
// Now, we perform the UpgradeResourceState operation by calling into TF's UpgradeResourceState.

// now we need to turn the state into the default json representation, so
// that it can be re-decoded using the actual schema.
v, err := schema.JSONMapToStateValue(json, configBlock)
resp, err := schema.NewGRPCProviderServer(p).
UpgradeResourceState(ctx, &tfprotov5.UpgradeResourceStateRequest{
TypeName: typeName,
Version: version,
RawState: &tfprotov5.RawState{JSON: json},
})
if err != nil {
return nil, err
return nil, fmt.Errorf("upgrade resource state GRPC: %w", err)
}

// Now we need to make sure blocks are represented correctly, which means
// that missing blocks are empty collections, rather than null.
// First we need to CoerceValue to ensure that all object types match.
v, err = configBlock.CoerceValue(v)
if err != nil {
return nil, err
// Handle returned diagnostics.
var dd diag.Diagnostics
for _, d := range resp.Diagnostics {
if d == nil {
continue
}
rd := recoverDiagnostic(*d)
dd = append(dd, rd)
logDiag(ctx, rd)
}
if err := diagToError(dd); err != nil {
return nil, fmt.Errorf("diag: %w", err)
}

// Normalize the value and fill in any missing blocks.
v = schema.NormalizeObjectFromLegacySDK(v, configBlock)
// Unmarshal to get back the underlying type.
var rawState cty.Value
if resp.UpgradedState.JSON != nil {
rawState, err = ctyjson.Unmarshal(resp.UpgradedState.JSON, res.CoreConfigSchema().ImpliedType())
} else if resp.UpgradedState.MsgPack != nil {
rawState, err = msgpack.Unmarshal(resp.UpgradedState.MsgPack, res.CoreConfigSchema().ImpliedType())
}
if err != nil {
return nil, fmt.Errorf("failed to unmarshal: %w", err)
}

// Convert the value back to an InstanceState.
newState, err := res.ShimInstanceStateFromValue(v)
newState, err := res.ShimInstanceStateFromValue(rawState)
if err != nil {
return nil, err
}

newState.RawConfig = instanceState.RawConfig

// Copy the original ID and meta to the new state and stamp in the new version.
newState.ID = instanceState.ID

// If state upgraders have modified the ID, respect the modification.
if updatedID, ok := findID(v); ok {
if updatedID, ok := findID(rawState); ok {
newState.ID = updatedID
}

Expand All @@ -90,8 +126,9 @@ func upgradeResourceState(ctx context.Context, p *schema.Provider, res *schema.R
if newState.Meta == nil {
newState.Meta = map[string]interface{}{}
}
newState.Meta["schema_version"] = strconv.Itoa(version)
newState.Meta["schema_version"] = strconv.Itoa(int(version))
}

return newState, nil
}

Expand All @@ -103,8 +140,13 @@ func findID(v cty.Value) (string, bool) {
if !ok {
return "", false
}
if !id.Type().Equals(cty.String) {
if !id.Type().Equals(cty.String) || id.IsNull() {
return "", false
}
return id.AsString(), true
}

func has[K comparable, V any](m map[K]V, k K) bool {
_, ok := m[k]
return ok
}
Loading