Skip to content

tfsdk: Introduce new ProviderServer functions, deprecate existing #294

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

Merged
merged 2 commits into from
Apr 21, 2022
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
11 changes: 11 additions & 0 deletions .changelog/pending.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
```release-note:note
tfsdk: The `NewProtocol6Server()` function has been deprecated in preference of `NewProtocol6ProviderServer()` and `NewProtocol6ProviderServerWithError()` functions, which will simplify muxing and testing implementations. The `NewProtocol6Server()` function will be removed in an upcoming minor release before v1.0.0.
```

```release-note:enhancement
tfsdk: Added `NewProtocol6ProviderServer()` function, which can be directly used with terraform-plugin-go and terraform-plugin-mux.
```

```release-note:enhancement
tfsdk: Added `NewProtocol6ProviderServerWithError()` function, which can be directly used with terraform-plugin-sdk acceptance testing.
```
27 changes: 27 additions & 0 deletions tfsdk/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,39 @@ type ServeOpts struct {

// NewProtocol6Server returns a tfprotov6.ProviderServer implementation based
// on the passed Provider implementation.
//
// Deprecated: Use NewProtocol6ProviderServer instead. This will be removed
// in an upcoming minor version before v1.0.0.
Comment on lines +42 to +43
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was going to ask if we really need to do this deprecation phase, but I did a quick GitHub global code search, and while it's in the order of ~70 usages, and it includes the HashiCorp repos, it's still 70 usages.

Yep, deprecation first it is.

func NewProtocol6Server(p Provider) tfprotov6.ProviderServer {
return &server{
p: p,
}
}

// Returns a protocol version 6 ProviderServer implementation based on the
// given Provider and suitable for usage with the terraform-plugin-go
// tf6server.Serve() function and various terraform-plugin-mux functions.
func NewProtocol6ProviderServer(p Provider) func() tfprotov6.ProviderServer {
return func() tfprotov6.ProviderServer {
return &server{
p: p,
}
}
}

// Returns a protocol version 6 ProviderServer implementation based on the
// given Provider and suitable for usage with the terraform-plugin-sdk
// acceptance testing helper/resource.TestCase.ProtoV6ProviderFactories.
//
// The error return is not currently used, but it may be in the future.
func NewProtocol6ProviderServerWithError(p Provider) (func() tfprotov6.ProviderServer, error) {
return func() tfprotov6.ProviderServer {
return &server{
p: p,
}
}, nil
}

// Serve serves a provider, blocking until the context is canceled.
func Serve(ctx context.Context, providerFunc func() Provider, opts ServeOpts) error {
var tf6serverOpts []tf6server.ServeOpt
Expand Down
33 changes: 33 additions & 0 deletions tfsdk/serve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,39 @@ import (
"github.com/hashicorp/terraform-plugin-go/tftypes"
)

func TestNewProtocol6ProviderServer(t *testing.T) {
provider := &testServeProvider{}

providerServerFunc := NewProtocol6ProviderServer(provider)
providerServer := providerServerFunc()

// Simple verification
_, err := providerServer.GetProviderSchema(context.Background(), &tfprotov6.GetProviderSchemaRequest{})

if err != nil {
t.Fatalf("unexpected error calling ProviderServer: %s", err)
}
}

func TestNewProtocol6ProviderServerWithError(t *testing.T) {
provider := &testServeProvider{}

providerServerFunc, err := NewProtocol6ProviderServerWithError(provider)

if err != nil {
t.Fatalf("unexpected error creating ProviderServer: %s", err)
}

providerServer := providerServerFunc()

// Simple verification
_, err = providerServer.GetProviderSchema(context.Background(), &tfprotov6.GetProviderSchemaRequest{})

if err != nil {
t.Fatalf("unexpected error calling ProviderServer: %s", err)
}
}

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

Expand Down