-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathntnxAPI.go
227 lines (159 loc) · 4.69 KB
/
ntnxAPI.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
package ntnxAPI
import (
log "github.com/Sirupsen/logrus"
"bytes"
"crypto/tls"
"encoding/base64"
"io/ioutil"
"net/http"
"os"
"strconv"
)
const appVersion = "0.9 beta"
// NTNXConnection ...
type NTNXConnection struct {
NutanixHost string
Username string
Password string
SEnc string
HTTPClient http.Client
}
// EncodeCredentials ...
func EncodeCredentials(n *NTNXConnection) {
n.SEnc = base64.StdEncoding.EncodeToString([]byte(n.Username + ":" + n.Password))
}
// NutanixAHVurl ...
func NutanixAHVurl(n *NTNXConnection) string {
return "https://" + n.NutanixHost + ":9440/api/nutanix/v0.8/"
}
// NutanixRestURL ...
func NutanixRestURL(n *NTNXConnection) string {
return "https://" + n.NutanixHost + ":9440/PrismGateway/services/rest/v1/"
}
// DebugRequest ...
func DebugRequest(req *http.Request) {
log.Debug(req.Method)
log.Debug(req.URL)
log.Debug(req.Header)
}
// DebugResponse ...
func DebugResponse(resp *http.Response, bodyText []byte) {
log.Debug(resp.StatusCode, string(bodyText))
}
// CreateHTTPClient ...
func CreateHTTPClient(n *NTNXConnection) {
// Ignore certificats which can not be validated (Nutanix CE edition)
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
n.HTTPClient = http.Client{Transport: tr}
}
// NutanixAPIGet ...
func NutanixAPIGet(n *NTNXConnection, NutanixAPIurl string, NutanixURI string) ([]byte, int) {
var req *http.Request
var err error
req, err = http.NewRequest("GET", NutanixAPIurl+NutanixURI, nil)
req.Header.Set("Authorization", "Basic "+n.SEnc)
DebugRequest(req)
resp, err := n.HTTPClient.Do(req)
if err != nil {
log.Fatal(err)
}
bodyText, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
DebugResponse(resp, bodyText)
return bodyText, resp.StatusCode
}
// NutanixAPIPost ...
func NutanixAPIPost(n *NTNXConnection, NutanixAPIurl string, NutanixURI string, body *bytes.Buffer) ([]byte, int) {
var req *http.Request
var err error
req, err = http.NewRequest("POST", NutanixAPIurl+NutanixURI, body)
req.Header.Set("Authorization", "Basic "+n.SEnc)
DebugRequest(req)
resp, err := n.HTTPClient.Do(req)
if err != nil {
log.Fatal(err)
}
bodyText, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
DebugResponse(resp, bodyText)
return bodyText, resp.StatusCode
}
// NutanixAPIDelete ...
func NutanixAPIDelete(n *NTNXConnection, NutanixAPIurl string, NutanixURI string) ([]byte, int) {
var req *http.Request
var err error
req, err = http.NewRequest("DELETE", NutanixAPIurl+NutanixURI, nil)
req.Header.Set("Authorization", "Basic "+n.SEnc)
DebugRequest(req)
resp, err := n.HTTPClient.Do(req)
if err != nil {
log.Fatal(err)
}
bodyText, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
DebugResponse(resp, bodyText)
return bodyText, resp.StatusCode
}
// NutanixCheckCredentials ...
func NutanixCheckCredentials(n *NTNXConnection) {
_, statusCode := NutanixAPIGet(n, NutanixRestURL(n), "cluster")
if statusCode == 401 {
log.Fatal("Username or password not valid for host: " + n.NutanixHost)
os.Exit(1)
}
if statusCode != 200 {
log.Fatal("Connection to host: " + n.NutanixHost + " not possible")
os.Exit(1)
}
}
// PutFileToImage ...
func PutFileToImage(n *NTNXConnection, NutanixAPIurl string, NutanixURI string, filename string, containerName string) ([]byte, int) {
var req *http.Request
// Open file which will be send via PUT
f, err := os.Open(filename)
if err != nil {
log.Fatal(err)
}
defer f.Close()
// file Stat().Size is needed to set Content-Length
fStat, err := f.Stat()
if err != nil {
// Could not obtain stat, handle error
log.Fatal(err)
}
log.Debug("The file " + filename + " is " + strconv.FormatInt(fStat.Size(), 10) + " bytes long")
req, err = http.NewRequest("PUT", NutanixAPIurl+NutanixURI, f)
containerID, _ := GetContainerUUIDbyName(n, containerName)
req.ContentLength = fStat.Size()
req.Header.Set("Authorization", "Basic "+n.SEnc)
req.Header.Set("X-Nutanix-Destination-Container", containerID)
req.Header.Set("Content-Type", "application/octet-stream;charset=UTF-8")
req.Header.Set("Content-Length", strconv.FormatInt(fStat.Size(), 10))
req.Header.Set("Accept-Language", "de-DE,de;q=0.8,en-US;q=0.6,en;q=0.4")
req.Header.Set("Connection", "keep-alive")
DebugRequest(req)
log.Info("Uploading file " + filename + " ...")
resp, err := n.HTTPClient.Do(req)
if err != nil {
log.Fatal(err)
}
bodyText, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
DebugResponse(resp, bodyText)
return bodyText, resp.StatusCode
}
// GetCluster ...
func GetCluster(n *NTNXConnection) []byte {
resp, _ := NutanixAPIGet(n, NutanixRestURL(n), "cluster")
return resp
}