Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add support for RDM disks #1366

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 42 additions & 2 deletions vsphere/data_source_vsphere_vmfs_disks.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"sort"
"time"

"github.com/hashicorp/terraform-provider-vsphere/vsphere/internal/helper/structure"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/vmware/govmomi/vim25/mo"
Expand Down Expand Up @@ -36,10 +38,32 @@ func dataSourceVSphereVmfsDisks() *schema.Resource {
},
"disks": {
Type: schema.TypeList,
Description: "The names of the disks discovered by the search.",
Description: "The canonical names of the disks discovered by the search.",
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"disk_details": {
Type: schema.TypeList,
Description: "The details of the disks discovered by the search.",
Computed: true,
Elem: &schema.Resource{Schema: map[string]*schema.Schema{
"display_name": {
Type: schema.TypeString,
Computed: true,
Description: "Display name of the disk.",
},
"device_path": {
Type: schema.TypeString,
Computed: true,
Description: "Path of the physical volume of the disk.",
},
"capacity_gb": {
Type: schema.TypeInt,
Computed: true,
Description: "Capacity of the disk in GiB.",
},
}},
},
},
}
}
Expand Down Expand Up @@ -70,19 +94,35 @@ func dataSourceVSphereVmfsDisksRead(d *schema.ResourceData, meta interface{}) er
d.SetId(time.Now().UTC().String())

var disks []string
diskDetailsMap := make(map[string]map[string]interface{})
for _, sl := range hss.StorageDeviceInfo.ScsiLun {
if hsd, ok := sl.(*types.HostScsiDisk); ok {
if matched, _ := regexp.MatchString(d.Get("filter").(string), hsd.CanonicalName); matched {
disk := make(map[string]interface{})
disk["display_name"] = hsd.DisplayName
disk["device_path"] = hsd.DevicePath
block := hsd.Capacity.Block
blockSize := int64(hsd.Capacity.BlockSize)
disk["capacity_gb"] = structure.ByteToGiB(block * blockSize)
disks = append(disks, hsd.CanonicalName)
diskDetailsMap[hsd.CanonicalName] = disk
}
}
}

sort.Strings(disks)
// use the now sorted name list to create a matching order details list
diskDetails := make([]map[string]interface{}, len(disks))
for i, name := range disks {
diskDetails[i] = diskDetailsMap[name]
}

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

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

return nil
}
29 changes: 29 additions & 0 deletions vsphere/data_source_vsphere_vmfs_disks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ func TestAccDataSourceVSphereVmfsDisks_basic(t *testing.T) {
testCheckOutputBool("found", "true"),
),
},
{
Config: testAccDataSourceVSphereVmfsDisksInfoConfig(),
Check: resource.ComposeTestCheckFunc(
testCheckOutputBool("found", "true"),
),
},
},
})
}
Expand Down Expand Up @@ -79,3 +85,26 @@ output "found" {
os.Getenv("TF_VAR_VSPHERE_ESXI1"),
)
}

func testAccDataSourceVSphereVmfsDisksInfoConfig() string {
return fmt.Sprintf(`
%s

data "vsphere_host" "esxi_host" {
name = "%s"
datacenter_id = "${data.vsphere_datacenter.rootdc1.id}"
}

data "vsphere_vmfs_disks" "available" {
host_system_id = "${data.vsphere_host.esxi_host.id}"
rescan = true
}

output "found" {
value = "${length(data.vsphere_vmfs_disks.available.disk_details) >= 1 ? "true" : "false" }"
}
`,
testhelper.CombineConfigs(testhelper.ConfigDataRootDC1(), testhelper.ConfigDataRootPortGroup1()),
os.Getenv("TF_VAR_VSPHERE_ESXI1"),
)
}
9 changes: 5 additions & 4 deletions vsphere/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,9 +384,10 @@ func testRenameVMFirstDisk(s *terraform.State, resourceName string, new string)
var dcSpec []types.BaseVirtualDeviceConfigSpec
for _, d := range vprops.Config.Hardware.Device {
if oldDisk, ok := d.(*types.VirtualDisk); ok {
backing, _ := virtualdevice.GetBackingForDisk(oldDisk)
newFileName, err := virtualdisk.Move(
tVars.client,
oldDisk.Backing.(*types.VirtualDiskFlatVer2BackingInfo).FileName,
backing.GetFileName(),
dc,
new,
nil,
Expand All @@ -400,9 +401,9 @@ func testRenameVMFirstDisk(s *terraform.State, resourceName string, new string)
VirtualDeviceFileBackingInfo: types.VirtualDeviceFileBackingInfo{
FileName: newFileName,
},
ThinProvisioned: oldDisk.Backing.(*types.VirtualDiskFlatVer2BackingInfo).ThinProvisioned,
EagerlyScrub: oldDisk.Backing.(*types.VirtualDiskFlatVer2BackingInfo).EagerlyScrub,
DiskMode: oldDisk.Backing.(*types.VirtualDiskFlatVer2BackingInfo).DiskMode,
ThinProvisioned: backing.GetThinProvisioned(),
EagerlyScrub: backing.GetEagerlyScrub(),
DiskMode: backing.GetDiskMode(),
},
},
}
Expand Down
Loading