Skip to content

Commit

Permalink
add resource identity support to protocol v6
Browse files Browse the repository at this point in the history
copied from protocol v5
  • Loading branch information
ansgarm committed Feb 10, 2025
1 parent 6ebe79b commit 1d335b3
Show file tree
Hide file tree
Showing 23 changed files with 1,259 additions and 9 deletions.
10 changes: 10 additions & 0 deletions tfprotov6/internal/fromproto/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ func GetProviderSchemaRequest(in *tfplugin6.GetProviderSchema_Request) *tfprotov
return resp
}

func GetResourceIdentitySchemasRequest(in *tfplugin6.GetResourceIdentitySchemas_Request) *tfprotov6.GetResourceIdentitySchemasRequest {
if in == nil {
return nil
}

resp := &tfprotov6.GetResourceIdentitySchemasRequest{}

return resp
}

func ValidateProviderConfigRequest(in *tfplugin6.ValidateProviderConfig_Request) *tfprotov6.ValidateProviderConfigRequest {
if in == nil {
return nil
Expand Down
32 changes: 32 additions & 0 deletions tfprotov6/internal/fromproto/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,38 @@ func TestGetProviderSchemaRequest(t *testing.T) {
}
}

func TestGetResourceIdentitySchemasRequest(t *testing.T) {
t.Parallel()

testCases := map[string]struct {
in *tfplugin6.GetResourceIdentitySchemas_Request
expected *tfprotov6.GetResourceIdentitySchemasRequest
}{
"nil": {
in: nil,
expected: nil,
},
"zero": {
in: &tfplugin6.GetResourceIdentitySchemas_Request{},
expected: &tfprotov6.GetResourceIdentitySchemasRequest{},
},
}

for name, testCase := range testCases {
name, testCase := name, testCase

t.Run(name, func(t *testing.T) {
t.Parallel()

got := fromproto.GetResourceIdentitySchemasRequest(testCase.in)

if diff := cmp.Diff(got, testCase.expected); diff != "" {
t.Errorf("unexpected difference: %s", diff)
}
})
}
}

func TestConfigureProviderRequest(t *testing.T) {
t.Parallel()

Expand Down
20 changes: 20 additions & 0 deletions tfprotov6/internal/fromproto/raw_identity.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package fromproto

import (
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
)

func RawIdentity(in []byte) *tfprotov6.RawIdentity {
if in == nil {
return nil
}

resp := &tfprotov6.RawIdentity{
JSON: in,
}

return resp
}
18 changes: 18 additions & 0 deletions tfprotov6/internal/fromproto/raw_identity_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package fromproto_test

import (
"testing"

"github.com/hashicorp/terraform-plugin-go/tfprotov6"
)

func testTfprotov6RawIdentity(t *testing.T, json []byte) *tfprotov6.RawIdentity {
t.Helper()

return &tfprotov6.RawIdentity{
JSON: json,
}
}
31 changes: 25 additions & 6 deletions tfprotov6/internal/fromproto/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,20 @@ func UpgradeResourceStateRequest(in *tfplugin6.UpgradeResourceState_Request) *tf
return resp
}

func UpgradeResourceIdentityRequest(in *tfplugin6.UpgradeResourceIdentity_Request) *tfprotov6.UpgradeResourceIdentityRequest {
if in == nil {
return nil
}

resp := &tfprotov6.UpgradeResourceIdentityRequest{
RawIdentity: RawIdentity(in.RawIdentity),
TypeName: in.TypeName,
Version: in.Version,
}

return resp
}

func ReadResourceRequest(in *tfplugin6.ReadResource_Request) *tfprotov6.ReadResourceRequest {
if in == nil {
return nil
Expand All @@ -47,6 +61,7 @@ func ReadResourceRequest(in *tfplugin6.ReadResource_Request) *tfprotov6.ReadReso
ProviderMeta: DynamicValue(in.ProviderMeta),
TypeName: in.TypeName,
ClientCapabilities: ReadResourceClientCapabilities(in.ClientCapabilities),
CurrentIdentity: ResourceIdentityData(in.CurrentIdentity),
}

return resp
Expand All @@ -65,6 +80,7 @@ func PlanResourceChangeRequest(in *tfplugin6.PlanResourceChange_Request) *tfprot
ProviderMeta: DynamicValue(in.ProviderMeta),
TypeName: in.TypeName,
ClientCapabilities: PlanResourceChangeClientCapabilities(in.ClientCapabilities),
PriorIdentity: ResourceIdentityData(in.PriorIdentity),
}

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

resp := &tfprotov6.ApplyResourceChangeRequest{
Config: DynamicValue(in.Config),
PlannedPrivate: in.PlannedPrivate,
PlannedState: DynamicValue(in.PlannedState),
PriorState: DynamicValue(in.PriorState),
ProviderMeta: DynamicValue(in.ProviderMeta),
TypeName: in.TypeName,
Config: DynamicValue(in.Config),
PlannedPrivate: in.PlannedPrivate,
PlannedState: DynamicValue(in.PlannedState),
PriorState: DynamicValue(in.PriorState),
ProviderMeta: DynamicValue(in.ProviderMeta),
TypeName: in.TypeName,
PlannedIdentity: ResourceIdentityData(in.PlannedIdentity),
}

return resp
Expand All @@ -96,6 +113,7 @@ func ImportResourceStateRequest(in *tfplugin6.ImportResourceState_Request) *tfpr
TypeName: in.TypeName,
ID: in.Id,
ClientCapabilities: ImportResourceStateClientCapabilities(in.ClientCapabilities),
Identity: ResourceIdentityData(in.Identity),
}

