/
config.go
60 lines (52 loc) · 1.34 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
package aviatrix
import (
"crypto/tls"
"crypto/x509"
"fmt"
"io/ioutil"
"log"
"net/http"
"github.com/AviatrixSystems/terraform-provider-aviatrix/v3/goaviatrix"
)
// Config contains the configuration for the Aviatrix provider
// (Username, Password, and Controller IP)
type Config struct {
Username string
Password string
ControllerIP string
VerifyCert bool
PathToCACert string
IgnoreTags *goaviatrix.IgnoreTagsConfig
}
// Client gets the Aviatrix client to access the Controller
// Arguments:
//
// None
//
// Returns:
//
// the aviatrix client (from goaviatrix)
// error (if any)
func (c *Config) Client() (*goaviatrix.Client, error) {
tr := &http.Transport{
Proxy: http.ProxyFromEnvironment,
TLSClientConfig: &tls.Config{
InsecureSkipVerify: !c.VerifyCert,
},
}
if c.VerifyCert && c.PathToCACert != "" {
caCert, err := ioutil.ReadFile(c.PathToCACert)
if err != nil {
return nil, fmt.Errorf(err.Error())
}
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM(caCert)
tr.TLSClientConfig.RootCAs = caCertPool
}
client, err := goaviatrix.NewClient(c.Username, c.Password, c.ControllerIP, &http.Client{Transport: tr}, c.IgnoreTags)
log.Printf("[INFO] Aviatrix Client configured for use")
if client == nil || err != nil {
log.Printf("[ERROR] unable to create client: %s", err)
}
return client, err
}