Skip to content

Commit b0c0d20

Browse files
ansgarmaustinvalle
andauthored
tfprotov6: Resource Identity implementation (#476)
* add resource identity support to protocol v6 * add changie entries for this change and the ones that landed in #472 and #474 * Apply suggestions from code review * fix comment typo * fix lint --------- Co-authored-by: Austin Valle <[email protected]>
1 parent d9ff6de commit b0c0d20

26 files changed

+1262
-10
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
kind: FEATURES
2+
body: 'tfprotov5+tfprotov6: Upgraded protocols and added types to support the new resource identity feature'
3+
time: 2025-02-10T17:51:44.461603+01:00
4+
custom:
5+
Issue: "476"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
kind: NOTES
2+
body: 'tfprotov5+tfprotov6: An upcoming release will require the `GetResourceIdentitySchemas` and `UpgradeResourceIdentity` implementations as part of `ProviderServer`.'
3+
time: 2025-02-10T17:54:14.054598+01:00
4+
custom:
5+
Issue: "476"

tfprotov5/provider.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ type GetResourceIdentitySchemasRequest struct{}
177177
// GetResourceIdentitySchemasResponse represents a Terraform RPC response containing
178178
// the provider's resource identity schemas.
179179
type GetResourceIdentitySchemasResponse struct {
180-
// ResourceSchemas is a map of resource names to the schema for the
180+
// IdentitySchemas is a map of resource names to the schema for the
181181
// identity specified for the resource. The name should be a
182182
// resource name, and should be prefixed with your provider's shortname
183183
// and an underscore. It should match the first label after `resource`

tfprotov6/internal/fromproto/provider.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@ func GetProviderSchemaRequest(in *tfplugin6.GetProviderSchema_Request) *tfprotov
2828
return resp
2929
}
3030

31+
func GetResourceIdentitySchemasRequest(in *tfplugin6.GetResourceIdentitySchemas_Request) *tfprotov6.GetResourceIdentitySchemasRequest {
32+
if in == nil {
33+
return nil
34+
}
35+
36+
resp := &tfprotov6.GetResourceIdentitySchemasRequest{}
37+
38+
return resp
39+
}
40+
3141
func ValidateProviderConfigRequest(in *tfplugin6.ValidateProviderConfig_Request) *tfprotov6.ValidateProviderConfigRequest {
3242
if in == nil {
3343
return nil

tfprotov6/internal/fromproto/provider_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,37 @@ func TestGetProviderSchemaRequest(t *testing.T) {
7272
}
7373
}
7474

75+
func TestGetResourceIdentitySchemasRequest(t *testing.T) {
76+
t.Parallel()
77+
78+
testCases := map[string]struct {
79+
in *tfplugin6.GetResourceIdentitySchemas_Request
80+
expected *tfprotov6.GetResourceIdentitySchemasRequest
81+
}{
82+
"nil": {
83+
in: nil,
84+
expected: nil,
85+
},
86+
"zero": {
87+
in: &tfplugin6.GetResourceIdentitySchemas_Request{},
88+
expected: &tfprotov6.GetResourceIdentitySchemasRequest{},
89+
},
90+
}
91+
92+
for name, testCase := range testCases {
93+
94+
t.Run(name, func(t *testing.T) {
95+
t.Parallel()
96+
97+
got := fromproto.GetResourceIdentitySchemasRequest(testCase.in)
98+
99+
if diff := cmp.Diff(got, testCase.expected); diff != "" {
100+
t.Errorf("unexpected difference: %s", diff)
101+
}
102+
})
103+
}
104+
}
105+
75106
func TestConfigureProviderRequest(t *testing.T) {
76107
t.Parallel()
77108

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package fromproto
5+
6+
import (
7+
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
8+
)
9+
10+
func RawIdentity(in []byte) *tfprotov6.RawIdentity {
11+
if in == nil {
12+
return nil
13+
}
14+
15+
resp := &tfprotov6.RawIdentity{
16+
JSON: in,
17+
}
18+
19+
return resp
20+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package fromproto_test
5+
6+
import (
7+
"testing"
8+
9+
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
10+
)
11+
12+
func testTfprotov6RawIdentity(t *testing.T, json []byte) *tfprotov6.RawIdentity {
13+
t.Helper()
14+
15+
return &tfprotov6.RawIdentity{
16+
JSON: json,
17+
}
18+
}

tfprotov6/internal/fromproto/resource.go

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,20 @@ func UpgradeResourceStateRequest(in *tfplugin6.UpgradeResourceState_Request) *tf
3636
return resp
3737
}
3838

39+
func UpgradeResourceIdentityRequest(in *tfplugin6.UpgradeResourceIdentity_Request) *tfprotov6.UpgradeResourceIdentityRequest {
40+
if in == nil {
41+
return nil
42+
}
43+
44+
resp := &tfprotov6.UpgradeResourceIdentityRequest{
45+
RawIdentity: RawIdentity(in.RawIdentity),
46+
TypeName: in.TypeName,
47+
Version: in.Version,
48+
}
49+
50+
return resp
51+
}
52+
3953
func ReadResourceRequest(in *tfplugin6.ReadResource_Request) *tfprotov6.ReadResourceRequest {
4054
if in == nil {
4155
return nil
@@ -47,6 +61,7 @@ func ReadResourceRequest(in *tfplugin6.ReadResource_Request) *tfprotov6.ReadReso
4761
ProviderMeta: DynamicValue(in.ProviderMeta),
4862
TypeName: in.TypeName,
4963
ClientCapabilities: ReadResourceClientCapabilities(in.ClientCapabilities),
64+
CurrentIdentity: ResourceIdentityData(in.CurrentIdentity),
5065
}
5166

5267
return resp
@@ -65,6 +80,7 @@ func PlanResourceChangeRequest(in *tfplugin6.PlanResourceChange_Request) *tfprot
6580
ProviderMeta: DynamicValue(in.ProviderMeta),
6681
TypeName: in.TypeName,
6782
ClientCapabilities: PlanResourceChangeClientCapabilities(in.ClientCapabilities),
83+
PriorIdentity: ResourceIdentityData(in.PriorIdentity),
6884
}
6985

7086
return resp
@@ -76,12 +92,13 @@ func ApplyResourceChangeRequest(in *tfplugin6.ApplyResourceChange_Request) *tfpr
7692
}
7793

7894
resp := &tfprotov6.ApplyResourceChangeRequest{
79-
Config: DynamicValue(in.Config),
80-
PlannedPrivate: in.PlannedPrivate,
81-
PlannedState: DynamicValue(in.PlannedState),
82-
PriorState: DynamicValue(in.PriorState),
83-
ProviderMeta: DynamicValue(in.ProviderMeta),
84-
TypeName: in.TypeName,
95+
Config: DynamicValue(in.Config),
96+
PlannedPrivate: in.PlannedPrivate,
97+
PlannedState: DynamicValue(in.PlannedState),
98+
PriorState: DynamicValue(in.PriorState),
99+
ProviderMeta: DynamicValue(in.ProviderMeta),
100+
TypeName: in.TypeName,
101+
PlannedIdentity: ResourceIdentityData(in.PlannedIdentity),
85102
}
86103

87104
return resp
@@ -96,6 +113,7 @@ func ImportResourceStateRequest(in *tfplugin6.ImportResourceState_Request) *tfpr
96113
TypeName: in.TypeName,
97114
ID: in.Id,
98115
ClientCapabilities: ImportResourceStateClientCapabilities(in.ClientCapabilities),
116+
Identity: ResourceIdentityData(in.Identity),
99117
}
100118

101119
return resp
@@ -113,6 +131,7 @@ func MoveResourceStateRequest(in *tfplugin6.MoveResourceState_Request) *tfprotov
113131
SourceState: RawState(in.SourceState),
114132
SourceTypeName: in.SourceTypeName,
115133
TargetTypeName: in.TargetTypeName,
134+
SourceIdentity: ResourceIdentityData(in.SourceIdentity),
116135
}
117136

