/
providerserver.go
59 lines (50 loc) · 1.63 KB
/
providerserver.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package providerserver
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
"github.com/hashicorp/terraform-plugin-go/tfprotov6/tf6server"
)
// Returns a protocol version 6 ProviderServer implementation based on the
// given Provider and suitable for usage with the terraform-plugin-go
// tf6server.Serve() function and various terraform-plugin-mux functions.
func NewProtocol6(p tfsdk.Provider) func() tfprotov6.ProviderServer {
return func() tfprotov6.ProviderServer {
return &tfsdk.Server{
Provider: p,
}
}
}
// Returns a protocol version 6 ProviderServer implementation based on the
// given Provider and suitable for usage with the terraform-plugin-sdk
// acceptance testing helper/resource.TestCase.ProtoV6ProviderFactories.
//
// The error return is not currently used, but it may be in the future.
func NewProtocol6WithError(p tfsdk.Provider) func() (tfprotov6.ProviderServer, error) {
return func() (tfprotov6.ProviderServer, error) {
return &tfsdk.Server{
Provider: p,
}, nil
}
}
// Serve serves a provider, blocking until the context is canceled.
func Serve(ctx context.Context, providerFunc func() tfsdk.Provider, opts ServeOpts) error {
err := opts.validate(ctx)
if err != nil {
return fmt.Errorf("unable to validate ServeOpts: %w", err)
}
var tf6serverOpts []tf6server.ServeOpt
if opts.Debug {
tf6serverOpts = append(tf6serverOpts, tf6server.WithManagedDebug())
}
return tf6server.Serve(
opts.Address,
func() tfprotov6.ProviderServer {
return &tfsdk.Server{
Provider: providerFunc(),
}
},
tf6serverOpts...,
)
}