Skip to content

Commit 59870d9

Browse files
authored
chore: Fix identity TODOs and add documentation for interfaces (#1133)
* chore: Fix identity TODOs and add documentation for interfaces * Update resource/resource.go
1 parent 3497335 commit 59870d9

9 files changed

+20
-15
lines changed

internal/fromproto5/resource_identity.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@ import (
1313
"github.com/hashicorp/terraform-plugin-go/tfprotov5"
1414
)
1515

16-
// TODO:ResourceIdentity: Should we create a wrapping struct to contain the identity data? To match the protocol (in-case we want to introduce other identity things)
17-
// - Need to think more on this (like what if we want to introduce display-only attributes)
18-
// - If we introduce one, add a test as well.
16+
// ResourceIdentity returns the *tfsdk.ResourceIdentity for a *tfprotov5.ResourceIdentityData and fwschema.Schema.
1917
func ResourceIdentity(ctx context.Context, in *tfprotov5.ResourceIdentityData, schema fwschema.Schema) (*tfsdk.ResourceIdentity, diag.Diagnostics) {
2018
if in == nil {
2119
return nil, nil

internal/fromproto6/resource_identity.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@ import (
1313
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
1414
)
1515

16-
// TODO:ResourceIdentity: Should we create a wrapping struct to contain the identity data? To match the protocol (in-case we want to introduce other identity things)
17-
// - Need to think more on this (like what if we want to introduce display-only attributes)
18-
// - If we introduce one, add a test as well.
16+
// ResourceIdentity returns the *tfsdk.ResourceIdentity for a *tfprotov6.ResourceIdentityData and fwschema.Schema.
1917
func ResourceIdentity(ctx context.Context, in *tfprotov6.ResourceIdentityData, schema fwschema.Schema) (*tfsdk.ResourceIdentity, diag.Diagnostics) {
2018
if in == nil {
2119
return nil, nil

internal/fwserver/server_createresource.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,6 @@ func (s *Server) CreateResource(ctx context.Context, req *CreateResourceRequest,
101101
}
102102

103103
// If the resource supports identity and there is no planned identity data, pre-populate with a null value.
104-
// TODO:ResourceIdentity: This logic is likely useless since plan should already handle this, probably should remove.
105104
if req.PlannedIdentity == nil && req.IdentitySchema != nil {
106105
nullIdentityTfValue := tftypes.NewValue(req.IdentitySchema.Type().TerraformType(ctx), nil)
107106

internal/fwserver/server_deleteresource.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,6 @@ func (s *Server) DeleteResource(ctx context.Context, req *DeleteResourceRequest,
9898
resp.Private = req.PlannedPrivate
9999
}
100100

101-
// If the resource supports identity pre-populate a null value.
102-
// TODO:ResourceIdentity: This should probably be prior identity, but we don't currently have that in the protocol.
103101
if req.IdentitySchema != nil {
104102
nullIdentityTfValue := tftypes.NewValue(req.IdentitySchema.Type().TerraformType(ctx), nil)
105103

internal/fwserver/server_importresourcestate.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,6 @@ func (s *Server) ImportResourceState(ctx context.Context, req *ImportResourceSta
149149
}
150150

151151
// If the resource supports identity and we are not importing by identity, pre-populate with a null value.
152-
// TODO:ResourceIdentity: Is there any reason a provider WOULD NOT want to populate an identity when it supports one?
153152
if req.Identity == nil && req.IdentitySchema != nil {
154153
nullTfValue := tftypes.NewValue(req.IdentitySchema.Type().TerraformType(ctx), nil)
155154

internal/fwserver/server_planresourcechange.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,6 @@ func (s *Server) PlanResourceChange(ctx context.Context, req *PlanResourceChange
119119
}
120120

121121
// If the resource supports identity and there is no prior identity data, pre-populate with a null value.
122-
// TODO:ResourceIdentity: Is there any reason a provider WOULD NOT want to populate an identity when it supports one?
123-
// TODO:ResourceIdentity: Should this be set to all unknowns?
124122
if req.PriorIdentity == nil && req.IdentitySchema != nil {
125123
nullIdentityTfValue := tftypes.NewValue(req.IdentitySchema.Type().TerraformType(ctx), nil)
126124

internal/fwserver/server_readresource.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@ func (s *Server) ReadResource(ctx context.Context, req *ReadResourceRequest, res
120120
}
121121

122122
// If the resource supports identity and there is no current identity data, pre-populate with a null value.
123-
// TODO:ResourceIdentity: Is there any reason a provider WOULD NOT want to populate an identity when it supports one?
124123
if req.CurrentIdentity == nil && req.IdentitySchema != nil {
125124
nullTfValue := tftypes.NewValue(req.IdentitySchema.Type().TerraformType(ctx), nil)
126125

internal/fwserver/server_updateresource.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,6 @@ func (s *Server) UpdateResource(ctx context.Context, req *UpdateResourceRequest,
122122
}
123123

124124
// If the resource supports identity and there is no planned identity data, pre-populate with a null value.
125-
// TODO:ResourceIdentity: This logic is likely useless since plan should already handle this, probably should remove.
126125
if req.PlannedIdentity == nil && req.IdentitySchema != nil {
127126
nullIdentityTfValue := tftypes.NewValue(req.IdentitySchema.Type().TerraformType(ctx), nil)
128127

resource/resource.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ import (
1919
// - Plan Modification: Schema-based or entire plan
2020
// via ResourceWithModifyPlan.
2121
// - State Upgrades: ResourceWithUpgradeState
22+
// - Identity: Define an identity schema with ResourceWithIdentity to enable
23+
// storing identity data in state during CRUD operations. Identity data is
24+
// used by Terraform to uniquely identify a managed resource.
2225
//
2326
// Although not required, it is conventional for resources to implement the
2427
// ResourceWithImportState interface.
@@ -199,7 +202,21 @@ type ResourceWithValidateConfig interface {
199202

200203
// ResourceWithIdentity is an interface type that extends Resource to implement managed resource identity.
201204
//
202-
// TODO:ResourceIdentity: Add more documentation here to describe what identity is used for.
205+
// Managed resources can optionally define an identity schema, which represents a separate object stored in state
206+
// alongside the resource instance data. This identity data is used by Terraform to uniquely identify
207+
// managed resources and has additional restrictions that allow external programs to determine equality
208+
// between two identities.
209+
//
210+
// Resource identity schemas can only contain primitive (bool, string, number) attributes and lists that
211+
// contain primitive elements. Additionally, a resource identity should have the following properties:
212+
// - The resource identity should correspond to at most one remote object per provider, across all
213+
// instances of that provider.
214+
// - Given a resource identity, the provider should be able to determine whether the corresponding remote
215+
// object exists, and if so, return the resource state. Resources that support identity can be imported
216+
// by the identity object via the ResourceWithImportState interface.
217+
// - The resource identity should not change during the lifecycle of the remote object. That is, from the
218+
// creation of the remote object in the remote system until its destruction. An exception to this rule
219+
// is an upgrade of the identity data after a schema change, via the ResourceWithUpgradeIdentity interface.
203220
type ResourceWithIdentity interface {
204221
Resource
205222

0 commit comments

Comments
 (0)