From 7cbf31737861636a6189459c9fd1e82b491db6d1 Mon Sep 17 00:00:00 2001 From: Matt Bush Date: Tue, 23 Jan 2024 13:39:39 -0800 Subject: [PATCH] Revert "Merge pull request #317 from ulucinar/fix-customize-diff" This reverts commit 1d547af3ddb6560f7675f793b5f4caa8061295b9, reversing changes made to 0bc8185b6fa1286204ec3723cdfa75f5978df6f8. --- pkg/controller/external_nofork.go | 39 +++++++++++++------------------ 1 file changed, 16 insertions(+), 23 deletions(-) diff --git a/pkg/controller/external_nofork.go b/pkg/controller/external_nofork.go index 4bb73c60..52a9413a 100644 --- a/pkg/controller/external_nofork.go +++ b/pkg/controller/external_nofork.go @@ -17,7 +17,7 @@ import ( "github.com/crossplane/crossplane-runtime/pkg/reconciler/managed" xpresource "github.com/crossplane/crossplane-runtime/pkg/resource" "github.com/hashicorp/go-cty/cty" - tfdiag "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" tf "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" "github.com/pkg/errors" @@ -104,8 +104,8 @@ func getJSONMap(mg xpresource.Managed) (map[string]any, error) { } type Resource interface { - Apply(ctx context.Context, s *tf.InstanceState, d *tf.InstanceDiff, meta interface{}) (*tf.InstanceState, tfdiag.Diagnostics) - RefreshWithoutUpgrade(ctx context.Context, s *tf.InstanceState, meta interface{}) (*tf.InstanceState, tfdiag.Diagnostics) + Apply(ctx context.Context, s *tf.InstanceState, d *tf.InstanceDiff, meta interface{}) (*tf.InstanceState, diag.Diagnostics) + RefreshWithoutUpgrade(ctx context.Context, s *tf.InstanceState, meta interface{}) (*tf.InstanceState, diag.Diagnostics) } type noForkExternal struct { @@ -474,21 +474,10 @@ func (n *noForkExternal) Observe(ctx context.Context, mg xpresource.Managed) (ma if diag != nil && diag.HasError() { return managed.ExternalObservation{}, errors.Errorf("failed to observe the resource: %v", diag) } - diffState := n.opTracker.GetTfState() n.opTracker.SetTfState(newState) // TODO: missing RawConfig & RawPlan here... - resourceExists := newState != nil && newState.ID != "" - var stateValueMap map[string]any - if resourceExists { - jsonMap, stateValue, err := n.fromInstanceStateToJSONMap(newState) - if err != nil { - return managed.ExternalObservation{}, errors.Wrap(err, "cannot convert instance state to JSON map") - } - stateValueMap = jsonMap - newState.RawPlan = stateValue - diffState = newState - } - instanceDiff, err := n.getResourceDataDiff(mg.(resource.Terraformed), ctx, diffState, resourceExists) + resourceExists := newState != nil && newState.ID != "" + instanceDiff, err := n.getResourceDataDiff(mg.(resource.Terraformed), ctx, newState, resourceExists) if err != nil { return managed.ExternalObservation{}, errors.Wrap(err, "cannot compute the instance diff") } @@ -507,6 +496,10 @@ func (n *noForkExternal) Observe(ctx context.Context, mg xpresource.Managed) (ma addTTR(mg) } mg.SetConditions(xpv1.Available()) + stateValueMap, err := n.fromInstanceStateToJSONMap(newState) + if err != nil { + return managed.ExternalObservation{}, errors.Wrap(err, "cannot convert instance state to JSON map") + } buff, err := json.TFParser.Marshal(stateValueMap) if err != nil { @@ -608,12 +601,12 @@ func (n *noForkExternal) Create(ctx context.Context, mg xpresource.Managed) (man } n.opTracker.SetTfState(newState) - stateValueMap, _, err := n.fromInstanceStateToJSONMap(newState) + stateValueMap, err := n.fromInstanceStateToJSONMap(newState) if err != nil { return managed.ExternalCreation{}, errors.Wrap(err, "failed to convert instance state to map") } if _, err := n.setExternalName(mg, stateValueMap); err != nil { - return managed.ExternalCreation{}, errors.Wrap(err, "failed to set the external-name of the managed resource during create") + return managed.ExternalCreation{}, errors.Wrapf(err, "failed to set the external-name of the managed resource during create") } err = mg.(resource.Terraformed).SetObservation(stateValueMap) if err != nil { @@ -662,7 +655,7 @@ func (n *noForkExternal) Update(ctx context.Context, mg xpresource.Managed) (man } n.opTracker.SetTfState(newState) - stateValueMap, _, err := n.fromInstanceStateToJSONMap(newState) + stateValueMap, err := n.fromInstanceStateToJSONMap(newState) if err != nil { return managed.ExternalUpdate{}, err } @@ -693,15 +686,15 @@ func (n *noForkExternal) Delete(ctx context.Context, _ xpresource.Managed) error return nil } -func (n *noForkExternal) fromInstanceStateToJSONMap(newState *tf.InstanceState) (map[string]interface{}, cty.Value, error) { +func (n *noForkExternal) fromInstanceStateToJSONMap(newState *tf.InstanceState) (map[string]interface{}, error) { impliedType := n.config.TerraformResource.CoreConfigSchema().ImpliedType() attrsAsCtyValue, err := newState.AttrsAsObjectValue(impliedType) if err != nil { - return nil, cty.NilVal, errors.Wrap(err, "could not convert attrs to cty value") + return nil, errors.Wrap(err, "could not convert attrs to cty value") } stateValueMap, err := schema.StateValueToJSONMap(attrsAsCtyValue, impliedType) if err != nil { - return nil, cty.NilVal, errors.Wrap(err, "could not convert instance state value to JSON") + return nil, errors.Wrap(err, "could not convert instance state value to JSON") } - return stateValueMap, attrsAsCtyValue, nil + return stateValueMap, nil }