-
Notifications
You must be signed in to change notification settings - Fork 0
/
provider.go
89 lines (75 loc) · 2.15 KB
/
provider.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
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
85
86
87
88
89
package border0
import (
"context"
"fmt"
border0client "github.com/borderzero/border0-go/client"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
type ProviderOption func(p *schema.Provider)
func Provider(options ...ProviderOption) *schema.Provider {
provider := &schema.Provider{
ConfigureContextFunc: providerConfigure,
Schema: map[string]*schema.Schema{
"auth_token": {
Type: schema.TypeString,
DefaultFunc: schema.EnvDefaultFunc("BORDER0_AUTH_TOKEN", ""),
Required: true,
Description: "The auth token used to authenticate with the Border0 API.",
Sensitive: true,
},
"base_url": {
Type: schema.TypeString,
DefaultFunc: schema.EnvDefaultFunc("BORDER0_BASE_URL", "https://api.border0.com/api/v1"),
Optional: true,
Description: "The URL of the Border0 API.",
},
},
ResourcesMap: map[string]*schema.Resource{
"border0_socket": resourceSocket(),
},
}
for _, option := range options {
option(provider)
}
return provider
}
func providerConfigure(_ context.Context, d *schema.ResourceData) (interface{}, diag.Diagnostics) {
authToken := d.Get("auth_token").(string)
baseURL := d.Get("base_url").(string)
if authToken == "" {
return nil, diag.Errorf("border0 provider credential is empty - set `auth_token`")
}
return border0client.New(
border0client.WithAuthToken(authToken),
border0client.WithBaseURL(baseURL),
), nil
}
func setValues(d *schema.ResourceData, values map[string]any) diag.Diagnostics {
for key, value := range values {
if err := d.Set(key, value); err != nil {
return diagnosticsError(err, "Failed to set %s", key)
}
}
return nil
}
func diagnosticsError(err error, message string, args ...interface{}) diag.Diagnostics {
var detail string
if err != nil {
detail = err.Error()
}
diags := []diag.Diagnostic{
{
Severity: diag.Error,
Summary: fmt.Sprintf(message, args...),
Detail: detail,
},
}
if clientError, ok := err.(border0client.Error); ok {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: clientError.Error(),
})
}
return diags
}