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

Fix nested computed read #2181

Merged
merged 35 commits into from
Jul 23, 2024
Merged

Conversation

VenelinMartinov
Copy link
Contributor

@VenelinMartinov VenelinMartinov commented Jul 12, 2024

Fixes an issue with the SDKv2 and PF input extraction. During import we wrongly return values for properties which are fully computed which causes invalid programs to be generated.

The object check in CastToTypeObject was wrong for SDKV2 objects:

func CastToTypeObject(tfs shim.Schema) (shim.SchemaMap, bool) {

The CastToTypeObject object check is wrong for both SDKv2 and PF. For the SDKV2 only sets and lists can have a Resource .Elem. For PF it list-nested blocks are represented with a Resource .Elem in the shim layer, so the check did not catch these. PF details here: #2231
This fixes the check and adds regression tests for the broken imports which resulted from 2180.

Test coverage added in #2224 for sdkv2 and #2225 for pf.
fixes #2180
depends on pulumi/providertest#91
stacked on #2225

@VenelinMartinov VenelinMartinov changed the title Fix nested computed read1 Fix nested computed read Jul 12, 2024
Copy link

codecov bot commented Jul 12, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 62.50%. Comparing base (bf90025) to head (13521d8).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2181      +/-   ##
==========================================
+ Coverage   62.49%   62.50%   +0.01%     
==========================================
  Files         355      355              
  Lines       38625    38625              
==========================================
+ Hits        24137    24141       +4     
+ Misses      12928    12925       -3     
+ Partials     1560     1559       -1     

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

@VenelinMartinov VenelinMartinov requested review from iwahbe, guineveresaenger and t0yv0 and removed request for iwahbe July 16, 2024 09:28
@VenelinMartinov VenelinMartinov marked this pull request as ready for review July 16, 2024 09:28
@VenelinMartinov
Copy link
Contributor Author

GH rate limits...

@@ -494,8 +493,6 @@ func (ctx *conversionContext) makeTerraformInput(

var tfflds shim.SchemaMap

// We cannot use [shimutil.CastToTypeObject] because we have machinery that constructs invalid
Copy link
Member

Choose a reason for hiding this comment

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

This is exactly the root cause, I find it unfortunate to remove this comment. elemSchemas produces

(&schema.Schema{Elem: e}).Shim()

This is not a valid schema as it does not have Type() defined. This is an artificial schema that you will not find coming from the actual provider, it's a recursion artifact.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But CastToTypeObject is wrong in the first place for SDKv2 - it checks for .Resource under a Map type which is illegal in SDKV2.

I don't understand why we'd want to use it here anyway - tfs.Elem().(shim.Resource) is the correct way to check for an object type

Copy link
Member

Choose a reason for hiding this comment

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

I'm not going to block on this change but the Elem() representation is continuously confusing. I don't think "object type" is defined enough for me.

How about #2187 to try to clarify some of the issues here?

Copy link
Member

Choose a reason for hiding this comment

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

My understanding was that tfs.Elem().(shim.Resource) was insufficient to identify an object type: tfs.Type() == TypeList && tfs.Elem().(shim.Resource) implied a List[Object] where tfs.Type() == TypeMap && tfs.Elem().(shim.Resource) implied an Object type.

Copy link
Member

Choose a reason for hiding this comment

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

When recurring over a List[Object] type which is indeed tfs.Type() == TypeList && tfs.Elem().(shim.Resource) the code will call elemSchemas to compute a shim.Schema for the Object, which will result in a shim.Schema value that's not recognized by CastToTypeObject. One can argue that it should, in fact, be recognized, and then we could bring that back here and work this out that way. That's a bit more changes though.

@t0yv0 t0yv0 self-requested a review July 16, 2024 16:53
@VenelinMartinov
Copy link
Contributor Author

Adding additional tests around this for sdkv2: #2224 and pf: #2225

I'll come back here once these are done to make sure we don't regress on any of the existing behaviour here.

Base automatically changed from vvm/remove_unused_schema_options to master July 22, 2024 14:58
VenelinMartinov added a commit that referenced this pull request Jul 23, 2024
This adds test cases for how TF types are handled by
`ExtractInputsFromOutputs` in combination with `Default` and `Computed`
and `MaxItems: 1`
`ExtractInputsFromOutputs` is the function in `schema.go` responsible
for generating the inputs from resource outputs when a resource is
imported. Its output is what the engine then uses for generating the
code for the import.

Notably, nested computed values are wrong for both max items one and
non-max items one.

In #2180 we
found a regression there, so this fills in some missing coverage.
Extracting the tests to make the changes more apparent and to make sure
we don't regress in
#2181
@VenelinMartinov VenelinMartinov changed the base branch from master to vvm/pf_extract_inputs_from_outputs July 23, 2024 08:45
@VenelinMartinov
Copy link
Contributor Author

After adding the PF tests it looks like this affects PF too. I'll add that to the issue and PR description.

VenelinMartinov added a commit that referenced this pull request Jul 23, 2024
This adds adds missing test coverage for `ExtractInputsFromOutputs` for
various PF schemas. `ExtractInputsFromOutputs` is a shared function so
we should have coverage on both sides to make sure we don't cause
regressions when fixing one.

Similar to #2224
but on the PF side.

This is in preparation for
#2181 for
#2180.

Seeing some more evidence of
#2218 here.
Base automatically changed from vvm/pf_extract_inputs_from_outputs to master July 23, 2024 16:01
@VenelinMartinov VenelinMartinov enabled auto-merge (squash) July 23, 2024 16:04
@VenelinMartinov VenelinMartinov merged commit da22823 into master Jul 23, 2024
11 checks passed
@VenelinMartinov VenelinMartinov deleted the vvm/fix_nested_computed_read1 branch July 23, 2024 16:16
@mjeffryes mjeffryes added this to the 0.107 milestone Jul 24, 2024
@pulumi-bot
Copy link
Contributor

This PR has been shipped in release v3.88.0.

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.

SDKv2 and PF Read return values for fully computed nested properties when importing
5 participants