88 "context"
99 "encoding/json"
1010 "fmt"
11+ "maps"
1112 "math"
1213 "math/big"
1314 "strings"
@@ -18,7 +19,6 @@ import (
1819 "github.com/crossplane/crossplane-runtime/pkg/meta"
1920 "github.com/crossplane/crossplane-runtime/pkg/reconciler/managed"
2021 xpresource "github.com/crossplane/crossplane-runtime/pkg/resource"
21- fwdiag "github.com/hashicorp/terraform-plugin-framework/diag"
2222 fwprovider "github.com/hashicorp/terraform-plugin-framework/provider"
2323 "github.com/hashicorp/terraform-plugin-framework/providerserver"
2424 fwresource "github.com/hashicorp/terraform-plugin-framework/resource"
@@ -35,6 +35,7 @@ import (
3535 "github.com/crossplane/upjet/pkg/resource"
3636 upjson "github.com/crossplane/upjet/pkg/resource/json"
3737 "github.com/crossplane/upjet/pkg/terraform"
38+ tferrors "github.com/crossplane/upjet/pkg/terraform/errors"
3839)
3940
4041// TerraformPluginFrameworkConnector is an external client, with credentials and
@@ -189,8 +190,7 @@ func (c *TerraformPluginFrameworkConnector) getResourceSchema(ctx context.Contex
189190 schemaResp := & fwresource.SchemaResponse {}
190191 res .Schema (ctx , fwresource.SchemaRequest {}, schemaResp )
191192 if schemaResp .Diagnostics .HasError () {
192- fwErrors := frameworkDiagnosticsToString (schemaResp .Diagnostics )
193- return rschema.Schema {}, errors .Errorf ("could not retrieve resource schema: %s" , fwErrors )
193+ return rschema.Schema {}, tferrors .FrameworkDiagnosticsError ("could not retrieve resource schema" , schemaResp .Diagnostics )
194194 }
195195
196196 return schemaResp .Schema , nil
@@ -209,8 +209,7 @@ func (c *TerraformPluginFrameworkConnector) configureProvider(ctx context.Contex
209209 var schemaResp fwprovider.SchemaResponse
210210 ts .FrameworkProvider .Schema (ctx , fwprovider.SchemaRequest {}, & schemaResp )
211211 if schemaResp .Diagnostics .HasError () {
212- fwDiags := frameworkDiagnosticsToString (schemaResp .Diagnostics )
213- return nil , fmt .Errorf ("cannot retrieve provider schema: %s" , fwDiags )
212+ return nil , tferrors .FrameworkDiagnosticsError ("cannot retrieve provider schema" , schemaResp .Diagnostics )
214213 }
215214 providerServer := providerserver .NewProtocol5 (ts .FrameworkProvider )()
216215
@@ -254,15 +253,20 @@ func (n *terraformPluginFrameworkExternalClient) filteredDiffExists(rawDiff []tf
254253// If plan response contains non-empty RequiresReplace (i.e. the resource needs
255254// to be recreated) an error is returned as Crossplane Resource Model (XRM)
256255// prohibits resource re-creations and rejects this plan.
257- func (n * terraformPluginFrameworkExternalClient ) getDiffPlanResponse (ctx context.Context ,
258- tfStateValue tftypes.Value ) (* tfprotov5.PlanResourceChangeResponse , bool , error ) {
259- tfConfigDynamicVal , err := protov5DynamicValueFromMap (n .params , n .resourceValueTerraformType )
256+ func (n * terraformPluginFrameworkExternalClient ) getDiffPlanResponse (ctx context.Context , tfStateValue tftypes.Value ) (* tfprotov5.PlanResourceChangeResponse , bool , error ) {
257+ params := maps .Clone (n .params )
258+ // if some computed identifiers have been configured,
259+ // remove them from config.
260+ for _ , id := range n .config .ExternalName .TFPluginFrameworkOptions .ComputedIdentifierAttributes {
261+ delete (params , id )
262+ }
263+ tfConfigDynamicVal , err := protov5DynamicValueFromMap (params , n .resourceValueTerraformType )
260264 if err != nil {
261265 return nil , false , errors .Wrap (err , "cannot construct dynamic value for TF Config" )
262266 }
263267
264268 //
265- tfPlannedStateDynamicVal , err := protov5DynamicValueFromMap (n . params , n .resourceValueTerraformType )
269+ tfPlannedStateDynamicVal , err := protov5DynamicValueFromMap (params , n .resourceValueTerraformType )
266270 if err != nil {
267271 return nil , false , errors .Wrap (err , "cannot construct dynamic value for TF Planned State" )
268272 }
@@ -708,18 +712,6 @@ func getFatalDiagnostics(diags []*tfprotov5.Diagnostic) error {
708712 return errs
709713}
710714
711- // frameworkDiagnosticsToString constructs an error string from the provided
712- // Plugin Framework diagnostics instance. Only Error severity diagnostics are
713- // included.
714- func frameworkDiagnosticsToString (fwdiags fwdiag.Diagnostics ) string {
715- frameworkErrorDiags := fwdiags .Errors ()
716- diagErrors := make ([]string , 0 , len (frameworkErrorDiags ))
717- for _ , tfdiag := range frameworkErrorDiags {
718- diagErrors = append (diagErrors , fmt .Sprintf ("%s: %s" , tfdiag .Summary (), tfdiag .Detail ()))
719- }
720- return strings .Join (diagErrors , "\n " )
721- }
722-
723715// protov5DynamicValueFromMap constructs a protov5 DynamicValue given the
724716// map[string]any using the terraform type as reference.
725717func protov5DynamicValueFromMap (data map [string ]any , terraformType tftypes.Type ) (* tfprotov5.DynamicValue , error ) {
0 commit comments