Skip to content

Commit

Permalink
split client capabilities per RPC
Browse files Browse the repository at this point in the history
  • Loading branch information
austinvalle committed May 3, 2024
1 parent 8648bec commit 3a4fbc8
Show file tree
Hide file tree
Showing 24 changed files with 562 additions and 72 deletions.
45 changes: 41 additions & 4 deletions tfprotov5/client_capabilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,47 @@

package tfprotov5

// ClientCapabilities allows Terraform to publish information regarding optionally supported
// protocol features, such as forward-compatible Terraform behavior changes.
type ClientCapabilities struct {
// DeferralAllowed signals that the Terraform client is able to
// ConfigureProviderClientCapabilities allows Terraform to publish information
// regarding optionally supported protocol features for the ConfigureProvider RPC,
// such as forward-compatible Terraform behavior changes.
type ConfigureProviderClientCapabilities struct {
// DeferralAllowed signals that the request from Terraform is able to
// handle deferred responses from the provider.
DeferralAllowed bool
}

// ReadDataSourceClientCapabilities allows Terraform to publish information
// regarding optionally supported protocol features for the ReadDataSource RPC,
// such as forward-compatible Terraform behavior changes.
type ReadDataSourceClientCapabilities struct {
// DeferralAllowed signals that the request from Terraform is able to
// handle deferred responses from the provider.
DeferralAllowed bool
}

// ReadResourceClientCapabilities allows Terraform to publish information
// regarding optionally supported protocol features for the ReadResource RPC,
// such as forward-compatible Terraform behavior changes.
type ReadResourceClientCapabilities struct {
// DeferralAllowed signals that the request from Terraform is able to
// handle deferred responses from the provider.
DeferralAllowed bool
}

// PlanResourceChangeClientCapabilities allows Terraform to publish information
// regarding optionally supported protocol features for the PlanResourceChange RPC,
// such as forward-compatible Terraform behavior changes.
type PlanResourceChangeClientCapabilities struct {
// DeferralAllowed signals that the request from Terraform is able to
// handle deferred responses from the provider.
DeferralAllowed bool
}

// ImportResourceStateClientCapabilities allows Terraform to publish information
// regarding optionally supported protocol features for the ImportResourceState RPC,
// such as forward-compatible Terraform behavior changes.
type ImportResourceStateClientCapabilities struct {
// DeferralAllowed signals that the request from Terraform is able to
// handle deferred responses from the provider.
DeferralAllowed bool
}
6 changes: 3 additions & 3 deletions tfprotov5/data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ type ReadDataSourceRequest struct {
// This configuration will have known values for all fields.
ProviderMeta *DynamicValue

// ClientCapabilities defines optionally supported protocol features for
// the Terraform client, such as forward-compatible Terraform behavior changes.
ClientCapabilities *ClientCapabilities
// ClientCapabilities defines optionally supported protocol features for the
// ReadDataSource RPC, such as forward-compatible Terraform behavior changes.
ClientCapabilities *ReadDataSourceClientCapabilities
}

// ReadDataSourceResponse is the response from the provider about the current
Expand Down
52 changes: 50 additions & 2 deletions tfprotov5/internal/fromproto/client_capabilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,60 @@ import (
"github.com/hashicorp/terraform-plugin-go/tfprotov5/internal/tfplugin5"
)

func ClientCapabilities(in *tfplugin5.ClientCapabilities) *tfprotov5.ClientCapabilities {
func ConfigureProviderClientCapabilities(in *tfplugin5.ClientCapabilities) *tfprotov5.ConfigureProviderClientCapabilities {
if in == nil {
return nil
}

resp := &tfprotov5.ClientCapabilities{
resp := &tfprotov5.ConfigureProviderClientCapabilities{
DeferralAllowed: in.DeferralAllowed,
}

return resp
}

func ReadDataSourceClientCapabilities(in *tfplugin5.ClientCapabilities) *tfprotov5.ReadDataSourceClientCapabilities {
if in == nil {
return nil
}

resp := &tfprotov5.ReadDataSourceClientCapabilities{
DeferralAllowed: in.DeferralAllowed,
}

return resp
}

func ReadResourceClientCapabilities(in *tfplugin5.ClientCapabilities) *tfprotov5.ReadResourceClientCapabilities {
if in == nil {
return nil
}

resp := &tfprotov5.ReadResourceClientCapabilities{
DeferralAllowed: in.DeferralAllowed,
}

return resp
}

func PlanResourceChangeClientCapabilities(in *tfplugin5.ClientCapabilities) *tfprotov5.PlanResourceChangeClientCapabilities {
if in == nil {
return nil
}

resp := &tfprotov5.PlanResourceChangeClientCapabilities{
DeferralAllowed: in.DeferralAllowed,
}

return resp
}

func ImportResourceStateClientCapabilities(in *tfplugin5.ClientCapabilities) *tfprotov5.ImportResourceStateClientCapabilities {
if in == nil {
return nil
}

resp := &tfprotov5.ImportResourceStateClientCapabilities{
DeferralAllowed: in.DeferralAllowed,
}

Expand Down
170 changes: 165 additions & 5 deletions tfprotov5/internal/fromproto/client_capabilities_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,26 @@ import (
"github.com/hashicorp/terraform-plugin-go/tfprotov5/internal/tfplugin5"
)

func TestClientCapabilities(t *testing.T) {
func TestConfigureProviderClientCapabilities(t *testing.T) {
t.Parallel()

testCases := map[string]struct {
in *tfplugin5.ClientCapabilities
expected *tfprotov5.ClientCapabilities
expected *tfprotov5.ConfigureProviderClientCapabilities
}{
"nil": {
in: nil,
expected: nil,
},
"zero": {
in: &tfplugin5.ClientCapabilities{},
expected: &tfprotov5.ClientCapabilities{},
expected: &tfprotov5.ConfigureProviderClientCapabilities{},
},
"DeferralAllowed": {
in: &tfplugin5.ClientCapabilities{
DeferralAllowed: true,
},
expected: &tfprotov5.ClientCapabilities{
expected: &tfprotov5.ConfigureProviderClientCapabilities{
DeferralAllowed: true,
},
},
Expand All @@ -43,7 +43,167 @@ func TestClientCapabilities(t *testing.T) {
t.Run(name, func(t *testing.T) {
t.Parallel()

got := fromproto.ClientCapabilities(testCase.in)
got := fromproto.ConfigureProviderClientCapabilities(testCase.in)

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

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

testCases := map[string]struct {
in *tfplugin5.ClientCapabilities
expected *tfprotov5.ReadDataSourceClientCapabilities
}{
"nil": {
in: nil,
expected: nil,
},
"zero": {
in: &tfplugin5.ClientCapabilities{},
expected: &tfprotov5.ReadDataSourceClientCapabilities{},
},
"DeferralAllowed": {
in: &tfplugin5.ClientCapabilities{
DeferralAllowed: true,
},
expected: &tfprotov5.ReadDataSourceClientCapabilities{
DeferralAllowed: true,
},
},
}

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

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

got := fromproto.ReadDataSourceClientCapabilities(testCase.in)

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

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

testCases := map[string]struct {
in *tfplugin5.ClientCapabilities
expected *tfprotov5.ReadResourceClientCapabilities
}{
"nil": {
in: nil,
expected: nil,
},
"zero": {
in: &tfplugin5.ClientCapabilities{},
expected: &tfprotov5.ReadResourceClientCapabilities{},
},
"DeferralAllowed": {
in: &tfplugin5.ClientCapabilities{
DeferralAllowed: true,
},
expected: &tfprotov5.ReadResourceClientCapabilities{
DeferralAllowed: true,
},
},
}

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

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

got := fromproto.ReadResourceClientCapabilities(testCase.in)

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

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

testCases := map[string]struct {
in *tfplugin5.ClientCapabilities
expected *tfprotov5.PlanResourceChangeClientCapabilities
}{
"nil": {
in: nil,
expected: nil,
},
"zero": {
in: &tfplugin5.ClientCapabilities{},
expected: &tfprotov5.PlanResourceChangeClientCapabilities{},
},
"DeferralAllowed": {
in: &tfplugin5.ClientCapabilities{
DeferralAllowed: true,
},
expected: &tfprotov5.PlanResourceChangeClientCapabilities{
DeferralAllowed: true,
},
},
}

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

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

got := fromproto.PlanResourceChangeClientCapabilities(testCase.in)

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

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

testCases := map[string]struct {
in *tfplugin5.ClientCapabilities
expected *tfprotov5.ImportResourceStateClientCapabilities
}{
"nil": {
in: nil,
expected: nil,
},
"zero": {
in: &tfplugin5.ClientCapabilities{},
expected: &tfprotov5.ImportResourceStateClientCapabilities{},
},
"DeferralAllowed": {
in: &tfplugin5.ClientCapabilities{
DeferralAllowed: true,
},
expected: &tfprotov5.ImportResourceStateClientCapabilities{
DeferralAllowed: true,
},
},
}

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

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

got := fromproto.ImportResourceStateClientCapabilities(testCase.in)

if diff := cmp.Diff(got, testCase.expected); diff != "" {
t.Errorf("unexpected difference: %s", diff)
Expand Down
2 changes: 1 addition & 1 deletion tfprotov5/internal/fromproto/data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func ReadDataSourceRequest(in *tfplugin5.ReadDataSource_Request) *tfprotov5.Read
Config: DynamicValue(in.Config),
ProviderMeta: DynamicValue(in.ProviderMeta),
TypeName: in.TypeName,
ClientCapabilities: ClientCapabilities(in.ClientCapabilities),
ClientCapabilities: ReadDataSourceClientCapabilities(in.ClientCapabilities),
}

return resp
Expand Down
2 changes: 1 addition & 1 deletion tfprotov5/internal/fromproto/data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func TestReadDataSourceRequest(t *testing.T) {
},
},
expected: &tfprotov5.ReadDataSourceRequest{
ClientCapabilities: &tfprotov5.ClientCapabilities{
ClientCapabilities: &tfprotov5.ReadDataSourceClientCapabilities{
DeferralAllowed: true,
},
},
Expand Down
2 changes: 1 addition & 1 deletion tfprotov5/internal/fromproto/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func ConfigureProviderRequest(in *tfplugin5.Configure_Request) *tfprotov5.Config
resp := &tfprotov5.ConfigureProviderRequest{
Config: DynamicValue(in.Config),
TerraformVersion: in.TerraformVersion,
ClientCapabilities: ClientCapabilities(in.ClientCapabilities),
ClientCapabilities: ConfigureProviderClientCapabilities(in.ClientCapabilities),
}

return resp
Expand Down
2 changes: 1 addition & 1 deletion tfprotov5/internal/fromproto/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func TestConfigureProviderRequest(t *testing.T) {
},
},
expected: &tfprotov5.ConfigureProviderRequest{
ClientCapabilities: &tfprotov5.ClientCapabilities{
ClientCapabilities: &tfprotov5.ConfigureProviderClientCapabilities{
DeferralAllowed: true,
},
},
Expand Down
6 changes: 3 additions & 3 deletions tfprotov5/internal/fromproto/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func ReadResourceRequest(in *tfplugin5.ReadResource_Request) *tfprotov5.ReadReso
Private: in.Private,
ProviderMeta: DynamicValue(in.ProviderMeta),
TypeName: in.TypeName,
ClientCapabilities: ClientCapabilities(in.ClientCapabilities),
ClientCapabilities: ReadResourceClientCapabilities(in.ClientCapabilities),
}

return resp
Expand All @@ -63,7 +63,7 @@ func PlanResourceChangeRequest(in *tfplugin5.PlanResourceChange_Request) *tfprot
ProposedNewState: DynamicValue(in.ProposedNewState),
ProviderMeta: DynamicValue(in.ProviderMeta),
TypeName: in.TypeName,
ClientCapabilities: ClientCapabilities(in.ClientCapabilities),
ClientCapabilities: PlanResourceChangeClientCapabilities(in.ClientCapabilities),
}

return resp
Expand Down Expand Up @@ -94,7 +94,7 @@ func ImportResourceStateRequest(in *tfplugin5.ImportResourceState_Request) *tfpr
resp := &tfprotov5.ImportResourceStateRequest{
TypeName: in.TypeName,
ID: in.Id,
ClientCapabilities: ClientCapabilities(in.ClientCapabilities),
ClientCapabilities: ImportResourceStateClientCapabilities(in.ClientCapabilities),
}

return resp
Expand Down
Loading

0 comments on commit 3a4fbc8

Please sign in to comment.