-
Notifications
You must be signed in to change notification settings - Fork 13
/
resource_server_disks.go
535 lines (449 loc) · 14.6 KB
/
resource_server_disks.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
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 (
resourceKeyServerDisk = "disk"
resourceKeyServerDiskID = "id"
resourceKeyServerDiskUnitID = "scsi_unit_id"
resourceKeyServerDiskSizeGB = "size_gb"
resourceKeyServerDiskSpeed = "speed"
)
func schemaDisk() *schema.Schema {
return &schema.Schema{
Type: schema.TypeList,
Optional: true,
Computed: true,
Default: nil,
Description: "The set of virtual disks attached to the server",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
resourceKeyServerDiskID: &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The CloudControl identifier for the virtual disk (computed when the disk is first created)",
},
resourceKeyServerDiskUnitID: &schema.Schema{
Type: schema.TypeInt,
Required: true,
Description: "The SCSI Logical Unit Number (LUN) for the disk",
},
resourceKeyServerDiskSizeGB: &schema.Schema{
Type: schema.TypeInt,
Required: true,
Description: "The size (in GB) of the disk",
},
resourceKeyServerDiskSpeed: &schema.Schema{
Type: schema.TypeString,
Optional: true,
Default: "STANDARD",
StateFunc: normalizeSpeed,
Description: "The disk speed",
},
},
},
}
}
// When creating a server resource, synchronise the server's disks with its resource data.
// imageDisks refers to the newly-deployed server's collection of disks (i.e. image disks).
func createDisks(imageDisks []compute.VirtualMachineDisk, data *schema.ResourceData, providerState *providerState) error {
propertyHelper := propertyHelper(data)
serverID := data.Id()
log.Printf("Configuring image disks for server '%s'...", serverID)
// Since this is the first time, populate image disks.
configuredDisks := propertyHelper.GetDisks()
log.Printf("Configuration for server '%s' specifies %d disks: %#v.", serverID, len(configuredDisks), configuredDisks)
actualDisks := models.NewDisksFromVirtualMachineDisks(imageDisks)
configuredDisks.CaptureIDs(actualDisks)
err := validateDisks(configuredDisks)
if err != nil {
return err
}
log.Printf("Configuration for server '%s' specifies %d disks: %#v.", serverID, len(configuredDisks), configuredDisks)
if len(configuredDisks) == 0 {
propertyHelper.SetDisks(actualDisks)
propertyHelper.SetPartial(resourceKeyServerDisk)
log.Printf("Server '%s' now has %d disks: %#v.", serverID, len(actualDisks), actualDisks)
return nil
}
propertyHelper.SetDisks(configuredDisks)
propertyHelper.SetPartial(resourceKeyServerDisk)
apiClient := providerState.Client()
server, err := apiClient.GetServer(serverID)
if err != nil {
return err
}
if server == nil {
return fmt.Errorf("Cannot find server with Id '%s'", serverID)
}
// After initial server deployment, we only need to handle disks that were part of the original server image (and of those, only ones we need to modify after the initial deployment completed deployment).
log.Printf("Configure image disks for server '%s'...", serverID)
actualDisks = models.NewDisksFromVirtualMachineDisks(server.Disks)
addDisks, modifyDisks, _ := configuredDisks.SplitByAction(actualDisks) // Ignore removeDisks since not all disks have been created yet
if addDisks.IsEmpty() && modifyDisks.IsEmpty() {
log.Printf("No post-deploy changes required for disks of server '%s'.", serverID)
return nil
}
err = processModifyDisks(modifyDisks, data, providerState)
if err != nil {
return err
}
log.Printf("Configure additional disks for server '%s'...", serverID)
err = processAddDisks(addDisks, data, providerState)
if err != nil {
return err
}
return nil
}
// When updating a server resource, synchronise the server's image disk attributes with its resource data
// Removes image disks from existingDisksByUnitID as they are processed, leaving only additional disks.
func updateDisks(data *schema.ResourceData, providerState *providerState) error {
propertyHelper := propertyHelper(data)
serverID := data.Id()
log.Printf("Configure image disks for server '%s'...", 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)
}
actualDisks := models.NewDisksFromVirtualMachineDisks(server.Disks)
configuredDisks := propertyHelper.GetDisks()
log.Printf("Configuration for server '%s' specifies %d disks: %#v.", serverID, len(configuredDisks), configuredDisks)
err = validateDisks(configuredDisks)
if err != nil {
return err
}
if configuredDisks.IsEmpty() {
// No explicitly-configured disks.
propertyHelper.SetDisks(
models.NewDisksFromVirtualMachineDisks(server.Disks),
)
propertyHelper.SetPartial(resourceKeyServerDisk)
log.Printf("Server '%s' now has %d disks: %#v.", serverID, len(server.Disks), server.Disks)
return nil
}
addDisks, modifyDisks, removeDisks := configuredDisks.SplitByAction(actualDisks)
if addDisks.IsEmpty() && modifyDisks.IsEmpty() && removeDisks.IsEmpty() {
log.Printf("No post-deploy changes required for disks of server '%s'.", serverID)
return nil
}
// First remove any disks that are no longer required.
err = processRemoveDisks(removeDisks, data, providerState)
if err != nil {
return err
}
// Then modify existing disks
err = processModifyDisks(modifyDisks, data, providerState)
if err != nil {
return err
}
// Finally, add new disks
err = processAddDisks(addDisks, data, providerState)
if err != nil {
return err
}
return nil
}
// Process the collection of disks that need to be added to the server.
func processAddDisks(addDisks models.Disks, data *schema.ResourceData, providerState *providerState) error {
propertyHelper := propertyHelper(data)
serverID := data.Id()
apiClient := providerState.Client()
for index := range addDisks {
addDisk := &addDisks[index]
operationDescription := fmt.Sprintf("Add disk with SCSI unit ID %d to server '%s'",
addDisk.SCSIUnitID,
serverID,
)
err := providerState.RetryAction(operationDescription, func(context retry.Context) {
asyncLock := providerState.AcquireAsyncOperationLock(operationDescription)
defer asyncLock.Release()
var addDiskError error
addDisk.ID, addDiskError = apiClient.AddDiskToServer(
serverID,
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 server '%s'...",
addDisk.ID,
addDisk.SizeGB,
addDisk.Speed,
addDisk.SCSIUnitID,
serverID,
)
resource, err := apiClient.WaitForChange(
compute.ResourceTypeServer,
serverID,
"Add disk",
resourceUpdateTimeoutServer,
)
if err != nil {
return err
}
server := resource.(*compute.Server)
propertyHelper.SetDisks(
models.NewDisksFromVirtualMachineDisks(server.Disks),
)
propertyHelper.SetPartial(resourceKeyServerDisk)
log.Printf("Server '%s' now has %d disks: %#v.", serverID, len(server.Disks), server.Disks)
log.Printf("Added disk '%s' with SCSI unit ID %d to server '%s'.",
addDisk.ID,
addDisk.SCSIUnitID,
serverID,
)
}
return nil
}
// Process the collection of disks whose configuration needs to be modified.
//
// Disk Ids must already be populated.
func processModifyDisks(modifyDisks models.Disks, data *schema.ResourceData, providerState *providerState) error {
log.Printf("modifyDisks = %#v", modifyDisks)
propertyHelper := propertyHelper(data)
serverID := data.Id()
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)
}
actualDisks := models.NewDisksFromVirtualMachineDisks(server.Disks)
actualDisksByUnitID := actualDisks.ByUnitID()
for index := range modifyDisks {
modifyDisk := &modifyDisks[index]
log.Printf("modifyDisk = %#v", modifyDisk)
actualImageDisk := actualDisksByUnitID[modifyDisk.SCSIUnitID]
// Can't shrink disk, only grow it.
if modifyDisk.SizeGB < actualImageDisk.SizeGB {
return fmt.Errorf(
"Cannot resize disk '%s' in server '%s' from %d to GB to %d (for now, disks can only be expanded).",
modifyDisk.ID,
serverID,
actualImageDisk.SizeGB,
modifyDisk.SizeGB,
)
}
// Do we need to expand the disk?
if modifyDisk.SizeGB > actualImageDisk.SizeGB {
log.Printf(
"Expanding disk '%s' in server '%s' (from %d GB to %d GB)...",
modifyDisk.ID,
serverID,
actualImageDisk.SizeGB,
modifyDisk.SizeGB,
)
operationDescription := fmt.Sprintf("Expand disk '%s' in server '%s'", modifyDisk.ID, serverID)
err = providerState.RetryAction(operationDescription, func(context retry.Context) {
asyncLock := providerState.AcquireAsyncOperationLock(operationDescription)
defer asyncLock.Release()
response, resizeError := apiClient.ResizeServerDisk(serverID, modifyDisk.ID, modifyDisk.SizeGB)
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 resizing server disk '%s' for server '%s'.",
response.Result,
modifyDisk.ID,
serverID,
))
}
})
if err != nil {
return err
}
log.Printf(
"Resizing disk '%s' for server '%s' (from %d to GB to %d)...",
modifyDisk.ID,
serverID,
actualImageDisk.SizeGB,
modifyDisk.SizeGB,
)
resource, err := apiClient.WaitForChange(
compute.ResourceTypeServer,
serverID,
"Resize disk",
resourceUpdateTimeoutServer,
)
if err != nil {
return err
}
modifyDisk.SizeGB = actualImageDisk.SizeGB
server := resource.(*compute.Server)
propertyHelper.SetDisks(
models.NewDisksFromVirtualMachineDisks(server.Disks),
)
propertyHelper.SetPartial(resourceKeyServerDisk)
log.Printf("Server '%s' now has %d disks: %#v.", serverID, len(server.Disks), server.Disks)
log.Printf(
"Resized disk '%s' for server '%s' (from %d to GB to %d).",
modifyDisk.ID,
serverID,
actualImageDisk.SizeGB,
modifyDisk.SizeGB,
)
}
// Do we need to change the disk speed?
if modifyDisk.Speed != actualImageDisk.Speed {
log.Printf(
"Changing speed of disk '%s' in server '%s' (from '%s' to '%s')...",
modifyDisk.ID,
serverID,
actualImageDisk.Speed,
modifyDisk.Speed,
)
operationDescription := fmt.Sprintf("Change speed of disk '%s' in server '%s'", modifyDisk.ID, serverID)
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 resizing server disk '%s' for server '%s'.",
response.Result,
modifyDisk.ID,
serverID,
))
}
})
if err != nil {
return err
}
resource, err := apiClient.WaitForChange(
compute.ResourceTypeServer,
serverID,
"Resize disk",
resourceUpdateTimeoutServer,
)
if err != nil {
return err
}
modifyDisk.Speed = actualImageDisk.Speed
server = resource.(*compute.Server)
propertyHelper.SetDisks(
models.NewDisksFromVirtualMachineDisks(server.Disks),
)
propertyHelper.SetPartial(resourceKeyServerDisk)
log.Printf(
"Resized disk '%s' for server '%s' (from %d to GB to %d).",
modifyDisk.ID,
serverID,
actualImageDisk.SizeGB,
modifyDisk.SizeGB,
)
}
}
return nil
}
// Process the collection of disks that need to be removed.
//
// Disk Ids must already be populated.
func processRemoveDisks(removeDisks models.Disks, data *schema.ResourceData, providerState *providerState) error {
propertyHelper := propertyHelper(data)
serverID := data.Id()
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)
}
for _, removeDisk := range removeDisks {
log.Printf("Remove disk '%s' (SCSI unit Id %d) from server '%s'...",
removeDisk.ID,
removeDisk.SCSIUnitID,
serverID,
)
operationDescription := fmt.Sprintf("Remove disk '%s' from server '%s'", removeDisk.ID, serverID)
err = providerState.RetryAction(operationDescription, func(context retry.Context) {
asyncLock := providerState.AcquireAsyncOperationLock(operationDescription)
defer asyncLock.Release()
removeError := apiClient.RemoveDiskFromServer(removeDisk.ID)
if compute.IsResourceBusyError(removeError) {
context.Retry()
} else if removeError != nil {
context.Fail(removeError)
}
})
if err != nil {
return err
}
resource, err := apiClient.WaitForChange(
compute.ResourceTypeServer,
serverID,
"Remove disk",
resourceUpdateTimeoutServer,
)
if err != nil {
return err
}
server := resource.(*compute.Server)
propertyHelper.SetDisks(
models.NewDisksFromVirtualMachineDisks(server.Disks),
)
propertyHelper.SetPartial(resourceKeyServerDisk)
log.Printf(
"Removed disk '%s' from server '%s'.",
removeDisk.ID,
serverID,
)
}
return nil
}
func hashDiskUnitID(item interface{}) int {
disk, ok := item.(models.Disk)
if ok {
return disk.SCSIUnitID
}
virtualMachineDisk, ok := item.(compute.VirtualMachineDisk)
if ok {
return virtualMachineDisk.SCSIUnitID
}
diskData := item.(map[string]interface{})
return diskData[resourceKeyServerDiskUnitID].(int)
}
func validateDisks(disks models.Disks) error {
if disks.IsEmpty() {
return nil
}
disksByUnitID := make(map[int]models.Disk)
for _, disk := range disks {
_, duplicate := disksByUnitID[disk.SCSIUnitID]
if duplicate {
return fmt.Errorf("Multiple disks with SCSI unit ID '%d'", disk.SCSIUnitID)
}
disksByUnitID[disk.SCSIUnitID] = disk
}
return nil
}