-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathresource_cloudstack_instance.go
More file actions
948 lines (791 loc) · 25.8 KB
/
resource_cloudstack_instance.go
File metadata and controls
948 lines (791 loc) · 25.8 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
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
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
//
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you 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 (
"crypto/sha1"
"encoding/base64"
"encoding/hex"
"fmt"
"log"
"strings"
"github.com/apache/cloudstack-go/v2/cloudstack"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)
func resourceCloudStackInstance() *schema.Resource {
return &schema.Resource{
Create: resourceCloudStackInstanceCreate,
Read: resourceCloudStackInstanceRead,
Update: resourceCloudStackInstanceUpdate,
Delete: resourceCloudStackInstanceDelete,
Importer: &schema.ResourceImporter{
State: resourceCloudStackInstanceImport,
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"display_name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"service_offering": {
Type: schema.TypeString,
Required: true,
},
"disk_offering": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"override_disk_offering": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"network_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"ip_address": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"template": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"root_disk_size": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
ForceNew: true,
},
"group": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"affinity_group_ids": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
ConflictsWith: []string{"affinity_group_names"},
},
"affinity_group_names": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
ConflictsWith: []string{"affinity_group_ids"},
},
"security_group_ids": {
Type: schema.TypeSet,
Optional: true,
ForceNew: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
ConflictsWith: []string{"security_group_names"},
},
"security_group_names": {
Type: schema.TypeSet,
Optional: true,
ForceNew: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
ConflictsWith: []string{"security_group_ids"},
},
"project": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"zone": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"keypair": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"keypairs"},
},
"keypairs": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
ConflictsWith: []string{"keypair"},
},
"host_id": {
Type: schema.TypeString,
Optional: true,
},
"cluster_id": {
Type: schema.TypeString,
Optional: true,
},
"uefi": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"boot_mode": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{"Secure", "Legacy"}, true),
ForceNew: true,
Description: "The boot mode of the instance. Can only be specified when uefi is true. Valid options are 'Legacy' and 'Secure'.",
},
"start_vm": {
Type: schema.TypeBool,
Optional: true,
Default: true,
ForceNew: true,
},
"user_data": {
Type: schema.TypeString,
Optional: true,
StateFunc: func(v interface{}) string {
switch v.(type) {
case string:
hash := sha1.Sum([]byte(v.(string)))
return hex.EncodeToString(hash[:])
default:
return ""
}
},
},
"userdata_id": {
Type: schema.TypeString,
Optional: true,
},
"userdata_details": {
Type: schema.TypeMap,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"details": {
Type: schema.TypeMap,
Optional: true,
},
"properties": {
Type: schema.TypeMap,
Optional: true,
},
"nicnetworklist": {
Type: schema.TypeMap,
Optional: true,
},
"expunge": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"pod_id": {
Type: schema.TypeString,
Optional: true,
},
"tags": tagsSchema(),
},
}
}
func resourceCloudStackInstanceCreate(d *schema.ResourceData, meta interface{}) error {
cs := meta.(*cloudstack.CloudStackClient)
// Retrieve the service_offering ID
serviceofferingid, e := retrieveID(cs, "service_offering", d.Get("service_offering").(string))
if e != nil {
return e.Error()
}
// Retrieve the zone ID
zoneid, e := retrieveID(cs, "zone", d.Get("zone").(string))
if e != nil {
return e.Error()
}
// Retrieve the zone object
zone, _, err := cs.Zone.GetZoneByID(zoneid)
if err != nil {
return err
}
// Retrieve the template ID
templateid, e := retrieveTemplateID(cs, zone.Id, d.Get("template").(string))
if e != nil {
return e.Error()
}
if bootMode, hasBoot := d.GetOk("boot_mode"); hasBoot && !d.Get("uefi").(bool) {
return fmt.Errorf("boot_mode can only be specified when uefi is true, got boot_mode=%s with uefi=false", bootMode.(string))
}
// Create a new parameter struct
p := cs.VirtualMachine.NewDeployVirtualMachineParams(serviceofferingid, templateid, zone.Id)
p.SetStartvm(d.Get("start_vm").(bool))
vmDetails := make(map[string]string)
if details, ok := d.GetOk("details"); ok {
for k, v := range details.(map[string]interface{}) {
vmDetails[k] = v.(string)
}
p.SetDetails(vmDetails)
}
// Set VM Properties
vmProperties := make(map[string]string)
if properties, ok := d.GetOk("properties"); ok {
for k, v := range properties.(map[string]interface{}) {
vmProperties[k] = v.(string)
}
p.SetProperties(vmProperties)
}
// SetNicNetworkList
if nicnetworklist, ok := d.GetOk("nicnetworklist"); ok {
nicNetworkDetails := []map[string]string{
{
"nic": nicnetworklist.(map[string]interface{})["nic"].(string),
"network": nicnetworklist.(map[string]interface{})["network"].(string),
},
}
p.SetNicnetworklist(nicNetworkDetails)
}
// Set the name
name, hasName := d.GetOk("name")
if hasName {
p.SetName(name.(string))
}
// Set the display name
if displayname, ok := d.GetOk("display_name"); ok {
p.SetDisplayname(displayname.(string))
} else if hasName {
p.SetDisplayname(name.(string))
}
// If there is a root_disk_size supplied, add it to the parameter struct
if rootdisksize, ok := d.GetOk("root_disk_size"); ok {
p.SetRootdisksize(int64(rootdisksize.(int)))
}
if diskoffering, ok := d.GetOk("disk_offering"); ok {
// Retrieve the disk_offering ID
diskofferingid, e := retrieveID(cs, "disk_offering", diskoffering.(string))
if e != nil {
return e.Error()
}
p.SetDiskofferingid(diskofferingid)
}
if override_disk_offering, ok := d.GetOk("override_disk_offering"); ok {
// Retrieve the override_disk_offering ID
override_disk_offeringid, e := retrieveID(cs, "disk_offering", override_disk_offering.(string))
if e != nil {
return e.Error()
}
p.SetOverridediskofferingid(override_disk_offeringid)
}
if d.Get("uefi").(bool) {
p.SetBoottype("UEFI")
if bootmode, ok := d.GetOk("boot_mode"); ok {
p.SetBootmode(bootmode.(string))
} else {
p.SetBootmode("Legacy")
}
}
if zone.Networktype == "Advanced" {
// Set the default network ID
networkID := d.Get("network_id").(string)
p.SetNetworkids([]string{networkID})
// If no project is explicitly set, try to inherit it from the network
if _, ok := d.GetOk("project"); !ok && networkID != "" {
// Get the network to retrieve its project
// Use projectid=-1 to search across all projects
network, count, err := cs.Network.GetNetworkByID(networkID, cloudstack.WithProject("-1"))
if err == nil && count > 0 && network.Projectid != "" {
log.Printf("[DEBUG] Inheriting project %s from network %s", network.Projectid, networkID)
p.SetProjectid(network.Projectid)
}
}
}
// If there is a ipaddres supplied, add it to the parameter struct
if ipaddress, ok := d.GetOk("ip_address"); ok {
p.SetIpaddress(ipaddress.(string))
}
// If there is a group supplied, add it to the parameter struct
if group, ok := d.GetOk("group"); ok {
p.SetGroup(group.(string))
}
// If there are affinity group IDs supplied, add them to the parameter struct
if agIDs := d.Get("affinity_group_ids").(*schema.Set); agIDs.Len() > 0 {
var groups []string
for _, group := range agIDs.List() {
groups = append(groups, group.(string))
}
p.SetAffinitygroupids(groups)
}
// If there are affinity group names supplied, add them to the parameter struct
if agNames := d.Get("affinity_group_names").(*schema.Set); agNames.Len() > 0 {
var groups []string
for _, group := range agNames.List() {
groups = append(groups, group.(string))
}
p.SetAffinitygroupnames(groups)
}
// If there are security group IDs supplied, add them to the parameter struct
if sgIDs := d.Get("security_group_ids").(*schema.Set); sgIDs.Len() > 0 {
var groups []string
for _, group := range sgIDs.List() {
groups = append(groups, group.(string))
}
p.SetSecuritygroupids(groups)
}
// If there are security group names supplied, add them to the parameter struct
if sgNames := d.Get("security_group_names").(*schema.Set); sgNames.Len() > 0 {
var groups []string
for _, group := range sgNames.List() {
groups = append(groups, group.(string))
}
p.SetSecuritygroupnames(groups)
}
// If there is a project supplied, we retrieve and set the project id
// This will override the inherited project from network if explicitly set
if err := setProjectid(p, cs, d); err != nil {
return err
}
// If a keypair is supplied, add it to the parameter struct
if keypair, ok := d.GetOk("keypair"); ok {
p.SetKeypair(keypair.(string))
}
if keypairs, ok := d.GetOk("keypairs"); ok {
var keypairStrings []string
for _, kp := range keypairs.([]interface{}) {
keypairStrings = append(keypairStrings, fmt.Sprintf("%v", kp))
}
p.SetKeypairs(keypairStrings)
}
// If a host_id is supplied, add it to the parameter struct
if hostid, ok := d.GetOk("host_id"); ok {
p.SetHostid(hostid.(string))
}
// If a pod_id is supplied, add it to the parameter struct
if podid, ok := d.GetOk("pod_id"); ok {
p.SetPodid(podid.(string))
}
// If a cluster_id is supplied, add it to the parameter struct
if clusterid, ok := d.GetOk("cluster_id"); ok {
p.SetClusterid(clusterid.(string))
}
if userData, ok := d.GetOk("user_data"); ok {
ud, err := getUserData(userData.(string))
if err != nil {
return err
}
p.SetUserdata(ud)
}
if userdataID, ok := d.GetOk("userdata_id"); ok {
p.SetUserdataid(userdataID.(string))
}
if userdataDetails, ok := d.GetOk("userdata_details"); ok {
udDetails := make(map[string]string)
index := 0
for k, v := range userdataDetails.(map[string]interface{}) {
udDetails[fmt.Sprintf("userdatadetails[%d].%s", index, k)] = v.(string)
index++
}
p.SetUserdatadetails(udDetails)
}
// Create the new instance
r, err := cs.VirtualMachine.DeployVirtualMachine(p)
if err != nil {
return fmt.Errorf("Error creating the new instance %s: %s", name, err)
}
d.SetId(r.Id)
// Set tags if necessary
if err = setTags(cs, d, "userVm"); err != nil {
return fmt.Errorf("Error setting tags on the new instance %s: %s", name, err)
}
// Set the connection info for any configured provisioners
d.SetConnInfo(map[string]string{
"host": r.Nic[0].Ipaddress,
"password": r.Password,
})
return resourceCloudStackInstanceRead(d, meta)
}
func resourceCloudStackInstanceRead(d *schema.ResourceData, meta interface{}) error {
cs := meta.(*cloudstack.CloudStackClient)
// Get the virtual machine details
// First try with the project from state (if any)
project := d.Get("project").(string)
vm, count, err := cs.VirtualMachine.GetVirtualMachineByID(
d.Id(),
cloudstack.WithProject(project),
)
// If not found and no explicit project was set, try with projectid=-1
// This handles the case where the project was inherited from network
if count == 0 && project == "" {
vm, count, err = cs.VirtualMachine.GetVirtualMachineByID(
d.Id(),
cloudstack.WithProject("-1"),
)
}
if err != nil {
if count == 0 {
log.Printf("[DEBUG] Instance %s does no longer exist", d.Get("name").(string))
d.SetId("")
return nil
}
return err
}
// Update the config
d.Set("name", vm.Name)
d.Set("display_name", vm.Displayname)
d.Set("group", vm.Group)
// Set the project if the instance belongs to one
if vm.Project != "" {
d.Set("project", vm.Project)
}
// In some rare cases (when destroying a machine fails) it can happen that
// an instance does not have any attached NIC anymore.
if len(vm.Nic) > 0 {
d.Set("network_id", vm.Nic[0].Networkid)
d.Set("ip_address", vm.Nic[0].Ipaddress)
}
// Create a new param struct.
p := cs.Volume.NewListVolumesParams()
p.SetType("ROOT")
p.SetVirtualmachineid(d.Id())
// Get the root disk of the instance.
l, err := cs.Volume.ListVolumes(p)
if err != nil {
return err
}
// If we found the root disk, then update its size.
if len(l.Volumes) != 1 {
log.Printf("[DEBUG] Failed to find root disk of instance: %s", vm.Name)
} else {
d.Set("root_disk_size", l.Volumes[0].Size>>30) // B to GiB
}
if _, ok := d.GetOk("affinity_group_ids"); ok {
groups := &schema.Set{F: schema.HashString}
for _, group := range vm.Affinitygroup {
groups.Add(group.Id)
}
d.Set("affinity_group_ids", groups)
}
if _, ok := d.GetOk("affinity_group_names"); ok {
groups := &schema.Set{F: schema.HashString}
for _, group := range vm.Affinitygroup {
groups.Add(group.Name)
}
d.Set("affinity_group_names", groups)
}
if _, ok := d.GetOk("security_group_ids"); ok {
groups := &schema.Set{F: schema.HashString}
for _, group := range vm.Securitygroup {
groups.Add(group.Id)
}
d.Set("security_group_ids", groups)
}
if _, ok := d.GetOk("security_group_names"); ok {
groups := &schema.Set{F: schema.HashString}
for _, group := range vm.Securitygroup {
groups.Add(group.Name)
}
d.Set("security_group_names", groups)
}
d.Set("tags", tagsToMap(vm.Tags))
setValueOrID(d, "service_offering", vm.Serviceofferingname, vm.Serviceofferingid)
setValueOrID(d, "disk_offering", vm.Diskofferingname, vm.Diskofferingid)
setValueOrID(d, "template", vm.Templatename, vm.Templateid)
setValueOrID(d, "project", vm.Project, vm.Projectid)
setValueOrID(d, "zone", vm.Zonename, vm.Zoneid)
d.Set("uefi", strings.EqualFold(vm.Boottype, "UEFI"))
if strings.EqualFold(vm.Boottype, "UEFI") && vm.Bootmode != "" {
d.Set("boot_mode", vm.Bootmode)
}
if vm.Userdataid != "" {
d.Set("userdata_id", vm.Userdataid)
}
if vm.Userdata != "" {
decoded, err := base64.StdEncoding.DecodeString(vm.Userdata)
if err != nil {
d.Set("user_data", vm.Userdata)
} else {
d.Set("user_data", string(decoded))
}
}
if vm.Userdatadetails != "" {
log.Printf("[DEBUG] Instance %s has userdata details: %s", vm.Name, vm.Userdatadetails)
}
return nil
}
func resourceCloudStackInstanceUpdate(d *schema.ResourceData, meta interface{}) error {
cs := meta.(*cloudstack.CloudStackClient)
name := d.Get("name").(string)
// Check if the display name is changed and if so, update the virtual machine
if d.HasChange("display_name") {
log.Printf("[DEBUG] Display name changed for %s, starting update", name)
// Create a new parameter struct
p := cs.VirtualMachine.NewUpdateVirtualMachineParams(d.Id())
// Set the new display name
p.SetDisplayname(d.Get("display_name").(string))
// Update the display name
_, err := cs.VirtualMachine.UpdateVirtualMachine(p)
if err != nil {
return fmt.Errorf(
"Error updating the display name for instance %s: %s", name, err)
}
}
// Check if the group is changed and if so, update the virtual machine
if d.HasChange("group") {
log.Printf("[DEBUG] Group changed for %s, starting update", name)
// Create a new parameter struct
p := cs.VirtualMachine.NewUpdateVirtualMachineParams(d.Id())
// Set the new group
p.SetGroup(d.Get("group").(string))
// Update the display name
_, err := cs.VirtualMachine.UpdateVirtualMachine(p)
if err != nil {
return fmt.Errorf(
"Error updating the group for instance %s: %s", name, err)
}
}
// Attributes that require reboot to update
if d.HasChange("name") || d.HasChange("service_offering") || d.HasChange("affinity_group_ids") ||
d.HasChange("affinity_group_names") || d.HasChange("keypair") || d.HasChange("keypairs") ||
d.HasChange("user_data") || d.HasChange("userdata_id") || d.HasChange("userdata_details") {
// Before we can actually make these changes, the virtual machine must be stopped
_, err := cs.VirtualMachine.StopVirtualMachine(
cs.VirtualMachine.NewStopVirtualMachineParams(d.Id()))
if err != nil {
return fmt.Errorf(
"Error stopping instance %s before making changes: %s", name, err)
}
// Check if the name has changed and if so, update the name
if d.HasChange("name") {
log.Printf("[DEBUG] Name for %s changed to %s, starting update", d.Id(), name)
// Create a new parameter struct
p := cs.VirtualMachine.NewUpdateVirtualMachineParams(d.Id())
// Set the new name
p.SetName(name)
// Update the display name
_, err := cs.VirtualMachine.UpdateVirtualMachine(p)
if err != nil {
return fmt.Errorf(
"Error updating the name for instance %s: %s", name, err)
}
}
// Check if the service offering is changed and if so, update the offering
if d.HasChange("service_offering") {
log.Printf("[DEBUG] Service offering changed for %s, starting update", name)
// Retrieve the service_offering ID
serviceofferingid, e := retrieveID(cs, "service_offering", d.Get("service_offering").(string))
if e != nil {
return e.Error()
}
// Create a new parameter struct
p := cs.VirtualMachine.NewChangeServiceForVirtualMachineParams(d.Id(), serviceofferingid)
// Change the service offering
_, err = cs.VirtualMachine.ChangeServiceForVirtualMachine(p)
if err != nil {
return fmt.Errorf(
"Error changing the service offering for instance %s: %s", name, err)
}
}
// Check if the affinity group IDs have changed and if so, update the IDs
if d.HasChange("affinity_group_ids") {
p := cs.AffinityGroup.NewUpdateVMAffinityGroupParams(d.Id())
groups := []string{}
if agIDs := d.Get("affinity_group_ids").(*schema.Set); agIDs.Len() > 0 {
for _, group := range agIDs.List() {
groups = append(groups, group.(string))
}
}
// Set the new groups
p.SetAffinitygroupids(groups)
// Update the affinity groups
_, err = cs.AffinityGroup.UpdateVMAffinityGroup(p)
if err != nil {
return fmt.Errorf(
"Error updating the affinity groups for instance %s: %s", name, err)
}
}
// Check if the affinity group names have changed and if so, update the names
if d.HasChange("affinity_group_names") {
p := cs.AffinityGroup.NewUpdateVMAffinityGroupParams(d.Id())
groups := []string{}
if agNames := d.Get("affinity_group_names").(*schema.Set); agNames.Len() > 0 {
for _, group := range agNames.List() {
groups = append(groups, group.(string))
}
}
// Set the new groups
p.SetAffinitygroupnames(groups)
// Update the affinity groups
_, err = cs.AffinityGroup.UpdateVMAffinityGroup(p)
if err != nil {
return fmt.Errorf(
"Error updating the affinity groups for instance %s: %s", name, err)
}
}
// Check if the keypair has changed and if so, update the keypair
if d.HasChange("keypair") || d.HasChange("keypairs") {
log.Printf("[DEBUG] SSH keypair(s) changed for %s, starting update", name)
p := cs.SSH.NewResetSSHKeyForVirtualMachineParams(d.Id())
if keypair, ok := d.GetOk("keypair"); ok {
p.SetKeypair(keypair.(string))
}
if keypairs, ok := d.GetOk("keypairs"); ok {
// Convert keypairsInterface to []interface{}
keypairsInterfaces := keypairs.([]interface{})
// Now, safely convert []interface{} to []string with error handling
strKeyPairs := make([]string, len(keypairsInterfaces))
for i, v := range keypairsInterfaces {
switch v := v.(type) {
case string:
strKeyPairs[i] = v
default:
log.Printf("Value at index %d is not a string: %v", i, v)
continue
}
}
p.SetKeypairs(strKeyPairs)
}
// If there is a project supplied, we retrieve and set the project id
if err := setProjectid(p, cs, d); err != nil {
return err
}
// Change the ssh keypair
_, err = cs.SSH.ResetSSHKeyForVirtualMachine(p)
if err != nil {
return fmt.Errorf(
"Error changing the SSH keypair(s) for instance %s: %s", name, err)
}
}
// Check if the user data has changed and if so, update the user data
if d.HasChange("user_data") {
log.Printf("[DEBUG] user_data changed for %s, starting update", name)
ud, err := getUserData(d.Get("user_data").(string))
if err != nil {
return err
}
p := cs.VirtualMachine.NewUpdateVirtualMachineParams(d.Id())
p.SetUserdata(ud)
_, err = cs.VirtualMachine.UpdateVirtualMachine(p)
if err != nil {
return fmt.Errorf(
"Error updating user_data for instance %s: %s", name, err)
}
}
if d.HasChange("userdata_id") {
log.Printf("[DEBUG] userdata_id changed for %s, starting update", name)
p := cs.VirtualMachine.NewUpdateVirtualMachineParams(d.Id())
if userdataID, ok := d.GetOk("userdata_id"); ok {
p.SetUserdataid(userdataID.(string))
}
_, err := cs.VirtualMachine.UpdateVirtualMachine(p)
if err != nil {
return fmt.Errorf(
"Error updating userdata_id for instance %s: %s", name, err)
}
}
if d.HasChange("userdata_details") {
log.Printf("[DEBUG] userdata_details changed for %s, starting update", name)
p := cs.VirtualMachine.NewUpdateVirtualMachineParams(d.Id())
if userdataDetails, ok := d.GetOk("userdata_details"); ok {
udDetails := make(map[string]string)
index := 0
for k, v := range userdataDetails.(map[string]interface{}) {
udDetails[fmt.Sprintf("userdatadetails[%d].%s", index, k)] = v.(string)
index++
}
p.SetUserdatadetails(udDetails)
}
_, err := cs.VirtualMachine.UpdateVirtualMachine(p)
if err != nil {
return fmt.Errorf(
"Error updating userdata_details for instance %s: %s", name, err)
}
}
// Start the virtual machine again
_, err = cs.VirtualMachine.StartVirtualMachine(
cs.VirtualMachine.NewStartVirtualMachineParams(d.Id()))
if err != nil {
return fmt.Errorf(
"Error starting instance %s after making changes", name)
}
}
// Check if the tags have changed and if so, update the tags
if d.HasChange("tags") {
if err := updateTags(cs, d, "UserVm"); err != nil {
return fmt.Errorf("Error updating tags on instance %s: %s", name, err)
}
}
// Check if the details have changed and if so, update the details
if d.HasChange("details") {
p := cs.VirtualMachine.NewUpdateVirtualMachineParams(d.Id())
vmDetails := make(map[string]string)
if details := d.Get("details"); details != nil {
for k, v := range details.(map[string]interface{}) {
vmDetails[k] = v.(string)
}
}
p.SetDetails(vmDetails)
_, err := cs.VirtualMachine.UpdateVirtualMachine(p)
if err != nil {
return fmt.Errorf(
"Error updating the details for instance %s: %s", vmDetails, err)
}
}
return resourceCloudStackInstanceRead(d, meta)
}
func resourceCloudStackInstanceDelete(d *schema.ResourceData, meta interface{}) error {
cs := meta.(*cloudstack.CloudStackClient)
// Create a new parameter struct
p := cs.VirtualMachine.NewDestroyVirtualMachineParams(d.Id())
if d.Get("expunge").(bool) {
p.SetExpunge(true)
}
log.Printf("[INFO] Destroying instance: %s", d.Get("name").(string))
if _, err := cs.VirtualMachine.DestroyVirtualMachine(p); err != nil {
// This is a very poor way to be told the ID does no longer exist :(
if strings.Contains(err.Error(), fmt.Sprintf(
"Invalid parameter id value=%s due to incorrect long value format, "+
"or entity does not exist", d.Id())) {
return nil
}
return fmt.Errorf("Error destroying instance: %s", err)
}
return nil
}
func resourceCloudStackInstanceImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
// We set start_vm to true as that matches the default and we assume that
// when you need to import an instance it means it is already running.
d.Set("start_vm", true)
return importStatePassthrough(d, meta)
}
// getUserData returns the user data as a base64 encoded string
func getUserData(userData string) (string, error) {
ud := userData
if _, err := base64.StdEncoding.DecodeString(ud); err != nil {
ud = base64.StdEncoding.EncodeToString([]byte(userData))
}
return ud, nil
}