Skip to content

Commit 7f11d28

Browse files
Ability to add RDM Disk in a VM
1 parent d665797 commit 7f11d28

8 files changed

+707
-53
lines changed

vsphere/data_source_vsphere_vmfs_disks.go

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package vsphere
33
import (
44
"context"
55
"fmt"
6+
"github.com/hashicorp/terraform-provider-vsphere/vsphere/internal/helper/structure"
67
"regexp"
78
"sort"
89
"time"
@@ -40,6 +41,28 @@ func dataSourceVSphereVmfsDisks() *schema.Resource {
4041
Computed: true,
4142
Elem: &schema.Schema{Type: schema.TypeString},
4243
},
44+
"disks_info": {
45+
Type: schema.TypeList,
46+
Description: "The details of the disks discovered by the search.",
47+
Computed: true,
48+
Elem: &schema.Resource{Schema: map[string]*schema.Schema{
49+
"name": {
50+
Type: schema.TypeString,
51+
Computed: true,
52+
Description: "Display name of the disk",
53+
},
54+
"path": {
55+
Type: schema.TypeString,
56+
Computed: true,
57+
Description: "Path of the physical volume of the disk.",
58+
},
59+
"capacity_in_gb": {
60+
Type: schema.TypeInt,
61+
Computed: true,
62+
Description: "Capacity in GB.",
63+
},
64+
}},
65+
},
4366
},
4467
}
4568
}
@@ -70,19 +93,30 @@ func dataSourceVSphereVmfsDisksRead(d *schema.ResourceData, meta interface{}) er
7093
d.SetId(time.Now().UTC().String())
7194

7295
var disks []string
96+
var disksInfo []map[string]interface{}
7397
for _, sl := range hss.StorageDeviceInfo.ScsiLun {
7498
if hsd, ok := sl.(*types.HostScsiDisk); ok {
7599
if matched, _ := regexp.MatchString(d.Get("filter").(string), hsd.CanonicalName); matched {
100+
disk := make(map[string]interface{})
101+
disk["name"] = hsd.DisplayName
102+
disk["path"] = hsd.DevicePath
103+
block := hsd.Capacity.Block
104+
blockSize := int64(hsd.Capacity.BlockSize)
105+
disk["capacity_in_gb"] = structure.ByteToGiB(block * blockSize)
106+
disksInfo = append(disksInfo, disk)
76107
disks = append(disks, hsd.CanonicalName)
77108
}
78109
}
79110
}
80-
81111
sort.Strings(disks)
82112

83113
if err := d.Set("disks", disks); err != nil {
84114
return fmt.Errorf("error saving results to state: %s", err)
85115
}
86116

117+
if err := d.Set("disks_info", disksInfo); err != nil {
118+
return fmt.Errorf("error saving results to state: %s", err)
119+
}
120+
87121
return nil
88122
}

vsphere/data_source_vsphere_vmfs_disks_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ func TestAccDataSourceVSphereVmfsDisks_basic(t *testing.T) {
2525
testCheckOutputBool("found", "true"),
2626
),
2727
},
28+
{
29+
Config: testAccDataSourceVSphereVmfsDisksInfoConfig(),
30+
Check: resource.ComposeTestCheckFunc(
31+
testCheckOutputBool("found", "true"),
32+
),
33+
},
2834
},
2935
})
3036
}
@@ -78,3 +84,26 @@ output "found" {
7884
os.Getenv("TF_VAR_VSPHERE_ESXI1"),
7985
)
8086
}
87+
88+
func testAccDataSourceVSphereVmfsDisksInfoConfig() string {
89+
return fmt.Sprintf(`
90+
%s
91+
92+
data "vsphere_host" "esxi_host" {
93+
name = "%s"
94+
datacenter_id = "${data.vsphere_datacenter.rootdc1.id}"
95+
}
96+
97+
data "vsphere_vmfs_disks" "available" {
98+
host_system_id = "${data.vsphere_host.esxi_host.id}"
99+
rescan = true
100+
}
101+
102+
output "found" {
103+
value = "${length(data.vsphere_vmfs_disks.available.disks_info) >= 1 ? "true" : "false" }"
104+
}
105+
`,
106+
testhelper.CombineConfigs(testhelper.ConfigDataRootDC1(), testhelper.ConfigDataRootPortGroup1()),
107+
os.Getenv("TF_VAR_VSPHERE_ESXI1"),
108+
)
109+
}

vsphere/helper_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -383,9 +383,10 @@ func testRenameVMFirstDisk(s *terraform.State, resourceName string, new string)
383383
var dcSpec []types.BaseVirtualDeviceConfigSpec
384384
for _, d := range vprops.Config.Hardware.Device {
385385
if oldDisk, ok := d.(*types.VirtualDisk); ok {
386+
backing, _ := virtualdevice.GetBackingForDisk(oldDisk)
386387
newFileName, err := virtualdisk.Move(
387388
tVars.client,
388-
oldDisk.Backing.(*types.VirtualDiskFlatVer2BackingInfo).FileName,
389+
backing.GetFileName(),
389390
dc,
390391
new,
391392
nil,
@@ -399,9 +400,9 @@ func testRenameVMFirstDisk(s *terraform.State, resourceName string, new string)
399400
VirtualDeviceFileBackingInfo: types.VirtualDeviceFileBackingInfo{
400401
FileName: newFileName,
401402
},
402-
ThinProvisioned: oldDisk.Backing.(*types.VirtualDiskFlatVer2BackingInfo).ThinProvisioned,
403-
EagerlyScrub: oldDisk.Backing.(*types.VirtualDiskFlatVer2BackingInfo).EagerlyScrub,
404-
DiskMode: oldDisk.Backing.(*types.VirtualDiskFlatVer2BackingInfo).DiskMode,
403+
ThinProvisioned: backing.GetThinProvisioned(),
404+
EagerlyScrub: backing.GetEagerlyScrub(),
405+
DiskMode: backing.GetDiskMode(),
405406
},
406407
},
407408
}

0 commit comments

Comments
 (0)