mirror of
https://github.com/opentofu/opentofu.git
synced 2025-02-25 18:45:20 -06:00
The main significant change here is that the package name for the proto definition is "tfplugin5", which is important because this name is part of the wire protocol for references to types defined in our package. Along with that, we also move the generated package into "internal" to make it explicit that importing the generated Go package from elsewhere is not the right approach for externally-implemented SDKs, which should instead vendor the proto definition they are using and generate their own stubs to ensure that the wire protocol is the only hard dependency between Terraform Core and plugins. After this is merged, any provider binaries built against our helper/schema package will need to be rebuilt so that they use the new "tfplugin5" package name instead of "proto". In a future commit we will include more elaborate and organized documentation on how an external codebase might make use of our RPC interface definition to implement an SDK, but the primary concern here is to ensure we have the right wire package name before release.
44 lines
1.2 KiB
Go
44 lines
1.2 KiB
Go
package resource
|
|
|
|
import (
|
|
"context"
|
|
"net"
|
|
"time"
|
|
|
|
"github.com/hashicorp/terraform/helper/plugin"
|
|
proto "github.com/hashicorp/terraform/internal/tfplugin5"
|
|
tfplugin "github.com/hashicorp/terraform/plugin"
|
|
"github.com/hashicorp/terraform/providers"
|
|
"github.com/hashicorp/terraform/terraform"
|
|
"google.golang.org/grpc"
|
|
"google.golang.org/grpc/test/bufconn"
|
|
)
|
|
|
|
// GRPCTestProvider takes a legacy ResourceProvider, wraps it in the new GRPC
|
|
// shim and starts it in a grpc server using an inmem connection. It returns a
|
|
// GRPCClient for this new server to test the shimmed resource provider.
|
|
func GRPCTestProvider(rp terraform.ResourceProvider) providers.Interface {
|
|
listener := bufconn.Listen(256 * 1024)
|
|
grpcServer := grpc.NewServer()
|
|
|
|
p := plugin.NewGRPCProviderServerShim(rp)
|
|
proto.RegisterProviderServer(grpcServer, p)
|
|
|
|
go grpcServer.Serve(listener)
|
|
|
|
conn, err := grpc.Dial("", grpc.WithDialer(func(string, time.Duration) (net.Conn, error) {
|
|
return listener.Dial()
|
|
}), grpc.WithInsecure())
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
var pp tfplugin.GRPCProviderPlugin
|
|
client, _ := pp.GRPCClient(context.Background(), nil, conn)
|
|
|
|
grpcClient := client.(*tfplugin.GRPCProvider)
|
|
grpcClient.TestListener = listener
|
|
|
|
return grpcClient
|
|
}
|