Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tfprotov6: Resource Identity implementation #476

Merged
merged 5 commits into from
Feb 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changes/unreleased/FEATURES-20250210-175144.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
kind: FEATURES
body: 'tfprotov5+tfprotov6: Upgraded protocols and added types to support the new resource identity feature'
time: 2025-02-10T17:51:44.461603+01:00
custom:
Issue: "476"
5 changes: 5 additions & 0 deletions .changes/unreleased/NOTES-20250210-175414.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
kind: NOTES
body: 'tfprotov5+tfprotov6: An upcoming release will require the `GetResourceIdentitySchemas` and `UpgradeResourceIdentity` implementations as part of `ProviderServer`.'
time: 2025-02-10T17:54:14.054598+01:00
custom:
Issue: "476"
2 changes: 1 addition & 1 deletion tfprotov5/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ type GetResourceIdentitySchemasRequest struct{}
// GetResourceIdentitySchemasResponse represents a Terraform RPC response containing
// the provider's resource identity schemas.
type GetResourceIdentitySchemasResponse struct {
// ResourceSchemas is a map of resource names to the schema for the
// IdentitySchemas is a map of resource names to the schema for the
// identity specified for the resource. The name should be a
// resource name, and should be prefixed with your provider's shortname
// and an underscore. It should match the first label after `resource`
Expand Down
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
31 changes: 31 additions & 0 deletions tfprotov6/internal/fromproto/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,37 @@ 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 {

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(),
}
}
95 changes: 95 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 @@ -134,6 +142,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 @@ -212,6 +228,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 @@ -302,6 +326,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 @@ -376,6 +408,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 @@ -445,6 +485,61 @@ 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 {

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
Loading