Skip to content

Commit 6ebe79b

Browse files
rainkwanansgarmaustinvalle
authored
tfprotov5: Resource Identity implementation (#474)
* start writing files for v5 * add missing code comments * add new fields added by identity protocol changes made by core * add missing copywrite headers * remove traces of flatmap from copied file that were missed * put new rpc calls in separate interface * first bunch of tests for files that already existed (and some for new files that are helpers for the other tests in prior art only) * Added test files for toproto for tfprotov5 and started adding to logging. Also addressed renamed the file as per suggested in the previous PR. * Fixing typo * update attribute names to match latest protobuf schema * fix tests * add test for raw_identity (copied from state.go) * update to latest gprc schema used by core * remove ResourceIdentityContext as we don't need it * add attributes for new identity data added to the protocol recently * add comments and fix logging * Apply suggestions from code review --------- Co-authored-by: Ansgar Mertens <[email protected]> Co-authored-by: Austin Valle <[email protected]>
1 parent 71a1984 commit 6ebe79b

29 files changed

+3106
-1682
lines changed

tfprotov5/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 *tfplugin5.GetProviderSchema_Request) *tfprotov
2828
return resp
2929
}
3030

31+
func GetResourceIdentitySchemasRequest(in *tfplugin5.GetResourceIdentitySchemas_Request) *tfprotov5.GetResourceIdentitySchemasRequest {
32+
if in == nil {
33+
return nil
34+
}
35+
36+
resp := &tfprotov5.GetResourceIdentitySchemasRequest{}
37+
38+
return resp
39+
}
40+
3141
func PrepareProviderConfigRequest(in *tfplugin5.PrepareProviderConfig_Request) *tfprotov5.PrepareProviderConfigRequest {
3242
if in == nil {
3343
return nil

tfprotov5/internal/fromproto/provider_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,38 @@ func TestGetProviderSchemaRequest(t *testing.T) {
7676
}
7777
}
7878

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

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/tfprotov5"
8+
)
9+
10+
func RawIdentity(in []byte) *tfprotov5.RawIdentity {
11+
if in == nil {
12+
return nil
13+
}
14+
15+
resp := &tfprotov5.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/tfprotov5"
10+
)
11+
12+
func testTfprotov5RawIdentity(t *testing.T, json []byte) *tfprotov5.RawIdentity {
13+
t.Helper()
14+
15+
return &tfprotov5.RawIdentity{
16+
JSON: json,
17+
}
18+
}

tfprotov5/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 *tfplugin5.UpgradeResourceState_Request) *tf
3636
return resp
3737
}
3838

39+
func UpgradeResourceIdentityRequest(in *tfplugin5.UpgradeResourceIdentity_Request) *tfprotov5.UpgradeResourceIdentityRequest {
40+
if in == nil {
41+
return nil
42+
}
43+
44+
resp := &tfprotov5.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 *tfplugin5.ReadResource_Request) *tfprotov5.ReadResourceRequest {
4054
if in == nil {
4155
return nil
@@ -47,6 +61,7 @@ func ReadResourceRequest(in *tfplugin5.ReadResource_Request) *tfprotov5.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 *tfplugin5.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 *tfplugin5.ApplyResourceChange_Request) *tfpr
7692
}
7793

7894
resp := &tfprotov5.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 *tfplugin5.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 *tfplugin5.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/tfprotov5"
8+
"github.com/hashicorp/terraform-plugin-go/tfprotov5/internal/tfplugin5"
9+
)
10+
11+
func ResourceIdentityData(in *tfplugin5.ResourceIdentityData) *tfprotov5.ResourceIdentityData {
12+
if in == nil {
13+
return nil
14+
}
15+
16+
resp := &tfprotov5.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/tfprotov5"
8+
"github.com/hashicorp/terraform-plugin-go/tfprotov5/internal/tfplugin5"
9+
"github.com/hashicorp/terraform-plugin-go/tfprotov5/internal/toproto"
10+
)
11+
12+
func testTfplugin5ResourceIdentityData() *tfplugin5.ResourceIdentityData {
13+
return toproto.ResourceIdentityData(testTfprotov5ResourceIdentityData())
14+
}
15+
16+
func testTfprotov5ResourceIdentityData() *tfprotov5.ResourceIdentityData {
17+
return &tfprotov5.ResourceIdentityData{
18+
IdentityData: testTfprotov5DynamicValue(),
19+
}
20+
}

tfprotov5/internal/fromproto/resource_test.go

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,14 @@ func TestApplyResourceChangeRequest(t *testing.T) {
7676
TypeName: "test",
7777
},
7878
},
79+
"PlannedIdentity": {
80+
in: &tfplugin5.ApplyResourceChange_Request{
81+
PlannedIdentity: testTfplugin5ResourceIdentityData(),
82+
},
83+
expected: &tfprotov5.ApplyResourceChangeRequest{
84+
PlannedIdentity: testTfprotov5ResourceIdentityData(),
85+
},
86+
},
7987
}
8088

