diff --git a/go.mod b/go.mod index f663a93403..60ba3b5ec9 100644 --- a/go.mod +++ b/go.mod @@ -24,6 +24,8 @@ require ( github.com/hashicorp/go-secure-stdlib/awsutil v0.2.3 github.com/hashicorp/go-secure-stdlib/parseutil v0.1.7 github.com/hashicorp/go-version v1.6.0 + github.com/hashicorp/terraform-plugin-go v0.19.0 + github.com/hashicorp/terraform-plugin-mux v0.12.0 github.com/hashicorp/terraform-plugin-sdk/v2 v2.29.0 github.com/hashicorp/vault v1.11.3 github.com/hashicorp/vault-plugin-auth-jwt v0.17.0 @@ -130,7 +132,6 @@ require ( github.com/hashicorp/serf v0.9.7 // indirect github.com/hashicorp/terraform-exec v0.19.0 // indirect github.com/hashicorp/terraform-json v0.17.1 // indirect - github.com/hashicorp/terraform-plugin-go v0.19.0 // indirect github.com/hashicorp/terraform-plugin-log v0.9.0 // indirect github.com/hashicorp/terraform-registry-address v0.2.2 // indirect github.com/hashicorp/terraform-svchost v0.1.1 // indirect diff --git a/go.sum b/go.sum index 62c4ed2803..4759f10bbd 100644 --- a/go.sum +++ b/go.sum @@ -1583,6 +1583,8 @@ github.com/hashicorp/terraform-plugin-go v0.19.0 h1:BuZx/6Cp+lkmiG0cOBk6Zps0Cb2t github.com/hashicorp/terraform-plugin-go v0.19.0/go.mod h1:EhRSkEPNoylLQntYsk5KrDHTZJh9HQoumZXbOGOXmec= github.com/hashicorp/terraform-plugin-log v0.9.0 h1:i7hOA+vdAItN1/7UrfBqBwvYPQ9TFvymaRGZED3FCV0= github.com/hashicorp/terraform-plugin-log v0.9.0/go.mod h1:rKL8egZQ/eXSyDqzLUuwUYLVdlYeamldAHSxjUFADow= +github.com/hashicorp/terraform-plugin-mux v0.12.0 h1:TJlmeslQ11WlQtIFAfth0vXx+gSNgvMEng2Rn9z3WZY= +github.com/hashicorp/terraform-plugin-mux v0.12.0/go.mod h1:8MR0AgmV+Q03DIjyrAKxXyYlq2EUnYBQP8gxAAA0zeM= github.com/hashicorp/terraform-plugin-sdk/v2 v2.29.0 h1:wcOKYwPI9IorAJEBLzgclh3xVolO7ZorYd6U1vnok14= github.com/hashicorp/terraform-plugin-sdk/v2 v2.29.0/go.mod h1:qH/34G25Ugdj5FcM95cSoXzUgIbgfhVLXCcEcYaMwq8= github.com/hashicorp/terraform-registry-address v0.2.2 h1:lPQBg403El8PPicg/qONZJDC6YlgCVbWDtNmmZKtBno= diff --git a/main.go b/main.go index f7103afa0c..ba2eae222b 100644 --- a/main.go +++ b/main.go @@ -4,41 +4,59 @@ package main import ( + "context" "flag" "log" - "github.com/hashicorp/terraform-plugin-sdk/v2/plugin" - + "github.com/hashicorp/terraform-plugin-go/tfprotov5" + "github.com/hashicorp/terraform-plugin-go/tfprotov5/tf5server" + "github.com/hashicorp/terraform-plugin-mux/tf5muxserver" "github.com/hashicorp/terraform-provider-vault/generated" "github.com/hashicorp/terraform-provider-vault/schema" "github.com/hashicorp/terraform-provider-vault/vault" ) func main() { - p := schema.NewProvider(vault.Provider()) + ctx := context.Background() + + sdkv2Provider := schema.NewProvider(vault.Provider()) for name, resource := range generated.DataSourceRegistry { - p.RegisterDataSource(name, vault.UpdateSchemaResource(resource)) + sdkv2Provider.RegisterDataSource(name, vault.UpdateSchemaResource(resource)) } for name, resource := range generated.ResourceRegistry { - p.RegisterResource(name, vault.UpdateSchemaResource(resource)) + sdkv2Provider.RegisterResource(name, vault.UpdateSchemaResource(resource)) } - serveOpts := &plugin.ServeOpts{ - ProviderFunc: p.SchemaProvider, + providers := []func() tfprotov5.ProviderServer{ + // providerserver.NewProtocol5(provider.New()), // Example terraform-plugin-framework provider + sdkv2Provider.GRPCProvider, } + muxServer, err := tf5muxserver.NewMuxServer(ctx, providers...) + if err != nil { + log.Fatal(err) + } + + var serveOpts []tf5server.ServeOpt + var debug bool flag.BoolVar(&debug, "debug", false, "set to true to run the provider with support for debuggers like delve") flag.Parse() - if debug { - serveOpts.Debug = debug - serveOpts.ProviderAddr = "hashicorp/vault" + serveOpts = append(serveOpts, tf5server.WithManagedDebug()) } - // fix duplicate timestamp and incorrect level messages + err = tf5server.Serve( + "registry.terraform.io/hashicorp/vault", + muxServer.ProviderServer, + serveOpts..., + ) + + if err != nil { + log.Fatal(err) + } + + // fix duplicate timestamp and incorrect level messages for legacy sdk v2 // https://developer.hashicorp.com/terraform/plugin/log/writing#legacy-log-troubleshooting log.SetFlags(log.Flags() &^ (log.Ldate | log.Ltime)) - - plugin.Serve(serveOpts) } diff --git a/schema/provider.go b/schema/provider.go index b976c86d52..174160f169 100644 --- a/schema/provider.go +++ b/schema/provider.go @@ -4,6 +4,7 @@ package schema import ( + "github.com/hashicorp/terraform-plugin-go/tfprotov5" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" ) @@ -28,3 +29,7 @@ func (p *Provider) RegisterResource(name string, resource *schema.Resource) { func (p *Provider) SchemaProvider() *schema.Provider { return p.provider } + +func (p *Provider) GRPCProvider() tfprotov5.ProviderServer { + return p.provider.GRPCProvider() +}