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

Simplify PF attr obj shim #2232

Draft
wants to merge 3 commits into
base: vvm/add_pf_shim_tests1
Choose a base branch
from

Conversation

VenelinMartinov
Copy link
Contributor

@VenelinMartinov VenelinMartinov commented Jul 23, 2024

We seem to represent list-nested objects in the shim layer differently depending on if it is an attr or block.
The attribute one seems overly complicated: list: {element: schema: element: resource} vs list: element: resource for blocks.

This is an attempt at simplifying it - not fully tested yet.

@VenelinMartinov VenelinMartinov changed the base branch from master to vvm/add_pf_shim_tests1 July 23, 2024 10:27
@VenelinMartinov
Copy link
Contributor Author

The failing tests even expects this:

assert.Truef(t, isPseudoResource, "expected shim.Elem() to be of type shim.Resource, encoding an object type")

It says it asserts on type .Resource but actually asserts on .Schema

Copy link

codecov bot commented Jul 23, 2024

Codecov Report

Attention: Patch coverage is 69.23077% with 4 lines in your changes missing coverage. Please review.

Project coverage is 60.66%. Comparing base (e46106b) to head (e316c27).

Files Patch % Lines
pf/internal/schemashim/attr_schema.go 69.23% 3 Missing and 1 partial ⚠️
Additional details and impacted files
@@                   Coverage Diff                   @@
##           vvm/add_pf_shim_tests1    #2232   +/-   ##
=======================================================
  Coverage                   60.66%   60.66%           
=======================================================
  Files                         356      356           
  Lines                       46447    46459   +12     
=======================================================
+ Hits                        28176    28184    +8     
- Misses                      16711    16714    +3     
- Partials                     1560     1561    +1     

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

@t0yv0 t0yv0 self-requested a review July 23, 2024 14:57
Copy link
Member

@iwahbe iwahbe left a comment

Choose a reason for hiding this comment

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

It looks like this change is correct. I'm extremely surprised there are not runtime implications to this.

@VenelinMartinov
Copy link
Contributor Author

I believe we must have been already handling both as both are present. I'll sit on this for a bit

}
}
},
"type": 6
Copy link
Member

Choose a reason for hiding this comment

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

type=6 is TypeMap I think. This looks like the test was expecting what Elem() describes as "single-nested block encoding" - "In this case s.Type() == TypeMap and s.Elem() is a Resource value."

IN the new encoding, this is encoded as TypeList with Elem()=Resource. Elem() doc comment describes that this is an encoding for List-nested block but with this change it also encodes a List attribute.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It the old encoding it is List(Elem(Map(Elem(Resource))))

In the new one it is List(Elem(Resource))

Given that pulumi doesn't care about attribute vs block, we might want to encode list nested blocks and list nested objects the same. Both represent an object in a list as far as pulumi is concerned.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Also in the old encoding list_nested_attribute and list with object elem are encoded the same. I'm not sure I understand why these would be different to list_nested_block in the shim layer.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In the sdkv2 list nested attributes are also represented the same as list nested blocks: #2242

Copy link
Member

Choose a reason for hiding this comment

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

Given that pulumi doesn't care about attribute vs block

Are we 100% sure. I was trying to remember if that's the case, can't find anything just now searching now. There is normalizeBlockCollections but it's scoped to sdkv2 "under" the Shim abstraction.

@@ -197,16 +192,11 @@ func TestSchemaShimRepresentations(t *testing.T) {
"_": {
"list_nested_attribute": {
"element": {
"schema": {
Copy link
Member

Choose a reason for hiding this comment

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

Similarly a "single-nested-block-ish" encoding layer is stripped here.

@@ -109,10 +109,10 @@ func TestCustomListAttribute(t *testing.T) {
shimmed := &attrSchema{"key", pfutils.FromAttrLike(raw)}
assert.Equal(t, shim.TypeList, shimmed.Type())
assert.NotNil(t, shimmed.Elem())
_, isPseudoResource := shimmed.Elem().(shim.Schema)
_, isPseudoResource := shimmed.Elem().(shim.Resource)
Copy link
Member

Choose a reason for hiding this comment

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

Assert on next line interestingly said it expects Resource, but the cast is casting to shim.Schema.

@t0yv0
Copy link
Member

t0yv0 commented Jul 23, 2024

This may be a better encoding, but this is pretty error-prone, I wonder if we could complement this with some tests taking this for a spin end to end. Specifically, for tfgen layer and runtime handling:

  • is the Pulumi schema generated for these resources what we expect?
  • can the bridged provider actually push data through and have it decoded in the PF Create method to match the shape expected by PF

@t0yv0
Copy link
Member

t0yv0 commented Jul 23, 2024

Also sounds like an update on the Elem() doc comment is needed here for this case.

@VenelinMartinov
Copy link
Contributor Author

Yeah, some end-to-end tests are certainly needed - thanks for the suggestions on what to focus on!

I'll also revisit the Elem docs.

@t0yv0
Copy link
Member

t0yv0 commented Jul 24, 2024

I expect there is a big encoding bug here somewhere.

Map-nested attributes Map<string,{"x":string}> and single-nested blocks {"x":string} don't have enough in shim.Schema to distinguish them:

https://developer.hashicorp.com/terraform/plugin/framework/handling-data/attributes/map-nested

But they might clobber each other in the shim.Schema representation with Type=TypeMap, Elem()=Resource.

Also in SDKv2 TypeMap, Elem()=Resource means a degenerate case of Map<string,string>. Ideally we cover all these cases and get this right.

@VenelinMartinov
Copy link
Contributor Author

Also in SDKv2 TypeMap, Elem()=Resource

This is invalid in SDKv2 - only lists and sets can have an Elem Resource

@t0yv0
Copy link
Member

t0yv0 commented Jul 24, 2024

IT is admitted though, and valid, per doc in Elem() and https://github.com/hashicorp/terraform-plugin-sdk/blob/main/helper/schema/core_schema_test.go#L220

@VenelinMartinov
Copy link
Contributor Author

Wondering if any providers actually use this - I doubt it works well

@t0yv0
Copy link
Member

t0yv0 commented Jul 24, 2024

We have to make it an error though, or else to make the schema Map<string,string>, since it's a possibility.

@VenelinMartinov
Copy link
Contributor Author

Yeah, absolutely, great point about Map nested Resource elems in SDKv2

@mjeffryes mjeffryes added this to the 0.108 milestone Aug 16, 2024
@mikhailshilkov mikhailshilkov removed this from the 0.108 milestone Aug 21, 2024
@t0yv0
Copy link
Member

t0yv0 commented Oct 1, 2024

AS time permits, could we rebase on #2456 and have another peek at the interesting bits with the generated Pulumi Schema side-by-side? Could be a good learning experience. Thanks!

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.

5 participants