forked from ivoronin/terraform-provider-netbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
provider.go
62 lines (58 loc) · 1.74 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
package main
import (
"fmt"
"strings"
"crypto/tls"
"github.com/go-resty/resty/v2"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)
func Provider() *schema.Provider {
return &schema.Provider{
Schema: map[string]*schema.Schema{
"token": {
Type: schema.TypeString,
Required: true,
DefaultFunc: schema.EnvDefaultFunc("NETBOX_TOKEN", nil),
Description: "The token used to connect to NetBox.",
},
"base_url": {
Type: schema.TypeString,
Required: true,
DefaultFunc: schema.EnvDefaultFunc("NETBOX_BASE_URL", ""),
Description: "The NetBox Base API URL",
ValidateFunc: validateApiURL,
},
"cacert_file": {
Type: schema.TypeString,
Optional: true,
Default: "",
Description: "A file containing the ca certificate to use in case ssl certificate is not from a standard chain",
},
"insecure": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Disable SSL verification of API calls",
},
},
ResourcesMap: map[string]*schema.Resource{
"netbox_prefix": resourcePrefix(),
"netbox_ipaddress": resourceIPAddress(),
},
ConfigureFunc: providerConfigure,
}
}
func validateApiURL(value interface{}, key string) (ws []string, es []error) {
v := value.(string)
if !strings.HasSuffix(v, "/api") {
es = append(es, fmt.Errorf("base_url should end with /api"))
}
return
}
func providerConfigure(d *schema.ResourceData) (interface{}, error) {
client := resty.New()
client.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: d.Get("insecure").(bool)})
client.SetHostURL(d.Get("base_url").(string))
client.SetHeader("Authorization", fmt.Sprintf("Token %s", d.Get("token").(string)))
return client, nil
}