Resource Identity: Add identity data to existing RPCs and add two new identity related RPCs"#1444
Resource Identity: Add identity data to existing RPCs and add two new identity related RPCs"#1444
Conversation
5a14a0c to
1aaf288
Compare
austinvalle
left a comment
There was a problem hiding this comment.
Took a first look, good stuff so far!
| if v.RequiredForImport { | ||
|
|
||
| } else if v.OptionalForImport { | ||
|
|
||
| } else if !v.Required && !v.Optional && !v.Computed { |
There was a problem hiding this comment.
It would be nice to validate the identity schema separate from the other schemas, it would make logic like this a little cleaner.
Would this allow you to create invalid resource schemas that use OptionalForImport or RequiredForImport, and therefore have Required, Optional, and Computed as false?
There was a problem hiding this comment.
Technically, this internalValidate() should be schema type agnostic. I don't think it's even possible to pass information about the schema type to this method without some major refactoring. If we have resource identity schema-specific validation that might need to be a separate method that's called in (*provider).InternalValidate(). This is where the validation that blocks write-only attributes in non-managed resource schemas is:
terraform-plugin-sdk/helper/schema/provider.go
Lines 235 to 238 in 19e5b30
There was a problem hiding this comment.
We'll implement validation in a follow-up PR – I'll leave this discussion open for reference.
37bfb7d to
2fe253a
Compare
SBGoods
left a comment
There was a problem hiding this comment.
Looks pretty good so far! Left a couple of comments
| if v.RequiredForImport { | ||
|
|
||
| } else if v.OptionalForImport { | ||
|
|
||
| } else if !v.Required && !v.Optional && !v.Computed { |
There was a problem hiding this comment.
Technically, this internalValidate() should be schema type agnostic. I don't think it's even possible to pass information about the schema type to this method without some major refactoring. If we have resource identity schema-specific validation that might need to be a separate method that's called in (*provider).InternalValidate(). This is where the validation that blocks write-only attributes in non-managed resource schemas is:
terraform-plugin-sdk/helper/schema/provider.go
Lines 235 to 238 in 19e5b30
…but getting close
…onfigIdentitySchemaToProto and coreConfigIdentitySchema to use in GetResourceIdentitySchemas
…tomize diff functions
…eed to check if the expected behavior is correct
…Identity_jsonStateBigInt however BigInt doesn't work just yet
…date resource identities
08cbafd to
379753f
Compare
4dd72ac to
fea8e20
Compare
| if v.WriteOnly && v.Default != nil { | ||
| return fmt.Errorf("%s: Default cannot be set with WriteOnly", k) | ||
| } |
There was a problem hiding this comment.
I think we can keep this small improvement
austinvalle
left a comment
There was a problem hiding this comment.
LGTM 🚀
We should follow up this PR with a bump to the terraform-plugin-go@v0.27.0-alpha.1 and maybe a pre-release changelog similar to the framework one -> https://github.com/hashicorp/terraform-plugin-framework/blob/main/.changes/1.15.0-alpha.1.md
| // TODO: is there a more elegant way to do this? this requires us to look for the identity schema block again | ||
| if req.CurrentIdentity != nil && req.CurrentIdentity.IdentityData != nil { |
There was a problem hiding this comment.
I'm not 100% sure what you mean by this comment, but in general we'll always need the identity schema to decode identity data from the protocol (any DynamicValue in the protocol needs a schema to decode to avoid losing any type/value information)
Perhaps all of this logic can eventually be extracted to a function that transforms a *tfprotov5.ResourceIdentityData into a flatmap, but not sure we can avoid the usage of the schema.
There was a problem hiding this comment.
Oh, that comment is mostly there to indicate that this code is repeated multiple times – so extracting it into a function might make sense, but yeah, schema would need to be passed into it to work.
|
|
||
| // convert req.CurrentIdentity to flat map identity structure | ||
| // Step 1: Turn JSON into cty.Value based on schema | ||
| identityBlock := s.getResourceIdentitySchemaBlock(req.TypeName) // TODO: handle error if no schema exists (and after we add the error) |
There was a problem hiding this comment.
I agree, we should generally trying to return some nice error if we encounter identity data unexpectedly
| // Step 2: Turn cty.Value into flatmap representation | ||
| identityAttrs := hcl2shim.FlatmapValueFromHCL2(identityVal) | ||
| // Step 3: Well, set it in the priorState | ||
| priorState.Identity = identityAttrs |
There was a problem hiding this comment.
Weird, I didn't know that planned state was also kind of side-car'd with prior state 😅
There was a problem hiding this comment.
This might be copy-pasted in error – I don't recall if it was needed
| // TODO: if schema defines identity, we should error if there's none written to newInstanceState | ||
| if res.Identity != nil { |
There was a problem hiding this comment.
Nothing I think needs immediate action before we merge this PR, just some thoughts
I'm actually not sure where the logic to "delete" the resource (send back a null value) happens in SDKv2, but I think we'll want to make sure we send a null identity object back if we're destroying.
For example, here it is in framework: https://github.com/hashicorp/terraform-plugin-framework/blob/f01eb331c102f536ee3b93c7a6066edbabb815fb/internal/fwserver/server_deleteresource.go#L116-L134
I also don't know if core cares about what we send back, but this might be something we run into if they do. Perhaps a follow-up! (Once we start using terraform-plugin-testing to test identity, we'll find out quick if this is a problem 😆 )
| UnsafeToUseLegacyTypeSystem: true, | ||
| }, | ||
| }, | ||
| "basic-plan-with-identity": { |
There was a problem hiding this comment.
These are great tests to have here! ❤️
I'd say once we start getting more complex then this, we can start implementing terraform-provider-corner tests, since they'll hit the plan/apply combo
| } | ||
|
|
||
| result.Identity = mapWIdentity.Map() | ||
| } // TODO: else log error? |
There was a problem hiding this comment.
➕ to eventually returning errors in situations like this. This would indicate that either Terraform has a bug or the SDK has a bug, so we want to explicitly call that out.

This PR adds initial support for Resource Identity to the SDKv2. It's in an early stage, meaning it's only lightly tested and mostly covering the happy path.