-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
104 lines (88 loc) · 2.12 KB
/
client.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package provisionclient
import (
"crypto/tls"
"encoding/base64"
"fmt"
"io"
"io/ioutil"
"net/http"
"strings"
"time"
)
// Define a custom type that implements the json.Unmarshaler interface.
type PVID string
func (n *PVID) UnmarshalJSON(b []byte) error {
// Convert the number to a string and store it in the Number value.
if b[0] == '"' {
*n = PVID(strings.Trim(string(b), "\""))
} else if string(b) == "null" {
*n = ""
} else {
*n = PVID(string(b))
}
return nil
}
// Client -
type Client struct {
HostURL string
HTTPClient *http.Client
Auth AuthStruct
DNS DNSMethods
Resources ResourceMethods
IPAM IPAMMethods
DHCP DHCPMethods
}
// AuthStruct -
type AuthStruct struct {
Username string `json:"username"`
Password string `json:"password"`
}
// NewClient -
func NewClient(host, username, password string, skipTLSVerify bool) (*Client, error) {
c := Client{
HTTPClient: &http.Client{
Timeout: 30 * time.Second,
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
TLSClientConfig: &tls.Config{
InsecureSkipVerify: skipTLSVerify,
},
}},
HostURL: strings.Trim(host, "/"),
}
c.Auth = AuthStruct{
Username: username,
Password: password,
}
c.DNS.Client = &c
c.Resources.Client = &c
c.IPAM.Client = &c
c.DHCP.Client = &c
return &c, nil
}
func (c *Client) doRequest(method, relative_url string, input_body io.Reader) ([]byte, error) {
url := c.HostURL + "/api/v2/" + strings.Trim(relative_url, "/")
req, err := http.NewRequest(method, url, input_body)
if err != nil {
return nil, err
}
auth := c.Auth.Username + ":" + c.Auth.Password
req.Header.Add("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(auth)))
req.Header.Add("Accept", "application/json")
if method != "GET" {
req.Header.Add("Content-Type", "application/json")
}
res, err := c.HTTPClient.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
if res.StatusCode > 299 {
return nil, fmt.Errorf("status: %d, body: %s", res.StatusCode, body)
}
return body, err
}