-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcloudstack.go
More file actions
499 lines (437 loc) · 12.3 KB
/
cloudstack.go
File metadata and controls
499 lines (437 loc) · 12.3 KB
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
package cloudstack
import (
"crypto/hmac"
"crypto/sha1"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/http/cookiejar"
"net/url"
"reflect"
"sort"
"strconv"
"strings"
"time"
"unicode"
)
func convertJsonToMap(b []byte) (map[string]json.RawMessage, error) {
var m map[string]json.RawMessage
if err := json.Unmarshal(b, &m); err != nil {
return nil, fmt.Errorf("Failed to convert json to map: %v", err)
}
return m, nil
}
// Get error message from API response
func getErrorText(m map[string]json.RawMessage) string {
quoted, ok := m["errortext"]
if !ok {
return ""
}
errortext, err := strconv.Unquote(string(quoted))
if err != nil {
return string(quoted)
}
return errortext
}
func getResponseContent(cmd *Command, resp []byte) (respBody []byte, err error) {
var ok bool
respMap, err := convertJsonToMap(resp)
if err != nil {
return nil, err
}
name := strings.ToLower(cmd.Name)
if name == "listsecondarystagingstores" {
respBody, ok = respMap["listsecondarystagingstoreresponse"]
} else {
respBody, ok = respMap[name+"response"]
if !ok {
// some API's response are not ended with "response"
respBody, ok = respMap[name]
}
}
if !ok {
if errortext := getErrorText(respMap); errortext != "" {
return nil, errors.New(errortext)
}
return nil, fmt.Errorf(
"Unexpected format: API response doesn't contain %s or %s or errortext",
name+"response", name)
}
return respBody, nil
}
// Get content from API response
func getObjectJson(cmd *Command, resp []byte, isJobResult bool) (objJson []byte, err error) {
var respBody []byte
if isJobResult {
respBody = resp
} else {
respBody, err = getResponseContent(cmd, resp)
if err != nil {
return nil, err
}
if cmd.IsAsync {
// return jobid
return respBody, nil
}
}
switch cmd.ObjectType {
case "result":
return respBody, nil
default:
respBodyMap, err := convertJsonToMap(respBody)
if err != nil {
return nil, err
}
objJson, ok := respBodyMap[cmd.ObjectType]
if !ok {
if errortext := getErrorText(respBodyMap); errortext != "" {
return nil, errors.New(errortext)
}
// respBodyMap can be empty. For example, list api returns nothing.
if len(respBodyMap) == 0 {
return nil, nil
}
// Type can be null. For example, destroyvirtualmachine is executed with expunge=true
if objJson, ok := respBodyMap["null"]; ok {
return objJson, nil
}
return nil, fmt.Errorf(
"Unexpected format: response doesn't contain %s or errortext",
cmd.ObjectType)
}
return objJson, nil
}
}
func sign(queryStr, secretKey string) string {
queryStr = strings.ToLower(queryStr)
queryStr = strings.Replace(queryStr, "+", "%20", -1)
queryStr = strings.Replace(queryStr, "@", "%40", -1)
mac := hmac.New(sha1.New, []byte(secretKey))
mac.Write([]byte(queryStr))
return url.QueryEscape(base64.StdEncoding.EncodeToString(mac.Sum(nil)))
}
func (client *Client) convertResponseJsonToObject(cmd *Command, resp []byte, isJobResult bool) (interface{}, error) {
objJson, err := getObjectJson(cmd, resp, isJobResult)
if err != nil {
return nil, err
}
p := cmd.Pointer()
if objJson == nil {
return reflect.ValueOf(p).Elem().Interface(), nil
}
if err := json.Unmarshal(objJson, p); err != nil {
return nil, fmt.Errorf("Failed to convert json to object: %v", err)
}
obj := reflect.ValueOf(p).Elem().Interface()
// Set Object's Client field
v := reflect.ValueOf(obj)
if v.Kind() == reflect.Slice {
for i := 0; i < v.Len(); i++ {
v.Index(i).Interface().(Resource).setClient(client)
}
} else if v.Kind() == reflect.Ptr {
obj.(Resource).setClient(client)
}
return obj, nil
}
// convert APIParameter to map[string]interface{}
func convertParamToMap(p APIParameter) (m map[string]interface{}) {
m = make(map[string]interface{})
v := reflect.ValueOf(p).Elem()
for i := 0; i < v.NumField(); i++ {
if unicode.IsUpper(rune(v.Type().Field(i).Name[0])) {
m[strings.ToLower(v.Type().Field(i).Name)] = v.Field(i).Interface()
}
}
return m
}
type LogInResponse struct {
Username NullString `json:"username"`
UserId ID `json:"userid"`
Password NullString `json:"password"`
DomainId ID `json:"domainid"`
Timeout NullString `json:"timeout"`
Account NullString `json:"account"`
FirstName NullString `json:"firstname"`
LastName NullString `json:"lastname"`
Type NullString `json:"type"`
TimeZone NullString `json:"timezone"`
TimeZoneOffset NullString `json:"timezoneoffset"`
SessionKey NullString `json:"sessionkey"`
}
type LogOutResponse struct {
Description NullString `json:"description"`
}
type AsyncApiResponse struct {
Id ID `json:"id"`
JobId ID `json:"jobid"`
}
type AsyncJobResult struct {
AccountId NullString `json:"accountid"`
Cmd NullString `json:"cmd"`
Created NullString `json:"created"`
JobId ID `json:"jobid"`
JobInstanceId ID `json:"jobinstanceid"`
JobInstanceType NullString `json:"jobinstancetype"`
JobProcsSatus NullNumber `json:"jobprocstatus"`
JobResult json.RawMessage `json:"jobresult"`
JobResultCode NullNumber `json:"jobresultcode"`
JobResultType NullString `json:"jobresulttype"`
JobStatus NullNumber `json:"jobstatus"`
UserId ID `json:"userid"`
}
type Client struct {
EndPoint *url.URL
APIKey string
SecretKey string
Username string
Password string
SessionKey string
PollingInterval time.Duration
HTTPClient *http.Client
}
func NewClient(endpoint *url.URL, apikey string, secretkey string,
username string, password string) (*Client, error) {
jar, err := cookiejar.New(&cookiejar.Options{})
if err != nil {
log.Println("cookiejar.New failed:", err)
}
return &Client{
EndPoint: endpoint,
APIKey: apikey,
SecretKey: secretkey,
Username: username,
Password: password,
SessionKey: "",
PollingInterval: config.PollingInterval,
HTTPClient: &http.Client{Jar: jar},
}, nil
}
// Generate Query URL from command and paramters
func (c *Client) GenerateQueryURL(command string, params map[string]interface{}) string {
queryURL := c.EndPoint
values := url.Values{}
values.Add("command", command)
values.Add("response", "json")
for k := range params {
if k == "userdata" {
_, ok := params[k]
if ok {
s := fmt.Sprint(params["userdata"])
// There seems to be a issue if base64 encoded string ended with
// padding character "="
// add space to original string to make result not ended with "="
for {
if len(s)%3 == 0 {
break
}
s += " "
}
values.Add(k, base64.StdEncoding.EncodeToString([]byte(s)))
}
continue
}
switch v := params[k].(type) {
case []string:
if len(v) > 0 {
values.Add(k, strings.Join(v, ","))
}
case map[string]string:
if len(v) > 0 {
if k == "tags" {
i := 0
for key, value := range v {
values.Add(fmt.Sprintf("%s[%d].key", k, i), key)
values.Add(fmt.Sprintf("%s[%d].value", k, i), value)
i += 1
}
} else {
for key, value := range v {
values.Add(fmt.Sprintf("%s[0].%s", k, key), value)
}
}
}
case NullBool, NullString, NullNumber, ID:
if !v.(Nullable).IsNil() {
values.Add(k, fmt.Sprint(v))
}
default:
values.Add(k, fmt.Sprint(v))
}
}
if c.APIKey != "" && c.SecretKey != "" {
values.Add("apikey", c.APIKey)
// queryStr must not be URL encoded. You can't use values.Encode.
keys := make([]string, 0, len(values))
for k := range values {
keys = append(keys, k)
}
sort.Strings(keys)
params := make([]string, 0, len(keys))
for _, k := range keys {
params = append(params, fmt.Sprintf("%s=%s", k, values[k][0]))
}
queryStr := strings.Join(params, "&")
signature := sign(queryStr, c.SecretKey)
queryURL.RawQuery = queryStr + "&signature=" + signature
} else {
if command == "login" && c.Username != "" && c.Password != "" {
values.Add("username", c.Username)
values.Add("password", c.Password)
} else if c.SessionKey != "" {
values.Add("sessionkey", c.SessionKey)
}
queryURL.RawQuery = values.Encode()
}
return queryURL.String()
}
func (c *Client) AsyncRequestJson(command string, params map[string]interface{}) (resp []byte, err error) {
cmd := getCommand(command)
queryURL := c.GenerateQueryURL(cmd.Name, params)
log.Println("QueryURL:", queryURL)
log.Println("request cookie:", c.HTTPClient.Jar)
httpReq, _ := http.NewRequest("GET", queryURL, nil)
httpResp, err := c.HTTPClient.Do(httpReq)
if err != nil {
log.Println("HTTPClient.Do failed:", err)
return nil, err
}
defer httpResp.Body.Close()
resp, err = ioutil.ReadAll(httpResp.Body)
log.Println("statuscode:", httpResp.StatusCode)
log.Println("response:", string(resp))
log.Println("cookie:", c.HTTPClient.Jar)
if httpResp.StatusCode != 200 {
return resp, fmt.Errorf("StatusCode %d: %s", httpResp.StatusCode, resp)
}
return resp, err
}
func (c *Client) AsyncRequest(command string, params map[string]interface{}) (interface{}, error) {
cmd := getCommand(command)
resp, err := c.AsyncRequestJson(cmd.Name, params)
if err != nil {
return nil, err
}
if cmd.IsAsync {
var r *AsyncApiResponse
respJson, err := getObjectJson(cmd, resp, false)
if err != nil {
return nil, err
}
if err = json.Unmarshal(respJson, &r); err != nil {
return nil, err
}
if r.JobId.IsNil() {
m, _ := convertJsonToMap(respJson)
errortext := getErrorText(m)
if errortext != "" {
return nil, errors.New(errortext)
}
return nil, fmt.Errorf("No jobid returned. %s", string(respJson))
}
return r, nil
}
return c.convertResponseJsonToObject(cmd, resp, false)
}
func (c *Client) RequestJson(command string, params map[string]interface{}) ([]byte, error) {
cmd := getCommand(command)
if cmd.IsAsync {
r, err := c.AsyncRequest(cmd.Name, params)
if err != nil {
return nil, err
}
job, err := c.Wait(r.(*AsyncApiResponse).JobId.String())
if err != nil {
return nil, err
}
return job.JobResult, nil
}
return c.AsyncRequestJson(command, params)
}
func (c *Client) Request(command string, params map[string]interface{}) (interface{}, error) {
cmd := getCommand(command)
resp, err := c.RequestJson(cmd.Name, params)
if err != nil {
return nil, err
}
// If command is Async, pass JobResult.
return c.convertResponseJsonToObject(cmd, resp, cmd.IsAsync)
}
func (c *Client) LogIn() (err error) {
r, err := c.AsyncRequestJson("login", nil)
if err != nil {
return err
}
lr := LogInResponse{}
if err = json.Unmarshal(r, &lr); err != nil {
return err
}
if lr.SessionKey.IsNil() {
return errors.New("login API didn't return valid sessionkey.")
}
c.SessionKey = lr.SessionKey.String()
return nil
}
func (c *Client) LogOut() error {
r, err := c.AsyncRequestJson("logout", nil)
lr := LogOutResponse{}
if err = json.Unmarshal(r, &lr); err != nil {
return err
}
if lr.Description.String() != "success" {
return fmt.Errorf("logout API is failed. description: %v", lr.Description)
}
return nil
}
func (c *Client) QueryAsyncJobResult(jobid string) (job *AsyncJobResult, err error) {
resp, err := c.AsyncRequestJson(
"queryAsyncJobResult", map[string]interface{}{"jobid": jobid})
if err != nil {
return nil, err
}
m, err := convertJsonToMap(resp)
if err != nil {
return nil, err
}
jobJson, ok := m["queryasyncjobresultresponse"]
if !ok {
if errortext := getErrorText(m); errortext != "" {
return nil, errors.New(errortext)
}
return nil, fmt.Errorf(
"Unexpected format: response doesn't contain queryasyncjobresultresponse or errortext")
}
if err = json.Unmarshal(jobJson, &job); err != nil {
return nil, fmt.Errorf("Failed to convert json to AsyncJobResult: %v", err)
}
return job, nil
}
func (c *Client) Wait(jobid string) (job *AsyncJobResult, err error) {
for {
job, err = c.QueryAsyncJobResult(jobid)
if err != nil {
return nil, err
}
if job.JobStatus.String() != "0" {
break
}
time.Sleep(c.PollingInterval * time.Second)
}
if job.JobStatus.String() != "1" {
m, err := convertJsonToMap(job.JobResult)
if err != nil {
return nil, err
}
errortext := getErrorText(m)
if errortext != "" {
return nil, errors.New(errortext)
}
return nil, fmt.Errorf(
"job doesn't finished properly: %s", job.JobStatus.String())
}
return job, nil
}