Skip to content

Commit 1901ef8

Browse files
sumitAgrawal007tenthirtyam
authored andcommitted
feat: add support for rdm disks
1 parent dea5add commit 1901ef8

7 files changed

+690
-46
lines changed

docs/data-sources/vmfs_disks.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,8 @@ the output `disks` attribute below, which is lexicographically sorted.
5858

5959
* `disks` - A lexicographically sorted list of devices discovered by the
6060
operation, matching the supplied `filter`, if provided.
61+
62+
* `disk_details` - List of disks discovered by the operation with more details about them. The order matches that of `disks`
63+
* `display_name` - Display name of the disk
64+
* `device_path` - Path of the physical volume of the disk.
65+
* `capacity_gb` - Capacity of the disk in GiB.

vsphere/data_source_vsphere_vmfs_disks.go

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
"sort"
1212
"time"
1313

14+
"github.com/hashicorp/terraform-provider-vsphere/vsphere/internal/helper/structure"
15+
1416
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1517
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
1618
"github.com/vmware/govmomi/vim25/mo"
@@ -40,10 +42,32 @@ func dataSourceVSphereVmfsDisks() *schema.Resource {
4042
},
4143
"disks": {
4244
Type: schema.TypeList,
43-
Description: "The names of the disks discovered by the search.",
45+
Description: "The canonical names of the disks discovered by the search.",
4446
Computed: true,
4547
Elem: &schema.Schema{Type: schema.TypeString},
4648
},
49+
"disk_details": {
50+
Type: schema.TypeList,
51+
Description: "The details of the disks discovered by the search.",
52+
Computed: true,
53+
Elem: &schema.Resource{Schema: map[string]*schema.Schema{
54+
"display_name": {
55+
Type: schema.TypeString,
56+
Computed: true,
57+
Description: "Display name of the disk.",
58+
},
59+
"device_path": {
60+
Type: schema.TypeString,
61+
Computed: true,
62+
Description: "Path of the physical volume of the disk.",
63+
},
64+
"capacity_gb": {
65+
Type: schema.TypeInt,
66+
Computed: true,
67+
Description: "Capacity of the disk in GiB.",
68+
},
69+
}},
70+
},
4771
},
4872
}
4973
}
@@ -74,19 +98,35 @@ func dataSourceVSphereVmfsDisksRead(d *schema.ResourceData, meta interface{}) er
7498
d.SetId(time.Now().UTC().String())
7599

76100
var disks []string
101+
diskDetailsMap := make(map[string]map[string]interface{})
77102
for _, sl := range hss.StorageDeviceInfo.ScsiLun {
78103
if hsd, ok := sl.(*types.HostScsiDisk); ok {
79104
if matched, _ := regexp.MatchString(d.Get("filter").(string), hsd.CanonicalName); matched {
105+
disk := make(map[string]interface{})
106+
disk["display_name"] = hsd.DisplayName
107+
disk["device_path"] = hsd.DevicePath
108+
block := hsd.Capacity.Block
109+
blockSize := int64(hsd.Capacity.BlockSize)
110+
disk["capacity_gb"] = structure.ByteToGiB(block * blockSize)
80111
disks = append(disks, hsd.CanonicalName)
112+
diskDetailsMap[hsd.CanonicalName] = disk
81113
}
82114
}
83115
}
84-
85116
sort.Strings(disks)
117+
// use the now sorted name list to create a matching order details list
118+
diskDetails := make([]map[string]interface{}, len(disks))
119+
for i, name := range disks {
120+
diskDetails[i] = diskDetailsMap[name]
121+
}
86122

87123
if err := d.Set("disks", disks); err != nil {
88124
return fmt.Errorf("error saving results to state: %s", err)
89125
}
90126

127+
if err := d.Set("disk_details", diskDetails); err != nil {
128+
return fmt.Errorf("error saving results to state: %s", err)
129+
}
130+
91131
return nil
92132
}

vsphere/data_source_vsphere_vmfs_disks_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ func TestAccDataSourceVSphereVmfsDisks_basic(t *testing.T) {
2929
testCheckOutputBool("found", "true"),
3030
),
3131
},
32+
{
33+
Config: testAccDataSourceVSphereVmfsDisksInfoConfig(),
34+
Check: resource.ComposeTestCheckFunc(
35+
testCheckOutputBool("found", "true"),
36+
),
37+
},
3238
},
3339
})
3440
}
@@ -82,3 +88,26 @@ output "found" {
8288
os.Getenv("TF_VAR_VSPHERE_ESXI1"),
8389
)
8490
}
91+
92+
func testAccDataSourceVSphereVmfsDisksInfoConfig() string {
93+
return fmt.Sprintf(`
94+
%s
95+
96+
data "vsphere_host" "esxi_host" {
97+
name = "%s"
98+
datacenter_id = "${data.vsphere_datacenter.rootdc1.id}"
99+
}
100+
101+
data "vsphere_vmfs_disks" "available" {
102+
host_system_id = "${data.vsphere_host.esxi_host.id}"
103+
rescan = true
104+
}
105+
106+
output "found" {
107+
value = "${length(data.vsphere_vmfs_disks.available.disk_details) >= 1 ? "true" : "false" }"
108+
}
109+
`,
110+
testhelper.CombineConfigs(testhelper.ConfigDataRootDC1(), testhelper.ConfigDataRootPortGroup1()),
111+
os.Getenv("TF_VAR_VSPHERE_ESXI1"),
112+
)
113+
}

vsphere/helper_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -393,9 +393,10 @@ func testRenameVMFirstDisk(s *terraform.State, resourceName string, newDiskName
393393
var dcSpec []types.BaseVirtualDeviceConfigSpec
394394
for _, d := range vprops.Config.Hardware.Device {
395395
if oldDisk, ok := d.(*types.VirtualDisk); ok {
396+
backing, _ := virtualdevice.GetBackingForDisk(oldDisk)
396397
newFileName, err := virtualdisk.Move(
397398
tVars.client,
398-
oldDisk.Backing.(*types.VirtualDiskFlatVer2BackingInfo).FileName,
399+
backing.GetFileName(),
399400
dc,
400401
newDiskName,
401402
nil,
@@ -409,9 +410,9 @@ func testRenameVMFirstDisk(s *terraform.State, resourceName string, newDiskName
409410
VirtualDeviceFileBackingInfo: types.VirtualDeviceFileBackingInfo{
410411
FileName: newFileName,
411412
},
412-
ThinProvisioned: oldDisk.Backing.(*types.VirtualDiskFlatVer2BackingInfo).ThinProvisioned,
413-
EagerlyScrub: oldDisk.Backing.(*types.VirtualDiskFlatVer2BackingInfo).EagerlyScrub,
414-
DiskMode: oldDisk.Backing.(*types.VirtualDiskFlatVer2BackingInfo).DiskMode,
413+
ThinProvisioned: backing.GetThinProvisioned(),
414+
EagerlyScrub: backing.GetEagerlyScrub(),
415+
DiskMode: backing.GetDiskMode(),
415416
},
416417
},
417418
}

0 commit comments

Comments
 (0)