forked from euforia/go-infoblox
/
infoblox.go
78 lines (73 loc) · 1.82 KB
/
infoblox.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
// Implements an Infoblox DNS/DHCP appliance client library in Go
package infoblox
import (
"crypto/tls"
"fmt"
"net/http"
"net/url"
"strings"
)
var (
WAPI_VERSION = "1.4.1"
BASE_PATH = "/wapi/v" + WAPI_VERSION + "/"
DEBUG = false
)
// Implements a Infoblox WAPI client.
// https://192.168.2.200/wapidoc/#transport-and-authentication
type Client struct {
Host string
Password string
Username string
HttpClient *http.Client
}
// Creates a new Infoblox client with the supplied user/pass configuration.
// Supports the use of HTTP proxies through the $HTTP_PROXY env var.
// For example:
// export HTTP_PROXY=http://localhost:8888
//
// When using a proxy, disable TLS certificate verification with the following:
// sslVerify = false
func NewClient(host, username, password string, sslVerify bool) *Client {
var (
req, _ = http.NewRequest("GET", host, nil)
proxy, _ = http.ProxyFromEnvironment(req)
transport *http.Transport
tlsconfig *tls.Config
)
tlsconfig = &tls.Config{
InsecureSkipVerify: !sslVerify,
}
if tlsconfig.InsecureSkipVerify {
fmt.Println("WARNING: SSL cert verification disabled!")
}
transport = &http.Transport{
TLSClientConfig: tlsconfig,
}
if proxy != nil {
transport.Proxy = http.ProxyURL(proxy)
}
return &Client{
Host: host,
HttpClient: &http.Client{
Transport: transport,
},
Username: username,
Password: password,
}
}
// Sends a HTTP request through this instance's HTTP client.
func (c *Client) SendRequest(req *http.Request) (resp *APIResponse, err error) {
u := req.URL.String()
if !strings.HasPrefix(u, "http") {
u = fmt.Sprintf("%v%v", c.Host, u)
req.URL, err = url.Parse(u)
if err != nil {
return
}
}
req.SetBasicAuth(c.Username, c.Password)
var r *http.Response
r, err = c.HttpClient.Do(req)
resp = (*APIResponse)(r)
return
}