Repository: sacloud/terraform-provider-sakuracloud
Pull request #932 (2022-07-26 08:21:23)

terraform-plugin-mux対応
SDK/Framework両方に対応するためにterraform-plugin-muxを導入する。



Patch:
diff --git a/go.mod b/go.mod
index fb23e7ae..30c7d771 100644
--- a/go.mod
+++ b/go.mod
@@ -5,6 +5,8 @@ go 1.18
 require (
 	github.com/goccy/go-yaml v1.9.5
 	github.com/hashicorp/go-multierror v1.1.1
+	github.com/hashicorp/terraform-plugin-go v0.12.0
+	github.com/hashicorp/terraform-plugin-mux v0.7.0
 	github.com/hashicorp/terraform-plugin-sdk/v2 v2.19.0
 	github.com/mitchellh/go-homedir v1.1.0
 	github.com/sacloud/api-client-go v0.2.0
@@ -52,7 +54,6 @@ require (
 	github.com/hashicorp/logutils v1.0.0 // indirect
 	github.com/hashicorp/terraform-exec v0.17.2 // indirect
 	github.com/hashicorp/terraform-json v0.14.0 // indirect
-	github.com/hashicorp/terraform-plugin-go v0.12.0 // indirect
 	github.com/hashicorp/terraform-plugin-log v0.6.0 // indirect
 	github.com/hashicorp/terraform-registry-address v0.0.0-20220623143253-7d51757b572c // indirect
 	github.com/hashicorp/terraform-svchost v0.0.0-20200729002733-f050f53b9734 // indirect
diff --git a/go.sum b/go.sum
index f9b8f6e2..a24f42f8 100644
--- a/go.sum
+++ b/go.sum
@@ -238,6 +238,8 @@ github.com/hashicorp/terraform-plugin-go v0.12.0 h1:6wW9mT1dSs0Xq4LR6HXj1heQ5ovr
 github.com/hashicorp/terraform-plugin-go v0.12.0/go.mod h1:kwhmaWHNDvT1B3QiSJdAtrB/D4RaKSY/v3r2BuoWK4M=
 github.com/hashicorp/terraform-plugin-log v0.6.0 h1:/Vq78uSIdUSZ3iqDc9PESKtwt8YqNKN6u+khD+lLjuw=
 github.com/hashicorp/terraform-plugin-log v0.6.0/go.mod h1:p4R1jWBXRTvL4odmEkFfDdhUjHf9zcs/BCoNHAc7IK4=
+github.com/hashicorp/terraform-plugin-mux v0.7.0 h1:wRbSYzg+v2sn5Mdee0UKm4YTt4wJG0LfSwtgNuBkglY=
+github.com/hashicorp/terraform-plugin-mux v0.7.0/go.mod h1:Ae30Mc5lz4d1awtiCbHP0YyvgBeiQ00Q1nAq0U3lb+I=
 github.com/hashicorp/terraform-plugin-sdk/v2 v2.19.0 h1:7gDAcfto/C4Cjtf90SdukQshsxdMxJ/P69QxiF3digI=
 github.com/hashicorp/terraform-plugin-sdk/v2 v2.19.0/go.mod h1:/WYikYjhKB7c2j1HmXZhRsAARldRb4M38bLCLOhC3so=
 github.com/hashicorp/terraform-registry-address v0.0.0-20220623143253-7d51757b572c h1:D8aRO6+mTqHfLsK/BC3j5OAoogv1WLRWzY1AaTo3rBg=
diff --git a/main.go b/main.go
index f7688453..38646999 100644
--- a/main.go
+++ b/main.go
@@ -15,25 +15,45 @@
 package main
 
 import (
+	"context"
 	"flag"
+	"log"
 
-	"github.com/hashicorp/terraform-plugin-sdk/v2/plugin"
+	"github.com/hashicorp/terraform-plugin-go/tfprotov5/tf5server"
+	"github.com/hashicorp/terraform-plugin-mux/tf5muxserver"
 	"github.com/sacloud/terraform-provider-sakuracloud/sakuracloud"
 )
 
 func main() {
-	var debugMode bool
-
-	flag.BoolVar(&debugMode, "debug", false, "set to true to run the provider with support for debuggers like delve")
+	debugFlag := flag.Bool("debug", false, "Start provider in debug mode.")
 	flag.Parse()
 
-	opts := &plugin.ServeOpts{
-		ProviderFunc: sakuracloud.Provider,
+	serverFactory, err := sakuracloud.ProtoV5ProviderServerFactory(context.Background())
+	if err != nil {
+		log.Fatal(err)
+	}
+
+	muxServer, err := tf5muxserver.NewMuxServer(context.Background(), serverFactory)
+	if err != nil {
+		log.Fatal(err)
 	}
 
-	if debugMode {
-		opts.ProviderAddr = "registry.terraform.io/sacloud/sakuracloud"
-		opts.Debug = true
+	var serveOpts []tf5server.ServeOpt
+	if *debugFlag {
+		serveOpts = append(serveOpts, tf5server.WithManagedDebug())
+	}
+
+	logFlags := log.Flags()
+	logFlags = logFlags &^ (log.Ldate | log.Ltime)
+	log.SetFlags(logFlags)
+
+	err = tf5server.Serve(
+		"registry.terraform.io/sacloud/sakuracloud",
+		muxServer.ProviderServer,
+		serveOpts...,
+	)
+
+	if err != nil {
+		log.Fatal(err)
 	}
-	plugin.Serve(opts)
 }
diff --git a/sakuracloud/factory.go b/sakuracloud/factory.go
new file mode 100644
index 00000000..c4dc0df2
--- /dev/null
+++ b/sakuracloud/factory.go
@@ -0,0 +1,36 @@
+// Copyright 2016-2022 The sacloud/terraform-provider-sakuracloud Authors
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package sakuracloud
+
+import (
+	"context"
+
+	"github.com/hashicorp/terraform-plugin-go/tfprotov5"
+	"github.com/hashicorp/terraform-plugin-mux/tf5muxserver"
+)
+
+func ProtoV5ProviderServerFactory(ctx context.Context) (func() tfprotov5.ProviderServer, error) {
+	providers := []func() tfprotov5.ProviderServer{
+		Provider().GRPCProvider,
+	}
+
+	muxServer, err := tf5muxserver.NewMuxServer(ctx, providers...)
+
+	if err != nil {
+		return nil, err
+	}
+
+	return muxServer.ProviderServer, nil
+}


Test patch:
diff --git a/sakuracloud/provider_test.go b/sakuracloud/provider_test.go
index afb2f6aa..686915ca 100644
--- a/sakuracloud/provider_test.go
+++ b/sakuracloud/provider_test.go
@@ -15,13 +15,17 @@
 package sakuracloud
 
 import (
+	"context"
 	"os"
 	"testing"
 
+	"github.com/hashicorp/terraform-plugin-go/tfprotov5"
 	"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
 )
 
 var testAccProviderFactories map[string]func() (*schema.Provider, error)
+var ProtoV5ProviderFactories map[string]func() (tfprotov5.ProviderServer, error)
+
 var testAccProvider *schema.Provider
 var (
 	testDefaultTargetZone   = "is1b"
@@ -37,6 +41,15 @@ func init() {
 	testAccProviderFactories = map[string]func() (*schema.Provider, error){
 		"sakuracloud": func() (*schema.Provider, error) { return testAccProvider, nil },
 	}
+	ProtoV5ProviderFactories = map[string]func() (tfprotov5.ProviderServer, error){
+		"sakuracloud": func() (tfprotov5.ProviderServer, error) {
+			providerServerFactory, err := ProtoV5ProviderServerFactory(context.Background())
+			if err != nil {
+				return nil, err
+			}
+			return providerServerFactory(), nil
+		},
+	}
 }
 
 func TestProvider(t *testing.T) {