When you update a resource's implementation in your provider, some changes may not be compatible with old versions. You
can create state upgraders to automatically migrate resources provisioned with old schema configurations. Refer to
State Upgrade in the Framework documentation for details.
This page explains how to migrate resource StateUpgraders in SDKv2 to UpgradeState in the plugin Framework.
In SDKv2, state upgraders are defined by populating the StateUpgraders field on the schema.Resource struct. Refer
to State Migration in the SDKv2 documentation for details.
The following code shows a basic implementation of the stateUpgraders field in SDKv2.
Remember the following differences between SDKv2 and the Framework when completing the migration.
In SDKv2, you implement state upgraders populating the StateUpgraders field on the schema.Resource struct. In the
Framework, you define an UpgradeState function on the resource itself.
In SDKv2, state upgraders apply each state upgrader in turn. For example, version 0 => version 1, version 1 =>
version 2. In the Framework, each UpgradeState function is required to perform all of the necessary transformations in
a single step. For example, version 0 => version 2, version 1 => version 2.
The following example shows the implementation of the resourcePasswordStateUpgradeV0 function with SDKv2.
funcresourcePasswordStateUpgradeV0(_ context.Context, rawState map[string]interface{},_interface{})(map[string]interface{},error){if rawState ==nil{returnnil, fmt.Errorf("resource password state upgrade failed, state is nil")}
result, ok := rawState["result"].(string)if!ok {returnnil, fmt.Errorf("resource password state upgrade failed, result is not a string: %T", rawState["result"])}
hash, err :=generateHash(result)if err !=nil{returnnil, fmt.Errorf("resource password state upgrade failed, generate hash error: %w", err)}
rawState["bcrypt_hash"]= hash
return rawState,nil}
funcresourcePasswordStateUpgradeV0(_ context.Context, rawState map[string]interface{},_interface{})(map[string]interface{},error){if rawState ==nil{returnnil, fmt.Errorf("resource password state upgrade failed, state is nil")} result, ok := rawState["result"].(string)if!ok {returnnil, fmt.Errorf("resource password state upgrade failed, result is not a string: %T", rawState["result"])} hash, err :=generateHash(result)if err !=nil{returnnil, fmt.Errorf("resource password state upgrade failed, generate hash error: %w", err)} rawState["bcrypt_hash"]= hash
return rawState,nil}
The following shows the same section of provider code after the migration.
This code implements the ResourceWithUpgradeState interface on the passwordResource type by defining an
UpgradeState function. The UpgradeState function returns a map from each state version (int64) to a
ResourceStateUpgrader struct.