return resp
Expand All @@ -113,6 +131,7 @@ func MoveResourceStateRequest(in *tfplugin6.MoveResourceState_Request) *tfprotov
SourceState: RawState(in.SourceState),
SourceTypeName: in.SourceTypeName,
TargetTypeName: in.TargetTypeName,
SourceIdentity: ResourceIdentityData(in.SourceIdentity),
}

return resp
Expand Down
21 changes: 21 additions & 0 deletions tfprotov6/internal/fromproto/resource_identity_data.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package fromproto

import (
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
"github.com/hashicorp/terraform-plugin-go/tfprotov6/internal/tfplugin6"
)

func ResourceIdentityData(in *tfplugin6.ResourceIdentityData) *tfprotov6.ResourceIdentityData {
if in == nil {
return nil
}

resp := &tfprotov6.ResourceIdentityData{
IdentityData: DynamicValue(in.IdentityData),
}

return resp
}
20 changes: 20 additions & 0 deletions tfprotov6/internal/fromproto/resource_identity_data_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package fromproto_test

import (
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
"github.com/hashicorp/terraform-plugin-go/tfprotov6/internal/tfplugin6"
"github.com/hashicorp/terraform-plugin-go/tfprotov6/internal/toproto"
)

func testTfplugin6ResourceIdentityData() *tfplugin6.ResourceIdentityData {
return toproto.ResourceIdentityData(testTfprotov6ResourceIdentityData())
}

func testTfprotov6ResourceIdentityData() *tfprotov6.ResourceIdentityData {
return &tfprotov6.ResourceIdentityData{
IdentityData: testTfprotov6DynamicValue(),
}
}
96 changes: 96 additions & 0 deletions tfprotov6/internal/fromproto/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@ func TestApplyResourceChangeRequest(t *testing.T) {
TypeName: "test",
},
},
"PlannedIdentity": {
in: &tfplugin6.ApplyResourceChange_Request{
PlannedIdentity: testTfplugin6ResourceIdentityData(),
},
expected: &tfprotov6.ApplyResourceChangeRequest{
PlannedIdentity: testTfprotov6ResourceIdentityData(),
},
},
}

for name, testCase := range testCases {
Expand Down Expand Up @@ -136,6 +144,14 @@ func TestImportResourceStateRequest(t *testing.T) {
},
},
},
"Identity": {
in: &tfplugin6.ImportResourceState_Request{
Identity: testTfplugin6ResourceIdentityData(),
},
expected: &tfprotov6.ImportResourceStateRequest{
Identity: testTfprotov6ResourceIdentityData(),
},
},
}