118137
return resp
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package fromproto
5+
6+
import (
7+
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
8+
"github.com/hashicorp/terraform-plugin-go/tfprotov6/internal/tfplugin6"
9+
)
10+
11+
func ResourceIdentityData(in *tfplugin6.ResourceIdentityData) *tfprotov6.ResourceIdentityData {
12+
if in == nil {
13+
return nil
14+
}
15+
16+
resp := &tfprotov6.ResourceIdentityData{
17+
IdentityData: DynamicValue(in.IdentityData),
18+
}
19+
20+
return resp
21+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package fromproto_test
5+
6+
import (
7+
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
8+
"github.com/hashicorp/terraform-plugin-go/tfprotov6/internal/tfplugin6"
9+
"github.com/hashicorp/terraform-plugin-go/tfprotov6/internal/toproto"
10+
)
11+
12+
func testTfplugin6ResourceIdentityData() *tfplugin6.ResourceIdentityData {
13+
return toproto.ResourceIdentityData(testTfprotov6ResourceIdentityData())
14+
}
15+
16+
func testTfprotov6ResourceIdentityData() *tfprotov6.ResourceIdentityData {
17+
return &tfprotov6.ResourceIdentityData{
18+
IdentityData: testTfprotov6DynamicValue(),
19+
}
20+
}

0 commit comments

Comments
 (0)