This repository has been archived by the owner on Sep 25, 2022. It is now read-only.
forked from xanzy/go-cloudstack
-
Notifications
You must be signed in to change notification settings - Fork 1
/
kcps.go
514 lines (430 loc) · 13.5 KB
/
kcps.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
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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
//
// Copyright 2016, Sander van Harmelen
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package gokcps
import (
"bytes"
"crypto/hmac"
"crypto/sha1"
"crypto/tls"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"math/rand"
"net/http"
"net/url"
"regexp"
"sort"
"strings"
"sync"
"time"
)
// UnlimitedResourceID is a special ID to define an unlimited resource
const UnlimitedResourceID = "-1"
var idRegex = regexp.MustCompile(`^([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}|-1)$`)
// For KDDI
// var requestMux = &sync.Mutex{}
// IsID return true if the passed ID is either a UUID or a UnlimitedResourceID
func IsID(id string) bool {
return idRegex.MatchString(id)
}
// OptionFunc can be passed to the courtesy helper functions to set additional parameters
type OptionFunc func(*KCPSClient, interface{}) error
type CSError struct {
ErrorCode int `json:"errorcode"`
CSErrorCode int `json:"cserrorcode"`
ErrorText string `json:"errortext"`
}
func (e *CSError) Error() error {
return fmt.Errorf("CloudStack API error %d (CSExceptionErrorCode: %d): %s", e.ErrorCode, e.CSErrorCode, e.ErrorText)
}
type KCPSClient struct {
HTTPGETOnly bool // If `true` only use HTTP GET calls
lock *sync.Mutex
client *http.Client // The http client for communicating
baseURL string // The base URL of the API
apiKey string // Api key
secret string // Secret key
async bool // Wait for async calls to finish
timeout int64 // Max waiting timeout in seconds for async jobs to finish; defaults to 300 seconds
Asyncjob *AsyncjobService
Event *EventService
Firewall *FirewallService
GuestOS *GuestOSService
Host *HostService
ISO *ISOService
LoadBalancer *LoadBalancerService
NatPortForward *NatPortForwardService
Nic *NicService
Snapshot *SnapshotService
Template *TemplateService
AccountDomain *AccountDomainService
VirtualMachine *VirtualMachineService
Volume *VolumeService
Tags *TagsService
}
// Creates a new client for communicating with CloudStack
func newClient(apiurl string, apikey string, secret string, async bool, verifyssl bool) *KCPSClient {
cs := &KCPSClient{
client: &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
TLSClientConfig: &tls.Config{InsecureSkipVerify: !verifyssl}, // If verifyssl is true, skipping the verify should be false and vice versa
},
Timeout: time.Duration(60 * time.Second),
},
baseURL: apiurl,
apiKey: apikey,
secret: secret,
async: async,
timeout: 300,
}
cs.Asyncjob = NewAsyncjobService(cs)
cs.Event = NewEventService(cs)
cs.Firewall = NewFirewallService(cs)
cs.GuestOS = NewGuestOSService(cs)
cs.Host = NewHostService(cs)
cs.ISO = NewISOService(cs)
cs.LoadBalancer = NewLoadBalancerService(cs)
cs.NatPortForward = NewNatPortForwardService(cs)
cs.Nic = NewNicService(cs)
cs.Snapshot = NewSnapshotService(cs)
cs.Template = NewTemplateService(cs)
cs.AccountDomain = NewAccountDomainService(cs)
cs.VirtualMachine = NewVirtualMachineService(cs)
cs.Volume = NewVolumeService(cs)
cs.Tags = NewTagsService(cs)
return cs
}
// Default non-async client. So for async calls you need to implement and check the async job result yourself. When using
// HTTPS with a self-signed certificate to connect to your CloudStack API, you would probably want to set 'verifyssl' to
// false so the call ignores the SSL errors/warnings.
func NewClient(apiurl string, apikey string, secret string, verifyssl bool) *KCPSClient {
cs := newClient(apiurl, apikey, secret, false, verifyssl)
cs.lock = new(sync.Mutex)
return cs
}
// For sync API calls this client behaves exactly the same as a standard client call, but for async API calls
// this client will wait until the async job is finished or until the configured AsyncTimeout is reached. When the async
// job finishes successfully it will return actual object received from the API and nil, but when the timout is
// reached it will return the initial object containing the async job ID for the running job and a warning.
func NewAsyncClient(apiurl string, apikey string, secret string, verifyssl bool) *KCPSClient {
cs := newClient(apiurl, apikey, secret, true, verifyssl)
cs.lock = new(sync.Mutex)
return cs
}
// When using the async client an api call will wait for the async call to finish before returning. The default is to poll for 300 seconds
// seconds, to check if the async job is finished.
func (cs *KCPSClient) AsyncTimeout(timeoutInSeconds int64) {
cs.timeout = timeoutInSeconds
}
var AsyncTimeoutErr = errors.New("Timeout while waiting for async job to finish")
// A helper function that you can use to get the result of a running async job. If the job is not finished within the configured
// timeout, the async job returns a AsyncTimeoutErr.
func (cs *KCPSClient) GetAsyncJobResult(jobid string, timeout int64) (json.RawMessage, error) {
var timer time.Duration
currentTime := time.Now().Unix()
for {
p := cs.Asyncjob.NewQueryAsyncJobResultParams(jobid)
r, err := cs.Asyncjob.QueryAsyncJobResult(p)
if err != nil {
return nil, err
}
// Status 1 means the job is finished successfully
if r.Jobstatus == 1 {
return r.Jobresult, nil
}
// When the status is 2, the job has failed
if r.Jobstatus == 2 {
if r.Jobresulttype == "text" {
return nil, fmt.Errorf(string(r.Jobresult))
} else {
return nil, fmt.Errorf("Undefined error: %s", string(r.Jobresult))
}
}
if time.Now().Unix()-currentTime > timeout {
return nil, AsyncTimeoutErr
}
// Add an (extremely simple) exponential backoff like feature to prevent
// flooding the CloudStack API
if timer < 15 {
timer++
}
time.Sleep(timer * time.Second)
}
}
// A helper function that you can use to get the result of a running async job. If the job is not finished within the configured
// timeout, the async job returns a AsyncTimeoutErr.
func (cs *KCPSClient) GetExAsyncJobResult(jobid string, timeout int64) (json.RawMessage, error) {
var timer time.Duration
currentTime := time.Now().Unix()
for {
p := cs.Asyncjob.NewQueryExAsyncJobResultParams(jobid)
r, err := cs.Asyncjob.QueryExAsyncJobResult(p)
if err != nil {
return nil, err
}
// Status 1 means the job is finished successfully
if r.Jobstatus == 1 {
return r.Jobresult, nil
}
// When the status is 2, the job has failed
if r.Jobstatus == 2 {
if r.Jobresulttype == "text" {
return nil, fmt.Errorf(string(r.Jobresult))
} else {
return nil, fmt.Errorf("Undefined error: %s", string(r.Jobresult))
}
}
if time.Now().Unix()-currentTime > timeout {
return nil, AsyncTimeoutErr
}
// Add an (extremely simple) exponential backoff like feature to prevent
// flooding the CloudStack API
if timer < 15 {
timer++
}
time.Sleep(timer * time.Second)
}
}
// Execute the request against a CS API. Will return the raw JSON data returned by the API and nil if
// no error occured. If the API returns an error the result will be nil and the HTTP error code and CS
// error details. If a processing (code) error occurs the result will be nil and the generated error
func (cs *KCPSClient) newRequest(api string, params url.Values) (json.RawMessage, error) {
cs.lock.Lock()
defer cs.lock.Unlock()
maxret := 5
retry := 0
var (
err error
message json.RawMessage
)
for {
retry++
if maxret < retry {
err = fmt.Errorf("api request max retry")
break
}
m, e := cs.oneRequest(api, params)
if e != nil && strings.HasSuffix(e.Error(), "connection reset by peer") {
time.Sleep(time.Duration(rand.Int63n(10)) * time.Second)
continue
}
message = m
err = e
break
}
return message, err
}
func (cs *KCPSClient) oneRequest(api string, params url.Values) (json.RawMessage, error) {
params.Set("apiKey", cs.apiKey)
params.Set("command", api)
params.Set("response", "json")
// Generate signature for API call
// * Serialize parameters, URL encoding only values and sort them by key, done by encodeValues
// * Convert the entire argument string to lowercase
// * Replace all instances of '+' to '%20'
// * Calculate HMAC SHA1 of argument string with CloudStack secret
// * URL encode the string and convert to base64
s := encodeValues(params)
s2 := strings.ToLower(s)
s3 := strings.Replace(s2, "+", "%20", -1)
mac := hmac.New(sha1.New, []byte(cs.secret))
mac.Write([]byte(s3))
signature := base64.StdEncoding.EncodeToString(mac.Sum(nil))
var err error
var resp *http.Response
if !cs.HTTPGETOnly && (api == "deployVirtualMachine" || api == "updateVirtualMachine") {
// The deployVirtualMachine API should be called using a POST call
// so we don't have to worry about the userdata size
// Add the unescaped signature to the POST params
params.Set("signature", signature)
// Make a POST call
resp, err = cs.client.PostForm(cs.baseURL, params)
} else {
// Create the final URL before we issue the request
url := cs.baseURL + "?" + s + "&signature=" + url.QueryEscape(signature)
// Make a GET call
// requestMux.Lock()
resp, err = cs.client.Get(url)
// For KDDI
// time.Sleep(2 * time.Second)
// requestMux.Unlock()
}
if err != nil {
return nil, err
}
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
// Need to get the raw value to make the result play nice
b, err = getRawValue(b)
if err != nil {
return nil, err
}
if resp.StatusCode != 200 {
var e CSError
if err := json.Unmarshal(b, &e); err != nil {
return nil, err
}
return nil, e.Error()
}
return b, nil
}
// Custom version of net/url Encode that only URL escapes values
// Unmodified portions here remain under BSD license of The Go Authors: https://go.googlesource.com/go/+/master/LICENSE
func encodeValues(v url.Values) string {
if v == nil {
return ""
}
var buf bytes.Buffer
keys := make([]string, 0, len(v))
for k := range v {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
vs := v[k]
prefix := k + "="
for _, v := range vs {
if buf.Len() > 0 {
buf.WriteByte('&')
}
buf.WriteString(prefix)
buf.WriteString(url.QueryEscape(v))
}
}
return buf.String()
}
// Generic function to get the first raw value from a response as json.RawMessage
func getRawValue(b json.RawMessage) (json.RawMessage, error) {
var m map[string]json.RawMessage
if err := json.Unmarshal(b, &m); err != nil {
return nil, err
}
for _, v := range m {
return v, nil
}
return nil, fmt.Errorf("Unable to extract the raw value from:\n\n%s\n\n", string(b))
}
// VPCIDSetter is an interface that every type that can set a vpc ID must implement
type VPCIDSetter interface {
SetVpcid(string)
}
// WithVPCID takes a vpc ID and sets the `vpcid` parameter
func WithVPCID(id string) OptionFunc {
return func(cs *KCPSClient, p interface{}) error {
vs, ok := p.(VPCIDSetter)
if !ok || id == "" {
return nil
}
vs.SetVpcid(id)
return nil
}
}
type AsyncjobService struct {
cs *KCPSClient
}
func NewAsyncjobService(cs *KCPSClient) *AsyncjobService {
return &AsyncjobService{cs: cs}
}
type EventService struct {
cs *KCPSClient
}
func NewEventService(cs *KCPSClient) *EventService {
return &EventService{cs: cs}
}
type FirewallService struct {
cs *KCPSClient
}
func NewFirewallService(cs *KCPSClient) *FirewallService {
return &FirewallService{cs: cs}
}
type GuestOSService struct {
cs *KCPSClient
}
func NewGuestOSService(cs *KCPSClient) *GuestOSService {
return &GuestOSService{cs: cs}
}
type HostService struct {
cs *KCPSClient
}
func NewHostService(cs *KCPSClient) *HostService {
return &HostService{cs: cs}
}
type ISOService struct {
cs *KCPSClient
}
func NewISOService(cs *KCPSClient) *ISOService {
return &ISOService{cs: cs}
}
type LoadBalancerService struct {
cs *KCPSClient
}
func NewLoadBalancerService(cs *KCPSClient) *LoadBalancerService {
return &LoadBalancerService{cs: cs}
}
type NatPortForwardService struct {
cs *KCPSClient
}
func NewNatPortForwardService(cs *KCPSClient) *NatPortForwardService {
return &NatPortForwardService{cs: cs}
}
type NicService struct {
cs *KCPSClient
}
func NewNicService(cs *KCPSClient) *NicService {
return &NicService{cs: cs}
}
type SnapshotService struct {
cs *KCPSClient
}
func NewSnapshotService(cs *KCPSClient) *SnapshotService {
return &SnapshotService{cs: cs}
}
type TemplateService struct {
cs *KCPSClient
}
func NewTemplateService(cs *KCPSClient) *TemplateService {
return &TemplateService{cs: cs}
}
type AccountDomainService struct {
cs *KCPSClient
}
func NewAccountDomainService(cs *KCPSClient) *AccountDomainService {
return &AccountDomainService{cs: cs}
}
type VirtualMachineService struct {
cs *KCPSClient
}
func NewVirtualMachineService(cs *KCPSClient) *VirtualMachineService {
return &VirtualMachineService{cs: cs}
}
type VolumeService struct {
cs *KCPSClient
}
func NewVolumeService(cs *KCPSClient) *VolumeService {
return &VolumeService{cs: cs}
}
type TagsService struct {
cs *KCPSClient
}
func NewTagsService(cs *KCPSClient) *TagsService {
return &TagsService{cs: cs}
}