for name, testCase := range testCases {
Expand Down Expand Up @@ -216,6 +232,14 @@ func TestMoveResourceStateRequest(t *testing.T) {
TargetTypeName: "test",
},
},
"SourceIdentity": {
in: &tfplugin6.MoveResourceState_Request{
SourceIdentity: testTfplugin6ResourceIdentityData(),
},
expected: &tfprotov6.MoveResourceStateRequest{
SourceIdentity: testTfprotov6ResourceIdentityData(),
},
},
}

for name, testCase := range testCases {
Expand Down Expand Up @@ -308,6 +332,14 @@ func TestPlanResourceChangeRequest(t *testing.T) {
},
},
},
"PriorIdentity": {
in: &tfplugin6.PlanResourceChange_Request{
PriorIdentity: testTfplugin6ResourceIdentityData(),
},
expected: &tfprotov6.PlanResourceChangeRequest{
PriorIdentity: testTfprotov6ResourceIdentityData(),
},
},
}

for name, testCase := range testCases {
Expand Down Expand Up @@ -384,6 +416,14 @@ func TestReadResourceRequest(t *testing.T) {
},
},
},
"CurrentIdentity": {
in: &tfplugin6.ReadResource_Request{
CurrentIdentity: testTfplugin6ResourceIdentityData(),
},
expected: &tfprotov6.ReadResourceRequest{
CurrentIdentity: testTfprotov6ResourceIdentityData(),
},
},
}

for name, testCase := range testCases {
Expand Down Expand Up @@ -457,6 +497,62 @@ func TestUpgradeResourceStateRequest(t *testing.T) {
}
}

func TestUpgradeResourceIdentityRequest(t *testing.T) {
t.Parallel()

testCases := map[string]struct {
in *tfplugin6.UpgradeResourceIdentity_Request
expected *tfprotov6.UpgradeResourceIdentityRequest
}{
"nil": {
in: nil,
expected: nil,
},
"zero": {
in: &tfplugin6.UpgradeResourceIdentity_Request{},
expected: &tfprotov6.UpgradeResourceIdentityRequest{},
},
"RawIdentity": {
in: &tfplugin6.UpgradeResourceIdentity_Request{
RawIdentity: []byte("{}"),
},
expected: &tfprotov6.UpgradeResourceIdentityRequest{
RawIdentity: testTfprotov6RawIdentity(t, []byte("{}")),
},
},
"TypeName": {
in: &tfplugin6.UpgradeResourceIdentity_Request{
TypeName: "test",
},
expected: &tfprotov6.UpgradeResourceIdentityRequest{
TypeName: "test",
},
},
"Version": {
in: &tfplugin6.UpgradeResourceIdentity_Request{
Version: 123,
},
expected: &tfprotov6.UpgradeResourceIdentityRequest{
Version: 123,
},
},
}

for name, testCase := range testCases {
name, testCase := name, testCase

t.Run(name, func(t *testing.T) {
t.Parallel()

got := fromproto.UpgradeResourceIdentityRequest(testCase.in)

if diff := cmp.Diff(got, testCase.expected); diff != "" {
t.Errorf("unexpected difference: %s", diff)
}
})
}
}

func TestValidateResourceConfigRequest(t *testing.T) {
t.Parallel()

Expand Down
17 changes: 17 additions & 0 deletions tfprotov6/internal/toproto/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,23 @@ func GetProviderSchema_Response(in *tfprotov6.GetProviderSchemaResponse) *tfplug
return resp
}

func GetResourceIdentitySchemas_Response(in *tfprotov6.GetResourceIdentitySchemasResponse) *tfplugin6.GetResourceIdentitySchemas_Response {
if in == nil {
return nil
}

resp := &tfplugin6.GetResourceIdentitySchemas_Response{
Diagnostics: Diagnostics(in.Diagnostics),
IdentitySchemas: make(map[string]*tfplugin6.ResourceIdentitySchema, len(in.IdentitySchemas)),
}

for name, schema := range in.IdentitySchemas {
resp.IdentitySchemas[name] = ResourceIdentitySchema(schema)
}

return resp
}

func ValidateProviderConfig_Response(in *tfprotov6.ValidateProviderConfigResponse) *tfplugin6.ValidateProviderConfig_Response {
if in == nil {
return nil
Expand Down
Loading

0 comments on commit 1d335b3

Please sign in to comment.