8189
for name, testCase := range testCases {
@@ -136,6 +144,14 @@ func TestImportResourceStateRequest(t *testing.T) {
136144
},
137145
},
138146
},
147+
"Identity": {
148+
in: &tfplugin5.ImportResourceState_Request{
149+
Identity: testTfplugin5ResourceIdentityData(),
150+
},
151+
expected: &tfprotov5.ImportResourceStateRequest{
152+
Identity: testTfprotov5ResourceIdentityData(),
153+
},
154+
},
139155
}
140156

141157
for name, testCase := range testCases {
@@ -216,6 +232,14 @@ func TestMoveResourceStateRequest(t *testing.T) {
216232
TargetTypeName: "test",
217233
},
218234
},
235+
"SourceIdentity": {
236+
in: &tfplugin5.MoveResourceState_Request{
237+
SourceIdentity: testTfplugin5ResourceIdentityData(),
238+
},
239+
expected: &tfprotov5.MoveResourceStateRequest{
240+
SourceIdentity: testTfprotov5ResourceIdentityData(),
241+
},
242+
},
219243
}
220244

221245
for name, testCase := range testCases {
@@ -308,6 +332,14 @@ func TestPlanResourceChangeRequest(t *testing.T) {
308332
},
309333
},
310334
},
335+
"PriorIdentity": {
336+
in: &tfplugin5.PlanResourceChange_Request{
337+
PriorIdentity: testTfplugin5ResourceIdentityData(),
338+
},
339+
expected: &tfprotov5.PlanResourceChangeRequest{
340+
PriorIdentity: testTfprotov5ResourceIdentityData(),
341+
},
342+
},
311343
}
312344

313345
for name, testCase := range testCases {
@@ -384,6 +416,14 @@ func TestReadResourceRequest(t *testing.T) {
384416
},
385417
},
386418
},
419+
"CurrentIdentity": {
420+
in: &tfplugin5.ReadResource_Request{
421+
CurrentIdentity: testTfplugin5ResourceIdentityData(),
422+
},
423+
expected: &tfprotov5.ReadResourceRequest{
424+
CurrentIdentity: testTfprotov5ResourceIdentityData(),
425+
},
426+
},
387427
}
388428

389429
for name, testCase := range testCases {
@@ -457,6 +497,62 @@ func TestUpgradeResourceStateRequest(t *testing.T) {
457497
}
458498
}
459499

500+
func TestUpgradeResourceIdentityRequest(t *testing.T) {
501+
t.Parallel()
502+
503+
testCases := map[string]struct {
504+
in *tfplugin5.UpgradeResourceIdentity_Request
505+
expected *tfprotov5.UpgradeResourceIdentityRequest
506+
}{
507+
"nil": {
508+
in: nil,
509+
expected: nil,
510+
},
511+
"zero": {
512+
in: &tfplugin5.UpgradeResourceIdentity_Request{},
513+
expected: &tfprotov5.UpgradeResourceIdentityRequest{},
514+
},
515+
"RawIdentity": {
516+
in: &tfplugin5.UpgradeResourceIdentity_Request{
517+
RawIdentity: []byte("{}"),
518+
},
519+
expected: &tfprotov5.UpgradeResourceIdentityRequest{
520+
RawIdentity: testTfprotov5RawIdentity(t, []byte("{}")),
521+
},
522+
},
523+
"TypeName": {
524+
in: &tfplugin5.UpgradeResourceIdentity_Request{
525+
TypeName: "test",
526+
},
527+
expected: &tfprotov5.UpgradeResourceIdentityRequest{
528+
TypeName: "test",
529+
},
530+
},
531+
"Version": {
532+
in: &tfplugin5.UpgradeResourceIdentity_Request{
533+
Version: 123,
534+
},
535+
expected: &tfprotov5.UpgradeResourceIdentityRequest{
536+
Version: 123,
537+
},
538+
},
539+
}
540+
541+
for name, testCase := range testCases {
542+
name, testCase := name, testCase
543+
544+
t.Run(name, func(t *testing.T) {
545+
t.Parallel()
546+
547+
got := fromproto.UpgradeResourceIdentityRequest(testCase.in)
548+
549+
if diff := cmp.Diff(got, testCase.expected); diff != "" {
550+
t.Errorf("unexpected difference: %s", diff)
551+
}
552+
})
553+
}
554+
}
555+
460556
func TestValidateResourceTypeConfigRequest(t *testing.T) {
461557
t.Parallel()
462558

0 commit comments

Comments
 (0)