-
Notifications
You must be signed in to change notification settings - Fork 100
Expand file tree
/
Copy pathserve_opts.go
More file actions
84 lines (71 loc) · 2.54 KB
/
serve_opts.go
File metadata and controls
84 lines (71 loc) · 2.54 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package providerserver
import (
"context"
"fmt"
"strings"
)
// ServeOpts are options for serving the provider.
type ServeOpts struct {
// Address is the full address of the provider. Full address form has three
// parts separated by forward slashes (/): Hostname, namespace, and
// provider type ("name").
//
// For example: registry.terraform.io/hashicorp/random.
Address string
// Debug runs the provider in a mode acceptable for debugging and testing
// processes, such as delve, by managing the process lifecycle. Information
// needed for Terraform CLI to connect to the provider is output to stdout.
// os.Interrupt (Ctrl-c) can be used to stop the provider.
Debug bool
// ProtocolVersion is the protocol version that should be used when serving
// the provider. Either protocol version 5 or protocol version 6 can be
// used. Defaults to protocol version 6.
//
// Protocol version 5 has the following functionality limitations, which
// will raise an error during the GetProviderSchema or other RPCs:
//
// - tfsdk.Attribute cannot use Attributes field (nested attributes).
//
ProtocolVersion int
}
// Validate a given provider address. This is only used for the Address field
// to preserve backwards compatibility for the Name field.
//
// This logic is manually implemented over importing
// github.com/hashicorp/terraform-registry-address as its functionality such as
// ParseAndInferProviderSourceString and ParseRawProviderSourceString allow
// shorter address formats, which would then require post-validation anyways.
func (opts ServeOpts) validateAddress(_ context.Context) error {
addressParts := strings.Split(opts.Address, "/")
formatErr := fmt.Errorf("expected hostname/namespace/type format, got: %s", opts.Address)
if len(addressParts) != 3 {
return formatErr
}
if addressParts[0] == "" || addressParts[1] == "" || addressParts[2] == "" {
return formatErr
}
return nil
}
// Validation checks for provider defined ServeOpts.
//
// Current checks which return errors:
//
// - If Address is not set
// - Address is a valid full provider address
// - ProtocolVersion, if set, is 5 or 6
func (opts ServeOpts) validate(ctx context.Context) error {
if opts.Address == "" {
return fmt.Errorf("Address must be provided")
}
err := opts.validateAddress(ctx)
if err != nil {
return fmt.Errorf("unable to validate Address: %w", err)
}
switch opts.ProtocolVersion {
// 0 represents unset, which Serve will use default.
case 0, 5, 6:
default:
return fmt.Errorf("ProtocolVersion, if set, must be 5 or 6")
}
return nil
}