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

Unify upgradeResourceState between provider and provider2 #2005

Merged
merged 3 commits into from
May 22, 2024

Conversation

iwahbe
Copy link
Member

@iwahbe iwahbe commented May 21, 2024

This PR extracts the non provider2 specific part of planResourceChangeImpl.upgradeState into it's own function: upgradeResourceStateGRPC. This part is a pure refactor.

The PR then re-implements upgradeResourceState in terms of upgradeResourceStateGRPC.

The driving motivation for this change is to reduce complexity and allow both upgraders to handle large JSON numbers.

Replaces #1735

The driving motivation for this change is to reduce complexity and allow both upgraders to
handle large JSON numbers.
return newState, newMeta, nil
}

func extractSchemaVersion(meta map[string]any) (int64, bool, error) {
Copy link
Member Author

Choose a reason for hiding this comment

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

This needed to be extracted out since we use it in multiple places:

  • We use it in upgradeResourceStateGRPC to send the current schema version to the provider server.
  • We use it in upgradeResourceState to find the correct type to hydrate the instanceState.Attributes into.

Copy link

codecov bot commented May 21, 2024

Codecov Report

Attention: Patch coverage is 71.42857% with 20 lines in your changes are missing coverage. Please review.

Project coverage is 62.80%. Comparing base (3d58741) to head (56e4a27).
Report is 13 commits behind head on master.

Files Patch % Lines
pkg/tfshim/sdk-v2/upgrade_state.go 69.69% 13 Missing and 7 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2005      +/-   ##
==========================================
+ Coverage   60.87%   62.80%   +1.92%     
==========================================
  Files         332      332              
  Lines       44756    37294    -7462     
==========================================
- Hits        27247    23421    -3826     
+ Misses      15987    12352    -3635     
+ Partials     1522     1521       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@iwahbe iwahbe requested a review from t0yv0 May 22, 2024 18:10
@@ -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))
Copy link
Member

Choose a reason for hiding this comment

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

Should this use GRPC version? Possibly later?

Copy link
Member Author

Choose a reason for hiding this comment

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

It does. upgradeResourceState shims from the representation used by v2Provider to cty.Value, invokes upgradeResourceStateGRPC and then shims back.

// Copy the original ID and meta to the new state and stamp in the new version.
newState.RawConfig = instanceState.RawConfig
newState.RawState = v
newState.Meta = newMeta
newState.ID = instanceState.ID

// If state upgraders have modified the ID, respect the modification.
Copy link
Member

Choose a reason for hiding this comment

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

I vaguely recall working on the fix for this, would be interesting to see if the gRPC version just handles it right.

Copy link
Member Author

Choose a reason for hiding this comment

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

It doesn't, since the gRPC version returns a (cty.Value, map[string]any, error). This is still needed to bridge back to *terraform.InstanceState.

Copy link
Member

Choose a reason for hiding this comment

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

Brilliant. We can connect this to #1895 - I believe if we do this right and transition to PlanResourceChange cty.Value is all that is needed.

@iwahbe iwahbe enabled auto-merge (squash) May 22, 2024 20:14
@iwahbe iwahbe merged commit 536f577 into master May 22, 2024
11 checks passed
@iwahbe iwahbe deleted the iwahbe/unify-upgrade-resource-state branch May 22, 2024 20:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants