Skip to content

Commit 7a74c38

Browse files
apricotejooola
andauthored
refactor: move mux server setup to shared function (#771)
This refactors the code around #763 and adds a unit test to make sure that the issue does not creep back in. We are still not sure why this was not caught in the regular e2e tests. Co-authored-by: jooola <ljonas@riseup.net>
1 parent dc4d675 commit 7a74c38

File tree

4 files changed

+61
-45
lines changed

4 files changed

+61
-45
lines changed

hcloud/mux.go

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package hcloud
2+
3+
import (
4+
"context"
5+
6+
"github.com/hashicorp/terraform-plugin-framework/providerserver"
7+
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
8+
"github.com/hashicorp/terraform-plugin-mux/tf5to6server"
9+
"github.com/hashicorp/terraform-plugin-mux/tf6muxserver"
10+
)
11+
12+
func GetMuxedProvider(ctx context.Context) (func() tfprotov6.ProviderServer, error) {
13+
upgradedSdkServer, err := tf5to6server.UpgradeServer(
14+
ctx,
15+
Provider().GRPCProvider,
16+
)
17+
if err != nil {
18+
return nil, err
19+
}
20+
21+
providers := []func() tfprotov6.ProviderServer{
22+
providerserver.NewProtocol6(NewPluginProvider()),
23+
func() tfprotov6.ProviderServer {
24+
return upgradedSdkServer
25+
},
26+
}
27+
28+
muxServer, err := tf6muxserver.NewMuxServer(ctx, providers...)
29+
if err != nil {
30+
return nil, err
31+
}
32+
33+
return muxServer.ProviderServer, nil
34+
}

hcloud/mux_test.go

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package hcloud
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
func TestMuxedProviderSchema(t *testing.T) {
12+
providerFactory, err := GetMuxedProvider(context.Background())
13+
if err != nil {
14+
t.Fatal(err)
15+
}
16+
17+
resp, err := providerFactory().GetProviderSchema(context.Background(), &tfprotov6.GetProviderSchemaRequest{})
18+
if err != nil {
19+
t.Fatal(err)
20+
}
21+
22+
assert.Len(t, resp.Diagnostics, 0)
23+
}

internal/e2etests/testing.go

+2-22
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@ import (
55
"os"
66
"testing"
77

8-
"github.com/hashicorp/terraform-plugin-framework/providerserver"
98
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
10-
"github.com/hashicorp/terraform-plugin-mux/tf5to6server"
11-
"github.com/hashicorp/terraform-plugin-mux/tf6muxserver"
129
"github.com/hetznercloud/hcloud-go/hcloud"
1310
tfhcloud "github.com/hetznercloud/terraform-provider-hcloud/hcloud"
1411
)
@@ -38,29 +35,12 @@ func ProtoV6ProviderFactories() map[string]func() (tfprotov6.ProviderServer, err
3835
"hcloud": func() (tfprotov6.ProviderServer, error) {
3936
ctx := context.Background()
4037

41-
upgradedSdkServer, err := tf5to6server.UpgradeServer(
42-
ctx,
43-
tfhcloud.Provider().GRPCProvider,
44-
)
45-
46-
if err != nil {
47-
return nil, err
48-
}
49-
50-
providers := []func() tfprotov6.ProviderServer{
51-
providerserver.NewProtocol6(tfhcloud.NewPluginProvider()),
52-
func() tfprotov6.ProviderServer {
53-
return upgradedSdkServer
54-
},
55-
}
56-
57-
muxServer, err := tf6muxserver.NewMuxServer(ctx, providers...)
58-
38+
providerFactory, err := tfhcloud.GetMuxedProvider(ctx)
5939
if err != nil {
6040
return nil, err
6141
}
6242

63-
return muxServer.ProviderServer(), nil
43+
return providerFactory(), nil
6444
},
6545
}
6646
}

main.go

+2-23
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,7 @@ import (
55
"flag"
66
"log"
77

8-
"github.com/hashicorp/terraform-plugin-framework/providerserver"
9-
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
108
"github.com/hashicorp/terraform-plugin-go/tfprotov6/tf6server"
11-
"github.com/hashicorp/terraform-plugin-mux/tf5to6server"
12-
"github.com/hashicorp/terraform-plugin-mux/tf6muxserver"
139

1410
"github.com/hetznercloud/terraform-provider-hcloud/hcloud"
1511
)
@@ -22,24 +18,7 @@ func main() {
2218
flag.BoolVar(&debug, "debug", false, "set to true to run the provider with support for debuggers like delve")
2319
flag.Parse()
2420

25-
upgradedSdkServer, err := tf5to6server.UpgradeServer(
26-
ctx,
27-
hcloud.Provider().GRPCProvider,
28-
)
29-
30-
if err != nil {
31-
log.Fatal(err)
32-
}
33-
34-
providers := []func() tfprotov6.ProviderServer{
35-
providerserver.NewProtocol6(hcloud.NewPluginProvider()),
36-
func() tfprotov6.ProviderServer {
37-
return upgradedSdkServer
38-
},
39-
}
40-
41-
muxServer, err := tf6muxserver.NewMuxServer(ctx, providers...)
42-
21+
providerFactory, err := hcloud.GetMuxedProvider(ctx)
4322
if err != nil {
4423
log.Fatal(err)
4524
}
@@ -52,7 +31,7 @@ func main() {
5231

5332
err = tf6server.Serve(
5433
"registry.terraform.io/hetznercloud/hcloud",
55-
muxServer.ProviderServer,
34+
providerFactory,
5635
serveOpts...,
5736
)
5837

0 commit comments

Comments
 (0)