forked from limberger/terraform-provider-netbox
/
config.go
executable file
·89 lines (66 loc) · 2.1 KB
/
config.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 netbox
import (
"fmt"
"strings"
log "github.com/sirupsen/logrus"
"net/url"
"github.com/go-openapi/strfmt"
"github.com/Preskton/go-netbox/netbox/client"
openapi_runtimeclient "github.com/go-openapi/runtime/client"
)
// Config provides the configuration for the Netbox provider.
type Config struct {
// The application ID required for API requests. This needs to be created
// in the NETBOX console (Admin->Users->Tokens). It can also be supplied via the NETBOX_APP_ID
// environment variable.
AppID string
// The API endpoint. This defaults to http://localhost/api, and can also be
// supplied via the NETBOX_ENDPOINT_ADDR environment variable.
Endpoint string
}
type ProviderNetboxClient struct {
client *client.NetBox
configuration Config
}
// Client does the heavy lifting of establishing a base Open API client to Netbox.
func (c *Config) Client() (interface{}, error) {
cfg := Config{
AppID: c.AppID,
Endpoint: c.Endpoint,
}
log.WithFields(
log.Fields{
"uri": cfg.Endpoint,
},
).Debug("Initializing Netbox client")
parsedURI, uriParseError := url.ParseRequestURI(cfg.Endpoint)
if uriParseError != nil {
log.WithFields(
log.Fields{
"uri": cfg.Endpoint,
"error": uriParseError,
},
).Error("Failed to parse URI")
return nil, uriParseError
}
parsedScheme := strings.ToLower(parsedURI.Scheme)
if parsedScheme == "" {
parsedScheme = "http"
}
desiredRuntimeClientSchemes := []string{parsedScheme}
log.WithFields(
log.Fields{
"host": parsedURI.Host,
"schemes": desiredRuntimeClientSchemes,
},
).Debug("Initializing open API runtime client")
runtimeClient := openapi_runtimeclient.New(parsedURI.Host, client.DefaultBasePath, desiredRuntimeClientSchemes)
runtimeClient.DefaultAuthentication = openapi_runtimeclient.APIKeyAuth("Authorization", "header", fmt.Sprintf("Token %v", cfg.AppID))
runtimeClient.SetLogger(log.StandardLogger())
netboxClient := client.New(runtimeClient, strfmt.Default)
terraformNetboxClient := ProviderNetboxClient{
client: netboxClient,
configuration: cfg,
}
return &terraformNetboxClient, nil
}