-
Notifications
You must be signed in to change notification settings - Fork 13
/
resource_storage_controller.go
886 lines (742 loc) · 27 KB
/
resource_storage_controller.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
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
package ddcloud
import (
"fmt"
"log"
"github.com/DimensionDataResearch/dd-cloud-compute-terraform/models"
"github.com/DimensionDataResearch/dd-cloud-compute-terraform/retry"
"github.com/DimensionDataResearch/go-dd-cloud-compute/compute"
"github.com/hashicorp/terraform/helper/schema"
)
const (
resourceKeyStorageControllerServerID = "server"
resourceKeyStorageControllerBusNumber = "scsi_bus_number"
resourceKeyStorageControllerAdapterType = "adapter_type"
resourceKeyStorageControllerDisk = "disk"
resourceKeyStorageControllerDiskID = "id"
resourceKeyStorageControllerDiskUnitID = "scsi_unit_id"
resourceKeyStorageControllerDiskSizeGB = "size_gb"
resourceKeyStorageControllerDiskSpeed = "speed"
)
/*
* AF: Note that "terraform plan" produces an incorrect diff when a disk is removed from the ddcloud_storage_controller
* (no idea why, but I'd say it's probably a bug, and should be reported as such).
*
* This implementation will still do the correct thing (i.e. remove the disk), but the diff output from "terraform plan" is confusing for the user.
*/
func resourceStorageController() *schema.Resource {
return &schema.Resource{
SchemaVersion: 1,
Create: resourceStorageControllerCreate,
Read: resourceStorageControllerRead,
Update: resourceStorageControllerUpdate,
Delete: resourceStorageControllerDelete,
Importer: &schema.ResourceImporter{
State: resourceStorageControllerImport,
},
Schema: map[string]*schema.Schema{
resourceKeyStorageControllerServerID: &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The Id of the server that the controller is attached to",
},
resourceKeyStorageControllerBusNumber: &schema.Schema{
Type: schema.TypeInt,
Required: true,
ForceNew: true,
Description: "The controller's SCSI bus number",
},
resourceKeyStorageControllerAdapterType: &schema.Schema{
Type: schema.TypeString,
Optional: true,
Default: compute.StorageControllerAdapterTypeLSILogicParallel,
ForceNew: true,
Description: "The type of storage adapter used to represent the controller",
},
resourceKeyStorageControllerDisk: schemaDisk(),
},
}
}
// Create a storage controller resource.
func resourceStorageControllerCreate(data *schema.ResourceData, provider interface{}) error {
propertyHelper := propertyHelper(data)
serverID := data.Get(resourceKeyStorageControllerServerID).(string)
adapterType := data.Get(resourceKeyStorageControllerAdapterType).(string)
busNumber := data.Get(resourceKeyStorageControllerBusNumber).(int)
providerState := provider.(*providerState)
apiClient := providerState.Client()
server, err := apiClient.GetServer(serverID)
if err != nil {
return err
}
if server == nil {
return fmt.Errorf("cannot find server '%s'", serverID)
}
log.Printf("Create storage controller for SCSI bus %d in server '%s'.",
busNumber,
server.Name,
)
var targetController *compute.VirtualMachineSCSIController
if busNumber == 0 {
// Default controller (always present)
targetController = server.SCSIControllers.GetByBusNumber(busNumber)
if targetController == nil {
return fmt.Errorf("cannot find controller for bus %d in server '%s'", busNumber, serverID)
}
log.Printf("This controller is the default controller; will treat as already-created.")
} else {
operationDescription := fmt.Sprintf("Add storage controller for bus %d to server '%s'",
busNumber,
serverID,
)
err = providerState.RetryAction(operationDescription, func(context retry.Context) {
asyncLock := providerState.AcquireAsyncOperationLock(operationDescription)
defer asyncLock.Release()
addControllerError := apiClient.AddSCSIControllerToServer(serverID, adapterType, busNumber)
if compute.IsResourceBusyError(addControllerError) {
context.Retry()
} else if addControllerError != nil {
context.Fail(addControllerError)
}
})
server, err = apiClient.GetServer(serverID)
if err != nil {
return err
}
if server == nil {
return fmt.Errorf("cannot find server '%s'", serverID)
}
resource, err := apiClient.WaitForChange(
compute.ResourceTypeServer,
serverID,
"Add storage controller",
resourceUpdateTimeoutServer,
)
if err != nil {
return err
}
server = resource.(*compute.Server)
targetController = server.SCSIControllers.GetByBusNumber(busNumber)
if targetController == nil {
return fmt.Errorf("cannot find controller for bus %d in server '%s'", busNumber, serverID)
}
}
log.Printf("Target storage controller '%s' has configuration: %#v",
targetController.ID,
targetController,
)
data.SetId(targetController.ID)
configuredDisks := propertyHelper.GetDisks()
actualDisks := models.NewDisksFromVirtualMachineSCSIController(*targetController)
if configuredDisks.IsEmpty() {
// No explicitly-configured disks so just populate from current controller state.
propertyHelper.SetDisks(actualDisks)
propertyHelper.SetPartial(resourceKeyServerDisk)
log.Printf("No disks configured; storage controller '%s' now has %d disks: %#v.", serverID, server.SCSIControllers.GetDiskCount(), server.SCSIControllers)
return nil
}
return updateStorageControllerDisks(data, providerState)
}
// Read a storage controller resource.
func resourceStorageControllerRead(data *schema.ResourceData, provider interface{}) error {
propertyHelper := propertyHelper(data)
controllerID := data.Id()
serverID := data.Get(resourceKeyStorageControllerServerID).(string)
providerState := provider.(*providerState)
apiClient := providerState.Client()
server, err := apiClient.GetServer(serverID)
if err != nil {
return err
}
if server == nil {
log.Printf("Cannot find server '%s' for controller '%s'.", serverID, controllerID)
// If the server is deleted, then so is the controller.
data.SetId("")
return nil
}
targetController := server.SCSIControllers.GetByID(controllerID)
if targetController == nil {
log.Printf("Cannot find controller '%s' in server '%s'.", controllerID, serverID)
// Mark as deleted.
data.SetId("")
return nil
}
log.Printf("Read storage controller '%s' in server '%s'.",
controllerID,
server.Name,
)
configuredDisks := propertyHelper.GetDisks()
log.Printf("Configuration for storage controller '%s' (bus %d) specifies %d disks: %#v.", controllerID, targetController.BusNumber, len(configuredDisks), configuredDisks)
oldConfDisks, newConfDisks := data.GetChange(resourceKeyServerDisk)
log.Printf("Configuration for storage controller '%s' (bus %d) previously specified: %#v.", controllerID, targetController.BusNumber, oldConfDisks)
log.Printf("Configuration for storage controller '%s' (bus %d) now specifies: %#v.", controllerID, targetController.BusNumber, newConfDisks)
actualDisks := models.NewDisksFromVirtualMachineSCSIController(*targetController)
log.Printf("Storage controller '%s' currently has %d disks: %#v.", controllerID, len(actualDisks), actualDisks)
propertyHelper.SetDisks(actualDisks)
return nil
}
// Update a storage controller resource.
func resourceStorageControllerUpdate(data *schema.ResourceData, provider interface{}) error {
propertyHelper := propertyHelper(data)
controllerID := data.Id()
serverID := data.Get(resourceKeyStorageControllerServerID).(string)
providerState := provider.(*providerState)
apiClient := providerState.Client()
server, err := apiClient.GetServer(serverID)
if err != nil {
return err
}
if server == nil {
log.Printf("Cannot find server '%s' for controller '%s'; will treat the controller as deleted.", serverID, controllerID)
// If the server is deleted, then so is the controller.
data.SetId("")
return nil
}
targetController := server.SCSIControllers.GetByID(controllerID)
if targetController == nil {
log.Printf("Cannot find controller '%s' in server '%s'; will treat the controller as deleted.", controllerID, serverID)
// Mark as deleted.
data.SetId("")
return nil
}
log.Printf("Update storage controller '%s' in server '%s'.",
controllerID,
server.Name,
)
configuredDisks := propertyHelper.GetDisks()
actualDisks := models.NewDisksFromVirtualMachineSCSIController(*targetController)
if configuredDisks.IsEmpty() {
// No explicitly-configured disks.
propertyHelper.SetDisks(actualDisks)
propertyHelper.SetPartial(resourceKeyServerDisk)
log.Printf("No disks configured; storage controller '%s' now has %d disks: %#v.", serverID, server.SCSIControllers.GetDiskCount(), server.SCSIControllers)
return nil
}
return updateStorageControllerDisks(data, providerState)
}
// Delete a storage controller resource.
func resourceStorageControllerDelete(data *schema.ResourceData, provider interface{}) error {
controllerID := data.Id()
serverID := data.Get(resourceKeyStorageControllerServerID).(string)
providerState := provider.(*providerState)
apiClient := providerState.Client()
targetController, err := getStorageController(apiClient, data)
if err != nil {
return err
}
log.Printf("Delete storage controller '%s' in server '%s'.",
controllerID,
serverID,
)
removeDisks := models.NewDisksFromVirtualMachineSCSIController(*targetController)
err = processRemoveStorageControllerDisks(removeDisks, data, providerState)
if err != nil {
return err
}
// We can't remove the SCSI adapter if it still has a disk (i.e. the last disk in the server).
targetController, err = getStorageController(apiClient, data)
if err != nil {
return err
}
if len(targetController.Disks) > 0 {
log.Printf("Treating storage controller '%s' in server '%s' as deleted because it still has one or more disks after disk-removal processing; this indicates that the minimum-disk-count-per-server limit has been exceeded.",
controllerID,
serverID,
)
return nil
}
operationDescription := fmt.Sprintf("Remove storage controller '%s' (SCSI bus %d) from server '%s'",
targetController.ID,
targetController.BusNumber,
serverID,
)
err = providerState.RetryAction(operationDescription, func(context retry.Context) {
asyncLock := providerState.AcquireAsyncOperationLock(operationDescription)
defer asyncLock.Release()
var removeSCSIControllerError error
removeSCSIControllerError = apiClient.RemoveSCSIControllerFromServer(controllerID)
if compute.IsResourceBusyError(removeSCSIControllerError) {
context.Retry()
} else if removeSCSIControllerError != nil {
context.Fail(removeSCSIControllerError)
}
})
if err != nil {
return err
}
log.Printf("Removing storage controller '%s' (SCSI bus %d) from server '%s'...",
targetController.ID,
targetController.BusNumber,
serverID,
)
_, err = apiClient.WaitForChange(
compute.ResourceTypeServer,
serverID,
fmt.Sprintf("Remove SCSI controller '%s' (bus %d)", targetController.ID, targetController.BusNumber),
resourceUpdateTimeoutServer,
)
if err != nil {
return err
}
log.Printf("Removed storage controller '%s' (SCSI bus %d) from server '%s'.",
targetController.ID,
targetController.BusNumber,
serverID,
)
return nil
}
// Import data for an existing storage controller.
func resourceStorageControllerImport(data *schema.ResourceData, provider interface{}) (importedData []*schema.ResourceData, err error) {
providerState := provider.(*providerState)
apiClient := providerState.Client()
storageControllerID := data.Id()
serverID := data.Get(resourceKeyStorageControllerServerID).(string)
log.Printf("Import storage controller '%s' in server '%s'.", storageControllerID, serverID)
server, err := apiClient.GetServer(serverID)
if err != nil {
return
}
if server == nil {
err = fmt.Errorf("Server '%s' not found", serverID)
return
}
storageController := server.SCSIControllers.GetByID(storageControllerID)
if storageController == nil {
err = fmt.Errorf("Storage controller '%s' not found in server '%s'", storageControllerID, serverID)
return
}
data.Set(resourceKeyStorageControllerBusNumber, storageController.BusNumber)
data.Set(resourceKeyStorageControllerAdapterType, storageController.AdapterType)
propertyHelper(data).SetDisks(
models.NewDisksFromVirtualMachineSCSIController(*storageController),
)
importedData = []*schema.ResourceData{data}
return
}
// When updating a storage controller resource, synchronise the controller's disk attributes with its resource data
func updateStorageControllerDisks(data *schema.ResourceData, providerState *providerState) error {
propertyHelper := propertyHelper(data)
controllerID := data.Id()
serverID := data.Get(resourceKeyStorageControllerServerID).(string)
log.Printf("Configure disks for storage controller '%s' in server '%s'...", controllerID, serverID)
apiClient := providerState.Client()
server, err := apiClient.GetServer(serverID)
if err != nil {
return err
}
if server == nil {
data.SetId("")
return fmt.Errorf("server '%s' has been deleted", serverID)
}
targetController := server.SCSIControllers.GetByID(controllerID)
if targetController == nil {
return fmt.Errorf("cannot find storage controller '%s' in server '%s'", controllerID, serverID)
}
log.Printf("Current state for storage controller '%s': %#v",
controllerID,
targetController,
)
// Filter disks so we're only looking at ones from this controller.
actualDisks := models.NewDisksFromVirtualMachineSCSIController(*targetController)
log.Printf("Storage controller '%s' currently has %d disks: %#v.", controllerID, len(actualDisks), actualDisks)
configuredDisks := propertyHelper.GetDisks()
log.Printf("Configuration for storage controller '%s' (bus %d) specifies %d disks: %#v.", controllerID, targetController.BusNumber, len(configuredDisks), configuredDisks)
err = validateStorageControllerDisks(configuredDisks)
if err != nil {
return err
}
addDisks, modifyDisks, removeDisks := configuredDisks.SplitByAction(actualDisks)
if addDisks.IsEmpty() && modifyDisks.IsEmpty() && removeDisks.IsEmpty() {
log.Printf("No changes required for disks of storage controller '%s' in server '%s'.", controllerID, serverID)
return nil
}
// First remove any disks that are no longer required.
err = processRemoveStorageControllerDisks(removeDisks, data, providerState)
if err != nil {
return err
}
// Then modify existing disks
err = processModifyStorageControllerDisks(modifyDisks, data, providerState)
if err != nil {
return err
}
// Finally, add new disks
err = processAddStorageControllerDisks(addDisks, data, providerState)
if err != nil {
return err
}
return nil
}
// Process the collection of disks that need to be added to the server.
func processAddStorageControllerDisks(addDisks models.Disks, data *schema.ResourceData, providerState *providerState) error {
propertyHelper := propertyHelper(data)
controllerID := data.Id()
busNumber := data.Get(resourceKeyStorageControllerBusNumber).(int)
serverID := data.Get(resourceKeyStorageControllerServerID).(string)
apiClient := providerState.Client()
targetController, err := getStorageController(apiClient, data)
if err != nil {
return err
}
for index := range addDisks {
addDisk := &addDisks[index]
operationDescription := fmt.Sprintf("Add disk with SCSI unit ID %d to controller '%s' (for bus %d) in server '%s'",
addDisk.SCSIUnitID,
targetController.ID,
busNumber,
serverID,
)
err := providerState.RetryAction(operationDescription, func(context retry.Context) {
asyncLock := providerState.AcquireAsyncOperationLock(operationDescription)
defer asyncLock.Release()
var addDiskError error
addDisk.ID, addDiskError = apiClient.AddDiskToSCSIController(
targetController.ID,
addDisk.SCSIUnitID,
addDisk.SizeGB,
addDisk.Speed,
)
if compute.IsResourceBusyError(addDiskError) {
context.Retry()
} else if addDiskError != nil {
context.Fail(addDiskError)
}
})
if err != nil {
return err
}
log.Printf("Adding disk '%s' (%dGB, speed = '%s') with SCSI unit ID %d to controller '%s' (for bus %d) in server '%s'...",
addDisk.ID,
addDisk.SizeGB,
addDisk.Speed,
addDisk.SCSIUnitID,
targetController.ID,
busNumber,
serverID,
)
resource, err := apiClient.WaitForChange(
compute.ResourceTypeServer,
serverID,
fmt.Sprintf("Add disk %d:%d", addDisk.SCSIBusNumber, addDisk.SCSIUnitID),
resourceUpdateTimeoutServer,
)
if err != nil {
return err
}
server := resource.(*compute.Server)
targetController := server.SCSIControllers.GetByID(controllerID)
if targetController == nil {
return fmt.Errorf("cannot find controller '%s' in server '%s'", controllerID, serverID)
}
propertyHelper.SetDisks(
models.NewDisksFromVirtualMachineSCSIController(*targetController),
)
propertyHelper.SetPartial(resourceKeyStorageControllerDisk)
log.Printf("Server '%s' now has %d disks: %#v.", serverID, server.SCSIControllers.GetDiskCount(), server.SCSIControllers)
log.Printf("Added disk '%s' with SCSI unit ID %d to to controller '%s' (for bus %d) in server '%s'.",
addDisk.ID,
addDisk.SCSIUnitID,
targetController.ID,
busNumber,
serverID,
)
}
return nil
}
// Process the collection of disks whose configuration needs to be modified.
//
// Disk Ids must already be populated.
func processModifyStorageControllerDisks(modifyDisks models.Disks, data *schema.ResourceData, providerState *providerState) error {
propertyHelper := propertyHelper(data)
controllerID := data.Id()
serverID := data.Get(resourceKeyStorageControllerServerID).(string)
apiClient := providerState.Client()
targetController, err := getStorageController(apiClient, data)
if err != nil {
return err
}
actualDisks := models.NewDisksFromVirtualMachineSCSIController(*targetController)
actualDisksBySCSIPath := actualDisks.BySCSIPath()
for index := range modifyDisks {
modifyDisk := &modifyDisks[index]
actualDisk := actualDisksBySCSIPath[modifyDisk.SCSIPath()]
// Can't shrink disk, only grow it.
if modifyDisk.SizeGB < actualDisk.SizeGB {
return fmt.Errorf(
"cannot resize disk '%s' on storage controller '%s' (SCSI bus %d) in server '%s' from %d to GB to %d (for now, disks can only be expanded)",
modifyDisk.ID,
targetController.ID,
targetController.BusNumber,
serverID,
actualDisk.SizeGB,
modifyDisk.SizeGB,
)
}
// Do we need to expand the disk?
if modifyDisk.SizeGB > actualDisk.SizeGB {
log.Printf(
"Expanding disk '%s' on storage controller '%s' (SCSI bus %d) in server '%s' (from %d GB to %d GB)...",
modifyDisk.ID,
targetController.ID,
targetController.BusNumber,
serverID,
actualDisk.SizeGB,
modifyDisk.SizeGB,
)
operationDescription := fmt.Sprintf("Expand disk '%s' on storage controller '%s' (SCSI bus %d) in server '%s'",
modifyDisk.ID,
targetController.ID,
targetController.BusNumber,
serverID,
)
err = providerState.RetryAction(operationDescription, func(context retry.Context) {
asyncLock := providerState.AcquireAsyncOperationLock(operationDescription)
defer asyncLock.Release()
response, resizeError := apiClient.ExpandDisk(modifyDisk.ID, modifyDisk.SizeGB)
if compute.IsResourceBusyError(resizeError) {
context.Retry()
} else if resizeError != nil {
context.Fail(resizeError)
}
if response.ResponseCode != compute.ResponseCodeInProgress {
context.Fail(response.ToError("unexpected response code '%s' when expanding server disk '%s' for server '%s'",
response.ResponseCode,
modifyDisk.ID,
serverID,
))
}
})
if err != nil {
return err
}
log.Printf(
"Expand disk '%s' on storage controller '%s' (SCSI bus %d) in server '%s' (from %d to GB to %d)...",
modifyDisk.ID,
targetController.ID,
targetController.BusNumber,
serverID,
actualDisk.SizeGB,
modifyDisk.SizeGB,
)
resource, err := apiClient.WaitForChange(
compute.ResourceTypeServer,
serverID,
fmt.Sprintf("Expand disk %d:%d", modifyDisk.SCSIBusNumber, modifyDisk.SCSIUnitID),
resourceUpdateTimeoutServer,
)
if err != nil {
return err
}
modifyDisk.SizeGB = actualDisk.SizeGB
server := resource.(*compute.Server)
targetController := server.SCSIControllers.GetByID(controllerID)
if targetController == nil {
return fmt.Errorf("cannot find controller '%s' in server '%s'", controllerID, serverID)
}
propertyHelper.SetDisks(
models.NewDisksFromVirtualMachineSCSIController(*targetController),
)
propertyHelper.SetPartial(resourceKeyServerDisk)
log.Printf("storage controller '%s' now has %d disks: %#v.", targetController.ID, len(targetController.Disks), targetController)
log.Printf(
"Expanded disk '%s' on storage controller '%s' (SCSI bus %d) server '%s' (from %d to GB to %d).",
modifyDisk.ID,
targetController.ID,
targetController.BusNumber,
serverID,
actualDisk.SizeGB,
modifyDisk.SizeGB,
)
}
// Do we need to change the disk speed?
if modifyDisk.Speed != actualDisk.Speed {
log.Printf(
"Changing speed of disk '%s' on storage controller '%s' (SCSI bus %d) in server '%s' (from '%s' to '%s')...",
modifyDisk.ID,
targetController.ID,
targetController.BusNumber,
serverID,
actualDisk.Speed,
modifyDisk.Speed,
)
operationDescription := fmt.Sprintf("Change speed of disk '%s' on storage controller '%s' (SCSI bus %d) in server '%s' (from '%s' to '%s')",
modifyDisk.ID,
targetController.ID,
targetController.BusNumber,
serverID,
actualDisk.Speed,
modifyDisk.Speed,
)
err = providerState.RetryAction(operationDescription, func(context retry.Context) {
asyncLock := providerState.AcquireAsyncOperationLock(operationDescription)
defer asyncLock.Release()
response, resizeError := apiClient.ChangeServerDiskSpeed(serverID, modifyDisk.ID, modifyDisk.Speed)
if compute.IsResourceBusyError(resizeError) {
context.Retry()
} else if resizeError != nil {
context.Fail(resizeError)
}
if response.Result != compute.ResultSuccess {
context.Fail(response.ToError(
"Unexpected result '%s' when changing speed of disk '%s' on storage controller '%s' (SCSI bus %d) in server '%s'.",
response.Result,
modifyDisk.ID,
targetController.ID,
targetController.BusNumber,
serverID,
))
}
})
if err != nil {
return err
}
resource, err := apiClient.WaitForChange(
compute.ResourceTypeServer,
serverID,
fmt.Sprintf("Change speed of disk %d:%d", modifyDisk.SCSIBusNumber, modifyDisk.SCSIUnitID),
resourceUpdateTimeoutServer,
)
if err != nil {
return err
}
modifyDisk.Speed = actualDisk.Speed
server := resource.(*compute.Server)
targetController := server.SCSIControllers.GetByID(controllerID)
if targetController == nil {
return fmt.Errorf("cannot find controller '%s' in server '%s'", controllerID, serverID)
}
propertyHelper.SetDisks(
models.NewDisksFromVirtualMachineSCSIController(*targetController),
)
propertyHelper.SetPartial(resourceKeyServerDisk)
log.Printf("Changed speed of disk '%s' on storage controller '%s' (SCSI bus %d) in server '%s' (from '%s' to GB to '%s').",
modifyDisk.ID,
targetController.ID,
targetController.BusNumber,
serverID,
actualDisk.Speed,
modifyDisk.Speed,
)
}
}
return nil
}
// Process the collection of disks that need to be removed.
//
// Disk Ids must already be populated.
func processRemoveStorageControllerDisks(removeDisks models.Disks, data *schema.ResourceData, providerState *providerState) error {
propertyHelper := propertyHelper(data)
controllerID := data.Id()
serverID := data.Get(resourceKeyStorageControllerServerID).(string)
apiClient := providerState.Client()
targetController, err := getStorageController(apiClient, data)
if err != nil {
return err
}
for _, removeDisk := range removeDisks {
log.Printf("Remove disk '%s' (SCSI unit Id %d) from storage controller '%s' (SCSI bus %d) in server '%s'...",
removeDisk.ID,
removeDisk.SCSIUnitID,
targetController.ID,
targetController.BusNumber,
serverID,
)
removingDisk := true
operationDescription := fmt.Sprintf("Remove disk '%s' (SCSI unit Id %d) from storage controller '%s' (SCSI bus %d) in server '%s'",
removeDisk.ID,
removeDisk.SCSIUnitID,
targetController.ID,
targetController.BusNumber,
serverID,
)
err = providerState.RetryAction(operationDescription, func(context retry.Context) {
asyncLock := providerState.AcquireAsyncOperationLock(operationDescription)
defer asyncLock.Release()
removeError := apiClient.RemoveDisk(removeDisk.ID)
if compute.IsResourceBusyError(removeError) {
context.Retry()
} else if compute.IsExceedsLimitError(removeError) {
removingDisk = false // It's the server's last disk, so we won't actually perform the removal
} else if removeError != nil {
context.Fail(removeError)
}
})
if err != nil {
return err
}
var server *compute.Server
if removingDisk {
resource, err := apiClient.WaitForChange(
compute.ResourceTypeServer,
serverID,
"Remove disk",
resourceUpdateTimeoutServer,
)
if err != nil {
return err
}
server = resource.(*compute.Server)
} else {
log.Printf("Not removing disk '%s' (SCSI unit Id %d) from storage controller '%s' (SCSI bus %d) in server '%s' because this is the server's last disk.",
removeDisk.ID,
removeDisk.SCSIUnitID,
targetController.ID,
targetController.BusNumber,
serverID,
)
server, err = apiClient.GetServer(serverID)
if err != nil {
return err
}
if server == nil {
log.Printf("Cannot find server '%s'; will treat storage controller '%s' as deleted.", serverID, controllerID)
return nil
}
}
targetController := server.SCSIControllers.GetByID(controllerID)
if targetController == nil {
return fmt.Errorf("cannot find controller '%s' in server '%s'", controllerID, serverID)
}
propertyHelper.SetDisks(
models.NewDisksFromVirtualMachineSCSIController(*targetController),
)
propertyHelper.SetPartial(resourceKeyServerDisk)
log.Printf(
"Removed disk '%s' (SCSI unit Id %d) from storage controller '%s' (SCSI bus %d) in server '%s'.",
removeDisk.ID,
removeDisk.SCSIUnitID,
targetController.ID,
targetController.BusNumber,
serverID,
)
}
return nil
}
func validateStorageControllerDisks(disks models.Disks) error {
return validateDisks(disks)
}
func getStorageController(apiClient *compute.Client, data *schema.ResourceData) (*compute.VirtualMachineSCSIController, error) {
controllerID := data.Id()
serverStorageControllers, err := getServerStorageControllers(apiClient, data)
if err != nil {
return nil, err
}
targetController := serverStorageControllers.GetByID(controllerID)
if targetController == nil {
serverID := data.Get(resourceKeyStorageControllerServerID).(string)
return nil, fmt.Errorf("cannot find storage controller '%s' in server '%s'", controllerID, serverID)
}
return targetController, nil
}
func getServerStorageControllers(apiClient *compute.Client, data *schema.ResourceData) (compute.VirtualMachineSCSIControllers, error) {
serverID := data.Get(resourceKeyStorageControllerServerID).(string)
server, err := apiClient.GetServer(serverID)
if err != nil {
return nil, err
}
if server == nil {
data.SetId("")
return nil, fmt.Errorf("server '%s' has been deleted", serverID)
}
return server.SCSIControllers, nil
}