From 3f3ad23f70b7ec16c25dd4f0e03b5e5a4f95712d Mon Sep 17 00:00:00 2001 From: Tanmay Rustagi <88379306+tanmay-db@users.noreply.github.com> Date: Wed, 17 Jul 2024 11:31:09 +0200 Subject: [PATCH] [Feature] Added support to use protocol version 6 provider server for SDK plugin (#3719) ## Changes Upgrade SDK plugin to use protocol version 6 as this will be used further for introducing plugin framework. We need to mux them (i.e. support both sdkv2 and plugin framework) until all resources are migrated to plugin framework. Reference for PR on main branch: https://github.com/databricks/terraform-provider-databricks/pull/3714, this PR against the main branch will be used to get the binary for testing with data team. ## Tests All Unit and Integration test passed. - [x] `make test` run locally - [ ] relevant change in `docs/` folder - [ ] covered with integration tests in `internal/acceptance` - [x] relevant acceptance tests are passing - [ ] using Go SDK --- go.mod | 3 ++- go.sum | 2 ++ main.go | 58 ++++++++++++++++++++++++++++++++++++++++++++------------- 3 files changed, 49 insertions(+), 14 deletions(-) diff --git a/go.mod b/go.mod index 8daba7b88d..30427dec11 100644 --- a/go.mod +++ b/go.mod @@ -9,7 +9,9 @@ require ( github.com/hashicorp/hcl v1.0.0 github.com/hashicorp/hcl/v2 v2.21.0 github.com/hashicorp/terraform-plugin-framework v1.9.0 + github.com/hashicorp/terraform-plugin-go v0.23.0 github.com/hashicorp/terraform-plugin-log v0.9.0 + github.com/hashicorp/terraform-plugin-mux v0.16.0 github.com/hashicorp/terraform-plugin-sdk/v2 v2.34.0 github.com/stretchr/testify v1.9.0 github.com/zclconf/go-cty v1.14.4 @@ -49,7 +51,6 @@ require ( github.com/hashicorp/logutils v1.0.0 // indirect github.com/hashicorp/terraform-exec v0.21.0 // indirect github.com/hashicorp/terraform-json v0.22.1 // indirect - github.com/hashicorp/terraform-plugin-go v0.23.0 // indirect github.com/hashicorp/terraform-registry-address v0.2.3 // indirect github.com/hashicorp/terraform-svchost v0.1.1 // indirect github.com/hashicorp/yamux v0.1.1 // indirect diff --git a/go.sum b/go.sum index 9add2781c0..46a0f91844 100644 --- a/go.sum +++ b/go.sum @@ -136,6 +136,8 @@ github.com/hashicorp/terraform-plugin-go v0.23.0 h1:AALVuU1gD1kPb48aPQUjug9Ir/12 github.com/hashicorp/terraform-plugin-go v0.23.0/go.mod h1:1E3Cr9h2vMlahWMbsSEcNrOCxovCZhOOIXjFHbjc/lQ= 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.16.0 h1:RCzXHGDYwUwwqfYYWJKBFaS3fQsWn/ZECEiW7p2023I= +github.com/hashicorp/terraform-plugin-mux v0.16.0/go.mod h1:PF79mAsPc8CpusXPfEVa4X8PtkB+ngWoiUClMrNZlYo= github.com/hashicorp/terraform-plugin-sdk/v2 v2.34.0 h1:kJiWGx2kiQVo97Y5IOGR4EMcZ8DtMswHhUuFibsCQQE= github.com/hashicorp/terraform-plugin-sdk/v2 v2.34.0/go.mod h1:sl/UoabMc37HA6ICVMmGO+/0wofkVIRxf+BMb/dnoIg= github.com/hashicorp/terraform-registry-address v0.2.3 h1:2TAiKJ1A3MAkZlH1YI/aTVcLZRu7JseiXNRHbOAyoTI= diff --git a/main.go b/main.go index 277d8fefd2..b64453db94 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + "context" "fmt" "log" "os" @@ -8,9 +9,20 @@ import ( "github.com/databricks/terraform-provider-databricks/common" "github.com/databricks/terraform-provider-databricks/exporter" "github.com/databricks/terraform-provider-databricks/provider" - "github.com/hashicorp/terraform-plugin-sdk/v2/plugin" + "github.com/hashicorp/terraform-plugin-go/tfprotov6" + "github.com/hashicorp/terraform-plugin-go/tfprotov6/tf6server" + "github.com/hashicorp/terraform-plugin-mux/tf5to6server" + "github.com/hashicorp/terraform-plugin-mux/tf6muxserver" ) +const startMessageFormat = `Databricks Terraform Provider + +Version %s + +https://registry.terraform.io/providers/databricks/databricks/latest/docs + +` + func main() { log.SetFlags(0) if len(os.Args) > 1 && os.Args[1] == "version" { @@ -24,20 +36,40 @@ func main() { } return } - var debug bool - if len(os.Args) > 1 && os.Args[1] == "debug" { - debug = true - } - log.Printf(`Databricks Terraform Provider -Version %s + log.Printf(startMessageFormat, common.Version()) -https://registry.terraform.io/providers/databricks/databricks/latest/docs + sdkPluginProvider := provider.DatabricksProvider() + + upgradedSdkPluginProvider, err := tf5to6server.UpgradeServer( + context.Background(), + sdkPluginProvider.GRPCProvider, + ) + if err != nil { + log.Fatal(err) + } -`, common.Version()) - plugin.Serve(&plugin.ServeOpts{ - ProviderFunc: provider.DatabricksProvider, - ProviderAddr: "registry.terraform.io/databricks/databricks", - Debug: debug, + ctx := context.Background() + muxServer, err := tf6muxserver.NewMuxServer(ctx, func() tfprotov6.ProviderServer { + return upgradedSdkPluginProvider }) + + if err != nil { + log.Fatal(err) + } + + var serveOpts []tf6server.ServeOpt + if len(os.Args) > 1 && os.Args[1] == "debug" { // debug mode + serveOpts = append(serveOpts, tf6server.WithManagedDebug()) + } + + err = tf6server.Serve( + "registry.terraform.io/databricks/databricks", + muxServer.ProviderServer, + serveOpts..., + ) + + if err != nil { + log.Fatal(err) + } }