forked from pulse-vadc/go-vtm
/
virtual_traffic_manager.go
240 lines (221 loc) · 6.7 KB
/
virtual_traffic_manager.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// Copyright (C) 2018-2019, Pulse Secure, LLC.
// Licensed under the terms of the MPL 2.0. See LICENSE file for details.
package vtm
import (
"bytes"
"crypto/tls"
"encoding/json"
"io"
"io/ioutil"
"log"
"net/http"
"net/http/httputil"
"net/url"
"strings"
"time"
)
const (
TEXT_ONLY_OBJ = true
STANDARD_OBJ = false
)
type vtmObjectChild struct {
Name string `json:"name"`
Href string `json:"href"`
}
type vtmObjectChildren struct {
Children []vtmObjectChild `json:"children"`
}
type vtmErrorResponse struct {
ErrorId string `json:"error_id"`
ErrorText string `json:"error_text"`
ErrorInfo interface{} `json:"error_info"`
}
//********************************************
type vtmConnector struct {
url string
client *http.Client
username string
password string
verifySslCert bool
textOnly bool
contentType string
expectedCodes map[string][]int
readOnly bool
verbose bool
}
func (c vtmConnector) getChildConnector(path string) *vtmConnector {
newUrl := c.url + path
conn := newConnector(newUrl, c.username, c.password, c.verifySslCert, c.verbose, c.client)
return conn
}
func (c vtmConnector) get() (io.Reader, bool) {
request, err := http.NewRequest("GET", c.url, nil)
if c.verbose {
reqDump, _ := httputil.DumpRequestOut(request, false)
log.Printf("REST GET REQUEST: %s\n", reqDump)
}
request.SetBasicAuth(c.username, c.password)
response, err := c.client.Do(request)
if err != nil {
panic(err)
}
defer response.Body.Close()
if c.verbose {
resDump, _ := httputil.DumpResponse(response, true)
log.Printf("REST GET RESPONSE: %q\n", resDump)
}
responseBody, err := ioutil.ReadAll(response.Body)
if err != nil {
panic(err)
}
bodyReader := bytes.NewReader(responseBody)
if response.StatusCode == 200 {
return bodyReader, true
}
return bodyReader, false
}
func (c vtmConnector) put(body string, isTextObject bool) (io.Reader, bool) {
var contentType string
if isTextObject == true {
contentType = "application/octet-stream"
} else {
contentType = "application/json"
}
request, err := http.NewRequest("PUT", c.url, strings.NewReader(body))
request.Header.Set("Content-Type", contentType)
if c.verbose {
reqDump, _ := httputil.DumpRequestOut(request, true)
log.Printf("REST PUT REQUEST: %q\n", reqDump)
}
request.SetBasicAuth(c.username, c.password)
response, err := c.client.Do(request)
if err != nil {
panic(err)
}
defer response.Body.Close()
if c.verbose {
resDump, _ := httputil.DumpResponse(response, true)
log.Printf("REST PUT RESPONSE: %q\n", resDump)
}
responseBody, err := ioutil.ReadAll(response.Body)
if err != nil {
panic(err)
}
bodyReader := bytes.NewReader(responseBody)
if response.StatusCode >= 200 && response.StatusCode < 300 {
return bodyReader, true
}
return bodyReader, false
}
func (c vtmConnector) delete() (io.Reader, bool) {
request, err := http.NewRequest("DELETE", c.url, nil)
if c.verbose {
reqDump, _ := httputil.DumpRequestOut(request, false)
log.Printf("REST DELETE REQUEST: %s\n", reqDump)
}
request.SetBasicAuth(c.username, c.password)
response, err := c.client.Do(request)
if err != nil {
panic(err)
}
defer response.Body.Close()
if c.verbose {
resDump, _ := httputil.DumpResponse(response, false)
log.Printf("REST DELETE RESPONSE: %s\n", resDump)
}
responseBody, err := ioutil.ReadAll(response.Body)
if err != nil {
panic(err)
}
bodyReader := bytes.NewReader(responseBody)
if response.StatusCode == 204 {
return bodyReader, true
}
return bodyReader, false
}
func newConnector(url, username, password string, verifySslCert, verbose bool, client *http.Client) *vtmConnector {
if client == nil {
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: !verifySslCert},
}
client = &http.Client{Transport: tr, Timeout: 60 * time.Second}
}
conn := &vtmConnector{
url: url,
username: username,
password: password,
verifySslCert: verifySslCert,
verbose: verbose,
client: client,
}
return conn
}
/*
VirtualTrafficManager is the central struct in the go-vtm library through which all tasks are performed.
*/
type VirtualTrafficManager struct {
connector *vtmConnector
}
func (tm VirtualTrafficManager) testConnectivityOnce() (ok bool, err *vtmErrorResponse) {
ok = false
defer func() {
if r := recover(); r != nil {
rue := r.(*url.Error)
err = &vtmErrorResponse{
ErrorId: rue.Err.Error(),
ErrorText: rue.Err.Error(),
}
}
}()
data, ok := tm.connector.get()
if ok != true {
object := new(vtmErrorResponse)
json.NewDecoder(data).Decode(object)
return false, object
}
return ok, nil
}
func (tm VirtualTrafficManager) testConnectivity() (bool, *vtmErrorResponse) {
var err *vtmErrorResponse
for i := 1; i <= 3; i++ {
var ok bool
ok, err = tm.testConnectivityOnce()
if ok == true {
return true, nil
}
time.Sleep(time.Duration(i) * time.Second)
}
return false, err
}
/*
NewVirtualTrafficManager creates an instance of VirtualTrafficManager and returns it, together with its reachability status.
Params:
url (string) The base URL of the target vTM, upto, but not including, the API verion portion.
eg. For direct connection to a vTM:
https://my-vtm-1:9070/api
For connection via a Services Director proxy:
https://my-sd-1:8100/api/tmcm/<VER>/instances/<INSTANCE_ID>
username (string) Username to use for the connection.
ie. For direct connection to a vTM:
Username on vTM with sufficient permissions to perform required operations.
For connections via a Services Director proxy:
Username on ServicesDirector with sufficient permissions to perform required operations.
password (string) Password to use for the connection.
ie. For direct connection to a vTM:
vTM password for the user specified in the 'username' parameter.
For connections via a Services Director proxy:
Services Director password for the user specified in the 'username' parameter.
verifySslCert (bool) Whether to perform verification on on the SSL certificate presented by the RESP API.
verbose (bool) Whether to write verbose logs to STDOUT.
Returns:
*VirtualTrafficManager The newly-instantiated object
bool true if the target vTM is reachable with the provided parameters, else false
*vtmErrorResponse An error object if failed to create new VirtualTrafficManager, else nil
*/
func NewVirtualTrafficManager(url, username, password string, verifySslCert, verbose bool) (*VirtualTrafficManager, bool, *vtmErrorResponse) {
vtm := new(VirtualTrafficManager)
conn := newConnector(url, username, password, verifySslCert, verbose, nil)
vtm.connector = conn
contactable, contactErr := vtm.testConnectivity()
return vtm, contactable, contactErr
}