Skip to content

Commit

Permalink
Multiplex provider to support both SDKv2 and Plugin Framework
Browse files Browse the repository at this point in the history
  • Loading branch information
fairclothjm committed Oct 18, 2023
1 parent 4b202f5 commit 3e636e2
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 14 deletions.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
44 changes: 31 additions & 13 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
5 changes: 5 additions & 0 deletions schema/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package schema

import (
"github.com/hashicorp/terraform-plugin-go/tfprotov5"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

Expand All @@ -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()
}

0 comments on commit 3e636e2

Please sign in to comment.