|
| 1 | +--- |
| 2 | +page_title: 'Plugin Development - Combining Protocol Version 5 Providers' |
| 3 | +description: >- |
| 4 | + Use the tf5muxserver package in terraform-plugin-mux to combine protocol version 5 providers. |
| 5 | +--- |
| 6 | + |
| 7 | +# Combining Protocol Version 5 Providers |
| 8 | + |
| 9 | +The [`tf5muxserver`](https://pkg.go.dev/github.com/hashicorp/terraform-plugin-mux/tf5muxserver) package enables combining any number of [protocol version 5 provider servers](/terraform/plugin/how-terraform-works#protocol-version-5) into a single server. |
| 10 | + |
| 11 | +Use this package to: |
| 12 | + |
| 13 | +* Support multiple development teams across separate codebases. |
| 14 | +* Support multiple provider SDK implementations. For example, you could downgrade an existing [terraform-plugin-framework](/terraform/plugin/framework) provider to protocol version 5 and then combine it with one or more providers built with [terraform-plugin-sdk/v2](/terraform/plugin/sdkv2). |
| 15 | + |
| 16 | +## Compatibility |
| 17 | + |
| 18 | +Protocol version 5 provider servers are compatible with Terraform CLI 0.12 and later. |
| 19 | + |
| 20 | +The following provider server implementations are supported: |
| 21 | + |
| 22 | +* [terraform-plugin-sdk/v2](/terraform/plugin/sdkv2): A higher-level SDK that makes Terraform provider development easier by abstracting implementation details. |
| 23 | +* [terraform-plugin-go tf5server](https://pkg.go.dev/github.com/hashicorp/terraform-plugin-go/tfprotov5/tf5server): A lower-level SDK to develop Terraform providers for more advanced use cases. |
| 24 | +* [tf6to5server](/terraform/plugin/mux/translating-protocol-version-6-to-5): A package to translate protocol version 6 providers into protocol version 5. |
| 25 | + |
| 26 | +## Requirements |
| 27 | + |
| 28 | +To be combined, provider servers must meet the following requirements: |
| 29 | + |
| 30 | +* All provider schemas (except block `MinItems`/`MaxItems`) must match. |
| 31 | +* All provider meta schemas must match. |
| 32 | +* Only one provider may implement each resource and data source. |
| 33 | + |
| 34 | +If publishing to the [Terraform Registry](/terraform/registry), set `metadata.protocol_versions` to `["5.0"]` in the [Terraform Registry manifest file](/terraform/registry/providers/publishing#terraform-registry-manifest-file). |
| 35 | + |
| 36 | +## Code Implementation |
| 37 | + |
| 38 | +Use the [`tf5muxserver.NewMuxServer()`](https://pkg.go.dev/github.com/hashicorp/terraform-plugin-mux/tf5muxserver#NewMuxServer) function to create a combined provider server. Most providers implement this method in the provider `main()` function of the root directory `main.go` file or within a shared internal package to enable [testing for the combined provider](#testing-implementation). You can use [`tf5server.WithManagedDebug()`](https://pkg.go.dev/github.com/hashicorp/terraform-plugin-go/tfprotov5/tf5server#WithManagedDebug) to enable debugger support. |
| 39 | + |
| 40 | +The following example implements `tf5muxserver.NewMuxServer()` in a `main()` function. |
| 41 | + |
| 42 | +```go |
| 43 | +package main |
| 44 | + |
| 45 | +import ( |
| 46 | + "context" |
| 47 | + "flag" |
| 48 | + "log" |
| 49 | + |
| 50 | + "github.com/example/terraform-provider-example/internal/provider1" |
| 51 | + "github.com/example/terraform-provider-example/internal/provider2" |
| 52 | + "github.com/example/terraform-provider-example/internal/framework_provider" |
| 53 | + "github.com/hashicorp/terraform-plugin-framework/providerserver" |
| 54 | + "github.com/hashicorp/terraform-plugin-go/tfprotov5" |
| 55 | + "github.com/hashicorp/terraform-plugin-go/tfprotov5/tf5server" |
| 56 | + "github.com/hashicorp/terraform-plugin-mux/tf5muxserver" |
| 57 | +) |
| 58 | + |
| 59 | +var ( |
| 60 | + // Version can be updated by goreleaser on release |
| 61 | + version string = "dev" |
| 62 | +) |
| 63 | + |
| 64 | +func main() { |
| 65 | + debugFlag := flag.Bool("debug", false, "Start provider in debug mode.") |
| 66 | + flag.Parse() |
| 67 | + |
| 68 | + ctx := context.Background() |
| 69 | + providers := []func() tfprotov5.ProviderServer{ |
| 70 | + // Example terraform-plugin-sdk/v2 providers |
| 71 | + provider1.New(version)().GRPCProvider, |
| 72 | + provider2.New(version)().GRPCProvider, |
| 73 | + |
| 74 | + // Example terraform-plugin-framework provider |
| 75 | + providerserver.NewProtocol5( |
| 76 | + framework_provider.New(version)(), |
| 77 | + ), |
| 78 | + } |
| 79 | + |
| 80 | + muxServer, err := tf5muxserver.NewMuxServer(ctx, providers...) |
| 81 | + |
| 82 | + if err != nil { |
| 83 | + log.Fatal(err) |
| 84 | + } |
| 85 | + |
| 86 | + var serveOpts []tf5server.ServeOpt |
| 87 | + |
| 88 | + if *debugFlag { |
| 89 | + serveOpts = append(serveOpts, tf5server.WithManagedDebug()) |
| 90 | + } |
| 91 | + |
| 92 | + err = tf5server.Serve( |
| 93 | + "registry.terraform.io/example/example", |
| 94 | + muxServer.ProviderServer, |
| 95 | + serveOpts..., |
| 96 | + ) |
| 97 | + |
| 98 | + if err != nil { |
| 99 | + log.Fatal(err) |
| 100 | + } |
| 101 | +} |
| 102 | +``` |
| 103 | + |
| 104 | +## Testing Implementation |
| 105 | + |
| 106 | +You can test individual providers using the same [acceptance tests](/terraform/plugin/testing/acceptance-tests) as before. Set the `ProtoV5ProviderFactories` field of `TestCase` with an instance of the mux server, instead of declaring the provider with other `TestCase` fields such as `ProviderFactories`. |
| 107 | + |
| 108 | +The following example uses the acceptance testing framework to create a test for two providers. |
| 109 | + |
| 110 | +```go |
| 111 | +resource.Test(t, resource.TestCase{ |
| 112 | + // ... other TestCase fields ... |
| 113 | + ProtoV5ProviderFactories: map[string]func() (tfprotov5.ProviderServer, error) { |
| 114 | + "example": func() (tfprotov5.ProviderServer, error) { |
| 115 | + ctx := context.Background() |
| 116 | + providers := []func() tfprotov5.ProviderServer{ |
| 117 | + // Example terraform-plugin-sdk/v2 providers |
| 118 | + provider1.New(version)().GRPCProvider, |
| 119 | + provider2.New(version)().GRPCProvider, |
| 120 | + |
| 121 | + // Example terraform-plugin-framework provider |
| 122 | + providerserver.NewProtocol5( |
| 123 | + framework_provider.New(version)(), |
| 124 | + ), |
| 125 | + } |
| 126 | + |
| 127 | + muxServer, err := tf5muxserver.NewMuxServer(ctx, providers...) |
| 128 | + |
| 129 | + if err != nil { |
| 130 | + return nil, err |
| 131 | + } |
| 132 | + |
| 133 | + return muxServer.ProviderServer(), nil |
| 134 | + }, |
| 135 | + }, |
| 136 | +}) |
| 137 | +``` |
| 138 | + |
| 139 | +Refer to the [acceptance tests](/terraform/plugin/testing/acceptance-tests) documentation for more details. |
0 commit comments