This repository has been archived by the owner on Sep 6, 2021. It is now read-only.
forked from xanzy/go-cloudstack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SSHService.go
692 lines (629 loc) · 19.7 KB
/
SSHService.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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
//
// Copyright 2014, 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 cloudstack
import (
"encoding/json"
"net/url"
"strconv"
)
type ResetSSHKeyForVirtualMachineParams struct {
p map[string]interface{}
}
func (p *ResetSSHKeyForVirtualMachineParams) toURLValues() url.Values {
u := url.Values{}
if p.p == nil {
return u
}
if v, found := p.p["account"]; found {
u.Set("account", v.(string))
}
if v, found := p.p["domainid"]; found {
u.Set("domainid", v.(string))
}
if v, found := p.p["id"]; found {
u.Set("id", v.(string))
}
if v, found := p.p["keypair"]; found {
u.Set("keypair", v.(string))
}
if v, found := p.p["projectid"]; found {
u.Set("projectid", v.(string))
}
return u
}
func (p *ResetSSHKeyForVirtualMachineParams) SetAccount(v string) {
if p.p == nil {
p.p = make(map[string]interface{})
}
p.p["account"] = v
return
}
func (p *ResetSSHKeyForVirtualMachineParams) SetDomainid(v string) {
if p.p == nil {
p.p = make(map[string]interface{})
}
p.p["domainid"] = v
return
}
func (p *ResetSSHKeyForVirtualMachineParams) SetId(v string) {
if p.p == nil {
p.p = make(map[string]interface{})
}
p.p["id"] = v
return
}
func (p *ResetSSHKeyForVirtualMachineParams) SetKeypair(v string) {
if p.p == nil {
p.p = make(map[string]interface{})
}
p.p["keypair"] = v
return
}
func (p *ResetSSHKeyForVirtualMachineParams) SetProjectid(v string) {
if p.p == nil {
p.p = make(map[string]interface{})
}
p.p["projectid"] = v
return
}
// You should always use this function to get a new ResetSSHKeyForVirtualMachineParams instance,
// as then you are sure you have configured all required params
func (s *SSHService) NewResetSSHKeyForVirtualMachineParams(id string, keypair string) *ResetSSHKeyForVirtualMachineParams {
p := &ResetSSHKeyForVirtualMachineParams{}
p.p = make(map[string]interface{})
p.p["id"] = id
p.p["keypair"] = keypair
return p
}
// Resets the SSH Key for virtual machine. The virtual machine must be in a "Stopped" state. [async]
func (s *SSHService) ResetSSHKeyForVirtualMachine(p *ResetSSHKeyForVirtualMachineParams) (*ResetSSHKeyForVirtualMachineResponse, error) {
resp, err := s.cs.newRequest("resetSSHKeyForVirtualMachine", p.toURLValues())
if err != nil {
return nil, err
}
var r ResetSSHKeyForVirtualMachineResponse
if err := json.Unmarshal(resp, &r); err != nil {
return nil, err
}
// If we have a async client, we need to wait for the async result
if s.cs.async {
b, warn, err := s.cs.GetAsyncJobResult(r.JobID, s.cs.timeout)
if err != nil {
return nil, err
}
// If 'warn' has a value it means the job is running longer than the configured
// timeout, the resonse will contain the jobid of the running async job
if warn != nil {
return &r, warn
}
var r ResetSSHKeyForVirtualMachineResponse
if err := json.Unmarshal(b, &r); err != nil {
return nil, err
}
return &r, nil
}
return &r, nil
}
type ResetSSHKeyForVirtualMachineResponse struct {
JobID string `json:"jobid,omitempty"`
Isoname string `json:"isoname,omitempty"`
Hypervisor string `json:"hypervisor,omitempty"`
Hostname string `json:"hostname,omitempty"`
Created string `json:"created,omitempty"`
Cpunumber int `json:"cpunumber,omitempty"`
Projectid string `json:"projectid,omitempty"`
Networkkbswrite int `json:"networkkbswrite,omitempty"`
Account string `json:"account,omitempty"`
Serviceofferingid string `json:"serviceofferingid,omitempty"`
Diskkbswrite int `json:"diskkbswrite,omitempty"`
Isdynamicallyscalable bool `json:"isdynamicallyscalable,omitempty"`
Rootdevicetype string `json:"rootdevicetype,omitempty"`
Details map[string]string `json:"details,omitempty"`
Rootdeviceid int `json:"rootdeviceid,omitempty"`
Isoid string `json:"isoid,omitempty"`
Templateid string `json:"templateid,omitempty"`
Cpuused string `json:"cpuused,omitempty"`
Project string `json:"project,omitempty"`
Displayvm bool `json:"displayvm,omitempty"`
Passwordenabled bool `json:"passwordenabled,omitempty"`
Domainid string `json:"domainid,omitempty"`
Cpuspeed int `json:"cpuspeed,omitempty"`
Diskioread int `json:"diskioread,omitempty"`
Tags []struct {
Account string `json:"account,omitempty"`
Value string `json:"value,omitempty"`
Projectid string `json:"projectid,omitempty"`
Resourcetype string `json:"resourcetype,omitempty"`
Project string `json:"project,omitempty"`
Key string `json:"key,omitempty"`
Customer string `json:"customer,omitempty"`
Domainid string `json:"domainid,omitempty"`
Resourceid string `json:"resourceid,omitempty"`
Domain string `json:"domain,omitempty"`
} `json:"tags,omitempty"`
Instancename string `json:"instancename,omitempty"`
Id string `json:"id,omitempty"`
Forvirtualnetwork bool `json:"forvirtualnetwork,omitempty"`
Keypair string `json:"keypair,omitempty"`
Securitygroup []struct {
Id string `json:"id,omitempty"`
Projectid string `json:"projectid,omitempty"`
Ingressrule []struct {
Startport int `json:"startport,omitempty"`
Cidr string `json:"cidr,omitempty"`
Endport int `json:"endport,omitempty"`
Ruleid string `json:"ruleid,omitempty"`
Securitygroupname string `json:"securitygroupname,omitempty"`
Icmpcode int `json:"icmpcode,omitempty"`
Icmptype int `json:"icmptype,omitempty"`
Protocol string `json:"protocol,omitempty"`
Account string `json:"account,omitempty"`
} `json:"ingressrule,omitempty"`
Name string `json:"name,omitempty"`
Account string `json:"account,omitempty"`
Description string `json:"description,omitempty"`
Project string `json:"project,omitempty"`
Egressrule []struct {
Ruleid string `json:"ruleid,omitempty"`
Securitygroupname string `json:"securitygroupname,omitempty"`
Endport int `json:"endport,omitempty"`
Startport int `json:"startport,omitempty"`
Icmptype int `json:"icmptype,omitempty"`
Cidr string `json:"cidr,omitempty"`
Protocol string `json:"protocol,omitempty"`
Account string `json:"account,omitempty"`
Icmpcode int `json:"icmpcode,omitempty"`
} `json:"egressrule,omitempty"`
Tags []struct {
Projectid string `json:"projectid,omitempty"`
Value string `json:"value,omitempty"`
Resourcetype string `json:"resourcetype,omitempty"`
Account string `json:"account,omitempty"`
Domain string `json:"domain,omitempty"`
Customer string `json:"customer,omitempty"`
Key string `json:"key,omitempty"`
Domainid string `json:"domainid,omitempty"`
Project string `json:"project,omitempty"`
Resourceid string `json:"resourceid,omitempty"`
} `json:"tags,omitempty"`
Domain string `json:"domain,omitempty"`
Domainid string `json:"domainid,omitempty"`
} `json:"securitygroup,omitempty"`
Publicip string `json:"publicip,omitempty"`
Publicipid string `json:"publicipid,omitempty"`
Name string `json:"name,omitempty"`
Zoneid string `json:"zoneid,omitempty"`
Guestosid string `json:"guestosid,omitempty"`
Group string `json:"group,omitempty"`
Nic []struct {
Gateway string `json:"gateway,omitempty"`
Type string `json:"type,omitempty"`
Networkid string `json:"networkid,omitempty"`
Macaddress string `json:"macaddress,omitempty"`
Isolationuri string `json:"isolationuri,omitempty"`
Broadcasturi string `json:"broadcasturi,omitempty"`
Ip6address string `json:"ip6address,omitempty"`
Ip6cidr string `json:"ip6cidr,omitempty"`
Networkname string `json:"networkname,omitempty"`
Isdefault bool `json:"isdefault,omitempty"`
Secondaryip []string `json:"secondaryip,omitempty"`
Traffictype string `json:"traffictype,omitempty"`
Netmask string `json:"netmask,omitempty"`
Id string `json:"id,omitempty"`
Ipaddress string `json:"ipaddress,omitempty"`
Ip6gateway string `json:"ip6gateway,omitempty"`
} `json:"nic,omitempty"`
Networkkbsread int `json:"networkkbsread,omitempty"`
Password string `json:"password,omitempty"`
Isodisplaytext string `json:"isodisplaytext,omitempty"`
Groupid string `json:"groupid,omitempty"`
Haenable bool `json:"haenable,omitempty"`
Hostid string `json:"hostid,omitempty"`
State string `json:"state,omitempty"`
Serviceofferingname string `json:"serviceofferingname,omitempty"`
Templatedisplaytext string `json:"templatedisplaytext,omitempty"`
Displayname string `json:"displayname,omitempty"`
Templatename string `json:"templatename,omitempty"`
Zonename string `json:"zonename,omitempty"`
Affinitygroup []struct {
Account string `json:"account,omitempty"`
Domain string `json:"domain,omitempty"`
Description string `json:"description,omitempty"`
Name string `json:"name,omitempty"`
Type string `json:"type,omitempty"`
Domainid string `json:"domainid,omitempty"`
VirtualmachineIds []string `json:"virtualmachineIds,omitempty"`
Id string `json:"id,omitempty"`
} `json:"affinitygroup,omitempty"`
Diskiowrite int `json:"diskiowrite,omitempty"`
Domain string `json:"domain,omitempty"`
Servicestate string `json:"servicestate,omitempty"`
Diskkbsread int `json:"diskkbsread,omitempty"`
Memory int `json:"memory,omitempty"`
}
type RegisterSSHKeyPairParams struct {
p map[string]interface{}
}
func (p *RegisterSSHKeyPairParams) toURLValues() url.Values {
u := url.Values{}
if p.p == nil {
return u
}
if v, found := p.p["account"]; found {
u.Set("account", v.(string))
}
if v, found := p.p["domainid"]; found {
u.Set("domainid", v.(string))
}
if v, found := p.p["name"]; found {
u.Set("name", v.(string))
}
if v, found := p.p["projectid"]; found {
u.Set("projectid", v.(string))
}
if v, found := p.p["publickey"]; found {
u.Set("publickey", v.(string))
}
return u
}
func (p *RegisterSSHKeyPairParams) SetAccount(v string) {
if p.p == nil {
p.p = make(map[string]interface{})
}
p.p["account"] = v
return
}
func (p *RegisterSSHKeyPairParams) SetDomainid(v string) {
if p.p == nil {
p.p = make(map[string]interface{})
}
p.p["domainid"] = v
return
}
func (p *RegisterSSHKeyPairParams) SetName(v string) {
if p.p == nil {
p.p = make(map[string]interface{})
}
p.p["name"] = v
return
}
func (p *RegisterSSHKeyPairParams) SetProjectid(v string) {
if p.p == nil {
p.p = make(map[string]interface{})
}
p.p["projectid"] = v
return
}
func (p *RegisterSSHKeyPairParams) SetPublickey(v string) {
if p.p == nil {
p.p = make(map[string]interface{})
}
p.p["publickey"] = v
return
}
// You should always use this function to get a new RegisterSSHKeyPairParams instance,
// as then you are sure you have configured all required params
func (s *SSHService) NewRegisterSSHKeyPairParams(name string, publickey string) *RegisterSSHKeyPairParams {
p := &RegisterSSHKeyPairParams{}
p.p = make(map[string]interface{})
p.p["name"] = name
p.p["publickey"] = publickey
return p
}
// Register a public key in a keypair under a certain name
func (s *SSHService) RegisterSSHKeyPair(p *RegisterSSHKeyPairParams) (*RegisterSSHKeyPairResponse, error) {
resp, err := s.cs.newRequest("registerSSHKeyPair", p.toURLValues())
if err != nil {
return nil, err
}
var r RegisterSSHKeyPairResponse
if err := json.Unmarshal(resp, &r); err != nil {
return nil, err
}
return &r, nil
}
type RegisterSSHKeyPairResponse struct {
Fingerprint string `json:"fingerprint,omitempty"`
Name string `json:"name,omitempty"`
}
type CreateSSHKeyPairParams struct {
p map[string]interface{}
}
func (p *CreateSSHKeyPairParams) toURLValues() url.Values {
u := url.Values{}
if p.p == nil {
return u
}
if v, found := p.p["account"]; found {
u.Set("account", v.(string))
}
if v, found := p.p["domainid"]; found {
u.Set("domainid", v.(string))
}
if v, found := p.p["name"]; found {
u.Set("name", v.(string))
}
if v, found := p.p["projectid"]; found {
u.Set("projectid", v.(string))
}
return u
}
func (p *CreateSSHKeyPairParams) SetAccount(v string) {
if p.p == nil {
p.p = make(map[string]interface{})
}
p.p["account"] = v
return
}
func (p *CreateSSHKeyPairParams) SetDomainid(v string) {
if p.p == nil {
p.p = make(map[string]interface{})
}
p.p["domainid"] = v
return
}
func (p *CreateSSHKeyPairParams) SetName(v string) {
if p.p == nil {
p.p = make(map[string]interface{})
}
p.p["name"] = v
return
}
func (p *CreateSSHKeyPairParams) SetProjectid(v string) {
if p.p == nil {
p.p = make(map[string]interface{})
}
p.p["projectid"] = v
return
}
// You should always use this function to get a new CreateSSHKeyPairParams instance,
// as then you are sure you have configured all required params
func (s *SSHService) NewCreateSSHKeyPairParams(name string) *CreateSSHKeyPairParams {
p := &CreateSSHKeyPairParams{}
p.p = make(map[string]interface{})
p.p["name"] = name
return p
}
// Create a new keypair and returns the private key
func (s *SSHService) CreateSSHKeyPair(p *CreateSSHKeyPairParams) (*CreateSSHKeyPairResponse, error) {
resp, err := s.cs.newRequest("createSSHKeyPair", p.toURLValues())
if err != nil {
return nil, err
}
var r CreateSSHKeyPairResponse
if err := json.Unmarshal(resp, &r); err != nil {
return nil, err
}
return &r, nil
}
type CreateSSHKeyPairResponse struct {
Privatekey string `json:"privatekey,omitempty"`
}
type DeleteSSHKeyPairParams struct {
p map[string]interface{}
}
func (p *DeleteSSHKeyPairParams) toURLValues() url.Values {
u := url.Values{}
if p.p == nil {
return u
}
if v, found := p.p["account"]; found {
u.Set("account", v.(string))
}
if v, found := p.p["domainid"]; found {
u.Set("domainid", v.(string))
}
if v, found := p.p["name"]; found {
u.Set("name", v.(string))
}
if v, found := p.p["projectid"]; found {
u.Set("projectid", v.(string))
}
return u
}
func (p *DeleteSSHKeyPairParams) SetAccount(v string) {
if p.p == nil {
p.p = make(map[string]interface{})
}
p.p["account"] = v
return
}
func (p *DeleteSSHKeyPairParams) SetDomainid(v string) {
if p.p == nil {
p.p = make(map[string]interface{})
}
p.p["domainid"] = v
return
}
func (p *DeleteSSHKeyPairParams) SetName(v string) {
if p.p == nil {
p.p = make(map[string]interface{})
}
p.p["name"] = v
return
}
func (p *DeleteSSHKeyPairParams) SetProjectid(v string) {
if p.p == nil {
p.p = make(map[string]interface{})
}
p.p["projectid"] = v
return
}
// You should always use this function to get a new DeleteSSHKeyPairParams instance,
// as then you are sure you have configured all required params
func (s *SSHService) NewDeleteSSHKeyPairParams(name string) *DeleteSSHKeyPairParams {
p := &DeleteSSHKeyPairParams{}
p.p = make(map[string]interface{})
p.p["name"] = name
return p
}
// Deletes a keypair by name
func (s *SSHService) DeleteSSHKeyPair(p *DeleteSSHKeyPairParams) (*DeleteSSHKeyPairResponse, error) {
resp, err := s.cs.newRequest("deleteSSHKeyPair", p.toURLValues())
if err != nil {
return nil, err
}
var r DeleteSSHKeyPairResponse
if err := json.Unmarshal(resp, &r); err != nil {
return nil, err
}
return &r, nil
}
type DeleteSSHKeyPairResponse struct {
Success bool `json:"success,omitempty"`
Displaytext string `json:"displaytext,omitempty"`
}
type ListSSHKeyPairsParams struct {
p map[string]interface{}
}
func (p *ListSSHKeyPairsParams) toURLValues() url.Values {
u := url.Values{}
if p.p == nil {
return u
}
if v, found := p.p["account"]; found {
u.Set("account", v.(string))
}
if v, found := p.p["domainid"]; found {
u.Set("domainid", v.(string))
}
if v, found := p.p["fingerprint"]; found {
u.Set("fingerprint", v.(string))
}
if v, found := p.p["isrecursive"]; found {
vv := strconv.FormatBool(v.(bool))
u.Set("isrecursive", vv)
}
if v, found := p.p["keyword"]; found {
u.Set("keyword", v.(string))
}
if v, found := p.p["listall"]; found {
vv := strconv.FormatBool(v.(bool))
u.Set("listall", vv)
}
if v, found := p.p["name"]; found {
u.Set("name", v.(string))
}
if v, found := p.p["page"]; found {
vv := strconv.Itoa(v.(int))
u.Set("page", vv)
}
if v, found := p.p["pagesize"]; found {
vv := strconv.Itoa(v.(int))
u.Set("pagesize", vv)
}
if v, found := p.p["projectid"]; found {
u.Set("projectid", v.(string))
}
return u
}
func (p *ListSSHKeyPairsParams) SetAccount(v string) {
if p.p == nil {
p.p = make(map[string]interface{})
}
p.p["account"] = v
return
}
func (p *ListSSHKeyPairsParams) SetDomainid(v string) {
if p.p == nil {
p.p = make(map[string]interface{})
}
p.p["domainid"] = v
return
}
func (p *ListSSHKeyPairsParams) SetFingerprint(v string) {
if p.p == nil {
p.p = make(map[string]interface{})
}
p.p["fingerprint"] = v
return
}
func (p *ListSSHKeyPairsParams) SetIsrecursive(v bool) {
if p.p == nil {
p.p = make(map[string]interface{})
}
p.p["isrecursive"] = v
return
}
func (p *ListSSHKeyPairsParams) SetKeyword(v string) {
if p.p == nil {
p.p = make(map[string]interface{})
}
p.p["keyword"] = v
return
}
func (p *ListSSHKeyPairsParams) SetListall(v bool) {
if p.p == nil {
p.p = make(map[string]interface{})
}
p.p["listall"] = v
return
}
func (p *ListSSHKeyPairsParams) SetName(v string) {
if p.p == nil {
p.p = make(map[string]interface{})
}
p.p["name"] = v
return
}
func (p *ListSSHKeyPairsParams) SetPage(v int) {
if p.p == nil {
p.p = make(map[string]interface{})
}
p.p["page"] = v
return
}
func (p *ListSSHKeyPairsParams) SetPagesize(v int) {
if p.p == nil {
p.p = make(map[string]interface{})
}
p.p["pagesize"] = v
return
}
func (p *ListSSHKeyPairsParams) SetProjectid(v string) {
if p.p == nil {
p.p = make(map[string]interface{})
}
p.p["projectid"] = v
return
}
// You should always use this function to get a new ListSSHKeyPairsParams instance,
// as then you are sure you have configured all required params
func (s *SSHService) NewListSSHKeyPairsParams() *ListSSHKeyPairsParams {
p := &ListSSHKeyPairsParams{}
p.p = make(map[string]interface{})
return p
}
// List registered keypairs
func (s *SSHService) ListSSHKeyPairs(p *ListSSHKeyPairsParams) (*ListSSHKeyPairsResponse, error) {
resp, err := s.cs.newRequest("listSSHKeyPairs", p.toURLValues())
if err != nil {
return nil, err
}
var r ListSSHKeyPairsResponse
if err := json.Unmarshal(resp, &r); err != nil {
return nil, err
}
return &r, nil
}
type ListSSHKeyPairsResponse struct {
Count int `json:"count"`
SSHKeyPairs []*SSHKeyPair `json:"sshkeypair"`
}
type SSHKeyPair struct {
Fingerprint string `json:"fingerprint,omitempty"`
Name string `json:"name,omitempty"`
}