@@ -28,8 +28,8 @@ func resourceCloudStackDisk() *schema.Resource {
28
28
Default : false ,
29
29
},
30
30
31
- "device " : & schema.Schema {
32
- Type : schema .TypeString ,
31
+ "device_id " : & schema.Schema {
32
+ Type : schema .TypeInt ,
33
33
Optional : true ,
34
34
Computed : true ,
35
35
},
@@ -59,6 +59,7 @@ func resourceCloudStackDisk() *schema.Resource {
59
59
"project" : & schema.Schema {
60
60
Type : schema .TypeString ,
61
61
Optional : true ,
62
+ Computed : true ,
62
63
ForceNew : true ,
63
64
},
64
65
@@ -116,7 +117,7 @@ func resourceCloudStackDiskCreate(d *schema.ResourceData, meta interface{}) erro
116
117
// Set the volume ID and partials
117
118
d .SetId (r .Id )
118
119
d .SetPartial ("name" )
119
- d .SetPartial ("device " )
120
+ d .SetPartial ("device_id " )
120
121
d .SetPartial ("disk_offering" )
121
122
d .SetPartial ("size" )
122
123
d .SetPartial ("virtual_machine_id" )
@@ -163,28 +164,7 @@ func resourceCloudStackDiskRead(d *schema.ResourceData, meta interface{}) error
163
164
setValueOrID (d , "zone" , v .Zonename , v .Zoneid )
164
165
165
166
if v .Attached != "" {
166
- // Get the virtual machine details
167
- vm , _ , err := cs .VirtualMachine .GetVirtualMachineByID (
168
- v .Virtualmachineid ,
169
- cloudstack .WithProject (d .Get ("project" ).(string )),
170
- )
171
- if err != nil {
172
- return err
173
- }
174
-
175
- // Get the guest OS type details
176
- os , _ , err := cs .GuestOS .GetOsTypeByID (vm .Guestosid )
177
- if err != nil {
178
- return err
179
- }
180
-
181
- // Get the guest OS category details
182
- c , _ , err := cs .GuestOS .GetOsCategoryByID (os .Oscategoryid )
183
- if err != nil {
184
- return err
185
- }
186
-
187
- d .Set ("device" , retrieveDeviceName (v .Deviceid , c .Name ))
167
+ d .Set ("device_id" , int (v .Deviceid ))
188
168
d .Set ("virtual_machine_id" , v .Virtualmachineid )
189
169
}
190
170
@@ -235,9 +215,9 @@ func resourceCloudStackDiskUpdate(d *schema.ResourceData, meta interface{}) erro
235
215
d .SetPartial ("size" )
236
216
}
237
217
238
- // If the device changed, just detach here so we can re-attach the
218
+ // If the device ID changed, just detach here so we can re-attach the
239
219
// volume at the end of this function
240
- if d .HasChange ("device " ) || d .HasChange ("virtual_machine" ) {
220
+ if d .HasChange ("device_id " ) || d .HasChange ("virtual_machine" ) {
241
221
// Detach the volume
242
222
if err := resourceCloudStackDiskDetach (d , meta ); err != nil {
243
223
return fmt .Errorf ("Error detaching disk %s from virtual machine: %s" , name , err )
@@ -253,7 +233,7 @@ func resourceCloudStackDiskUpdate(d *schema.ResourceData, meta interface{}) erro
253
233
254
234
// Set the additional partials
255
235
d .SetPartial ("attach" )
256
- d .SetPartial ("device " )
236
+ d .SetPartial ("device_id " )
257
237
d .SetPartial ("virtual_machine_id" )
258
238
} else {
259
239
// Detach the volume
@@ -304,21 +284,14 @@ func resourceCloudStackDiskAttach(d *schema.ResourceData, meta interface{}) erro
304
284
// Create a new parameter struct
305
285
p := cs .Volume .NewAttachVolumeParams (d .Id (), virtualmachineid .(string ))
306
286
307
- if device , ok := d .GetOk ("device" ); ok {
308
- // Retrieve the device ID
309
- deviceid := retrieveDeviceID (device .(string ))
310
- if deviceid == - 1 {
311
- return fmt .Errorf ("Device %s is not a valid device" , device .(string ))
312
- }
313
-
314
- // Set the device ID
315
- p .SetDeviceid (deviceid )
287
+ if deviceid , ok := d .GetOk ("device_id" ); ok {
288
+ p .SetDeviceid (int64 (deviceid .(int )))
316
289
}
317
290
318
291
// Attach the new volume
319
- r , err := Retry (4 , retryableAttachVolumeFunc (cs , p ))
292
+ r , err := Retry (10 , retryableAttachVolumeFunc (cs , p ))
320
293
if err != nil {
321
- return err
294
+ return fmt . Errorf ( "Error attaching volume to VM: %s" , err )
322
295
}
323
296
324
297
d .SetId (r .(* cloudstack.AttachVolumeResponse ).Id )
@@ -397,115 +370,3 @@ func retryableAttachVolumeFunc(
397
370
return r , nil
398
371
}
399
372
}
400
-
401
- func retrieveDeviceID (device string ) int64 {
402
- switch device {
403
- case "/dev/xvdb" , "D:" :
404
- return 1
405
- case "/dev/xvdc" , "E:" :
406
- return 2
407
- case "/dev/xvde" , "F:" :
408
- return 4
409
- case "/dev/xvdf" , "G:" :
410
- return 5
411
- case "/dev/xvdg" , "H:" :
412
- return 6
413
- case "/dev/xvdh" , "I:" :
414
- return 7
415
- case "/dev/xvdi" , "J:" :
416
- return 8
417
- case "/dev/xvdj" , "K:" :
418
- return 9
419
- case "/dev/xvdk" , "L:" :
420
- return 10
421
- case "/dev/xvdl" , "M:" :
422
- return 11
423
- case "/dev/xvdm" , "N:" :
424
- return 12
425
- case "/dev/xvdn" , "O:" :
426
- return 13
427
- case "/dev/xvdo" , "P:" :
428
- return 14
429
- case "/dev/xvdp" , "Q:" :
430
- return 15
431
- default :
432
- return - 1
433
- }
434
- }
435
-
436
- func retrieveDeviceName (device int64 , os string ) string {
437
- switch device {
438
- case 1 :
439
- if os == "Windows" {
440
- return "D:"
441
- }
442
- return "/dev/xvdb"
443
- case 2 :
444
- if os == "Windows" {
445
- return "E:"
446
- }
447
- return "/dev/xvdc"
448
- case 4 :
449
- if os == "Windows" {
450
- return "F:"
451
- }
452
- return "/dev/xvde"
453
- case 5 :
454
- if os == "Windows" {
455
- return "G:"
456
- }
457
- return "/dev/xvdf"
458
- case 6 :
459
- if os == "Windows" {
460
- return "H:"
461
- }
462
- return "/dev/xvdg"
463
- case 7 :
464
- if os == "Windows" {
465
- return "I:"
466
- }
467
- return "/dev/xvdh"
468
- case 8 :
469
- if os == "Windows" {
470
- return "J:"
471
- }
472
- return "/dev/xvdi"
473
- case 9 :
474
- if os == "Windows" {
475
- return "K:"
476
- }
477
- return "/dev/xvdj"
478
- case 10 :
479
- if os == "Windows" {
480
- return "L:"
481
- }
482
- return "/dev/xvdk"
483
- case 11 :
484
- if os == "Windows" {
485
- return "M:"
486
- }
487
- return "/dev/xvdl"
488
- case 12 :
489
- if os == "Windows" {
490
- return "N:"
491
- }
492
- return "/dev/xvdm"
493
- case 13 :
494
- if os == "Windows" {
495
- return "O:"
496
- }
497
- return "/dev/xvdn"
498
- case 14 :
499
- if os == "Windows" {
500
- return "P:"
501
- }
502
- return "/dev/xvdo"
503
- case 15 :
504
- if os == "Windows" {
505
- return "Q:"
506
- }
507
- return "/dev/xvdp"
508
- default :
509
- return "unknown"
510
- }
511
- }
0 commit comments