@@ -1436,10 +1436,48 @@ func (r *DiskSubresource) Read(l object.VirtualDeviceList) error {
1436
1436
attach = r .Get ("attach" ).(bool )
1437
1437
}
1438
1438
// Save disk backing settings
1439
- b , ok := disk .Backing .(* types.VirtualDiskFlatVer2BackingInfo )
1440
- if ! ok {
1439
+ if b , ok := disk .Backing .(* types.VirtualDiskFlatVer2BackingInfo ); ok {
1440
+ if err := r .setFlatBackingProperties (b , disk , attach ); err != nil {
1441
+ return err
1442
+ }
1443
+ } else if b , ok := disk .Backing .(* types.VirtualDiskSparseVer2BackingInfo ); ok {
1444
+ if err := r .setSparseBackingProperties (b , disk , attach ); err != nil {
1445
+ return err
1446
+ }
1447
+ } else {
1441
1448
return fmt .Errorf ("disk backing at %s is of an unsupported type (type %T)" , r .Get ("device_address" ).(string ), disk .Backing )
1442
1449
}
1450
+
1451
+ if allocation := disk .StorageIOAllocation ; allocation != nil {
1452
+ r .Set ("io_limit" , allocation .Limit )
1453
+ r .Set ("io_reservation" , allocation .Reservation )
1454
+ if shares := allocation .Shares ; shares != nil {
1455
+ r .Set ("io_share_level" , string (shares .Level ))
1456
+ r .Set ("io_share_count" , shares .Shares )
1457
+ }
1458
+ }
1459
+
1460
+ if spbm .IsSupported (r .client ) {
1461
+ // Set storage policy if the VM exists.
1462
+ vmUUID := r .rdd .Id ()
1463
+ if vmUUID != "" {
1464
+ result , err := virtualmachine .MOIDForUUID (r .client , vmUUID )
1465
+ if err != nil {
1466
+ return err
1467
+ }
1468
+ polID , err := spbm .PolicyIDByVirtualDisk (r .client , result .MOID , r .Get ("key" ).(int ))
1469
+ if err != nil {
1470
+ return err
1471
+ }
1472
+ r .Set ("storage_policy_id" , polID )
1473
+ }
1474
+ }
1475
+
1476
+ log .Printf ("[DEBUG] %s: Read finished (key and device address may have changed)" , r )
1477
+ return nil
1478
+ }
1479
+
1480
+ func (r * DiskSubresource ) setFlatBackingProperties (b * types.VirtualDiskFlatVer2BackingInfo , disk * types.VirtualDisk , attach bool ) error {
1443
1481
r .Set ("uuid" , b .Uuid )
1444
1482
r .Set ("disk_mode" , b .DiskMode )
1445
1483
r .Set ("write_through" , b .WriteThrough )
@@ -1469,29 +1507,24 @@ func (r *DiskSubresource) Read(l object.VirtualDeviceList) error {
1469
1507
r .Set ("size" , diskCapacityInGiB (disk ))
1470
1508
}
1471
1509
1472
- if allocation := disk .StorageIOAllocation ; allocation != nil {
1473
- r .Set ("io_limit" , allocation .Limit )
1474
- r .Set ("io_reservation" , allocation .Reservation )
1475
- if shares := allocation .Shares ; shares != nil {
1476
- r .Set ("io_share_level" , string (shares .Level ))
1477
- r .Set ("io_share_count" , shares .Shares )
1478
- }
1479
- }
1510
+ return nil
1511
+ }
1480
1512
1481
- if spbm .IsSupported (r .client ) {
1482
- // Set storage policy if the VM exists.
1483
- vmUUID := r .rdd .Id ()
1484
- if vmUUID != "" {
1485
- result , err := virtualmachine .MOIDForUUID (r .client , vmUUID )
1486
- if err != nil {
1487
- return err
1488
- }
1489
- polID , err := spbm .PolicyIDByVirtualDisk (r .client , result .MOID , r .Get ("key" ).(int ))
1490
- if err != nil {
1491
- return err
1492
- }
1493
- r .Set ("storage_policy_id" , polID )
1513
+ func (r * DiskSubresource ) setSparseBackingProperties (b * types.VirtualDiskSparseVer2BackingInfo , disk * types.VirtualDisk , attach bool ) error {
1514
+ r .Set ("uuid" , b .Uuid )
1515
+ r .Set ("disk_mode" , b .DiskMode )
1516
+ r .Set ("write_through" , b .WriteThrough )
1517
+
1518
+ r .Set ("datastore_id" , b .Datastore .Value )
1519
+
1520
+ // Disk settings
1521
+ if ! attach {
1522
+ dp := & object.DatastorePath {}
1523
+ if ok := dp .FromString (b .FileName ); ! ok {
1524
+ return fmt .Errorf ("could not parse path from filename: %s" , b .FileName )
1494
1525
}
1526
+ r .Set ("path" , dp .Path )
1527
+ r .Set ("size" , diskCapacityInGiB (disk ))
1495
1528
}
1496
1529
1497
1530
log .Printf ("[DEBUG] %s: Read finished (key and device address may have changed)" , r )
@@ -2364,14 +2397,15 @@ func diskUUIDMatch(device types.BaseVirtualDevice, uuid string) bool {
2364
2397
if ! ok {
2365
2398
return false
2366
2399
}
2367
- backing , ok := disk . Backing .( * types. VirtualDiskFlatVer2BackingInfo )
2368
- if ! ok {
2369
- return false
2400
+
2401
+ if backing , ok := disk . Backing .( * types. VirtualDiskFlatVer2BackingInfo ); ok {
2402
+ return backing . Uuid == uuid
2370
2403
}
2371
- if backing . Uuid != uuid {
2372
- return false
2404
+ if backing , ok := disk . Backing .( * types. VirtualDiskSparseVer2BackingInfo ); ok {
2405
+ return backing . Uuid == uuid
2373
2406
}
2374
- return true
2407
+
2408
+ return false
2375
2409
}
2376
2410
2377
2411
// diskCapacityInGiB reports the supplied disk's capacity, by first checking
0 commit comments