diff --git a/vsphere/data_source_vsphere_vmfs_disks.go b/vsphere/data_source_vsphere_vmfs_disks.go index 31ed6c80b..97370204f 100644 --- a/vsphere/data_source_vsphere_vmfs_disks.go +++ b/vsphere/data_source_vsphere_vmfs_disks.go @@ -3,11 +3,12 @@ package vsphere import ( "context" "fmt" - "github.com/hashicorp/terraform-provider-vsphere/vsphere/internal/helper/structure" "regexp" "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" @@ -37,29 +38,29 @@ 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}, }, - "disks_info": { + "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{ - "name": { + "display_name": { Type: schema.TypeString, Computed: true, - Description: "Display name of the disk", + Description: "Display name of the disk.", }, - "path": { + "device_path": { Type: schema.TypeString, Computed: true, Description: "Path of the physical volume of the disk.", }, - "capacity_in_gb": { + "capacity_gb": { Type: schema.TypeInt, Computed: true, - Description: "Capacity in GB.", + Description: "Capacity of the disk in GiB.", }, }}, }, @@ -93,28 +94,33 @@ func dataSourceVSphereVmfsDisksRead(d *schema.ResourceData, meta interface{}) er d.SetId(time.Now().UTC().String()) var disks []string - var disksInfo []map[string]interface{} + 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["name"] = hsd.DisplayName - disk["path"] = hsd.DevicePath + disk["display_name"] = hsd.DisplayName + disk["device_path"] = hsd.DevicePath block := hsd.Capacity.Block blockSize := int64(hsd.Capacity.BlockSize) - disk["capacity_in_gb"] = structure.ByteToGiB(block * blockSize) - disksInfo = append(disksInfo, disk) + 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("disks_info", disksInfo); err != nil { + if err := d.Set("disk_details", diskDetails); err != nil { return fmt.Errorf("error saving results to state: %s", err) } diff --git a/vsphere/data_source_vsphere_vmfs_disks_test.go b/vsphere/data_source_vsphere_vmfs_disks_test.go index 6d205b91e..9167f8f21 100644 --- a/vsphere/data_source_vsphere_vmfs_disks_test.go +++ b/vsphere/data_source_vsphere_vmfs_disks_test.go @@ -101,7 +101,7 @@ data "vsphere_vmfs_disks" "available" { } output "found" { - value = "${length(data.vsphere_vmfs_disks.available.disks_info) >= 1 ? "true" : "false" }" + value = "${length(data.vsphere_vmfs_disks.available.disk_details) >= 1 ? "true" : "false" }" } `, testhelper.CombineConfigs(testhelper.ConfigDataRootDC1(), testhelper.ConfigDataRootPortGroup1()), diff --git a/vsphere/internal/virtualdevice/virtual_machine_disk_subresource.go b/vsphere/internal/virtualdevice/virtual_machine_disk_subresource.go index 68c0e45f8..a618e3632 100644 --- a/vsphere/internal/virtualdevice/virtual_machine_disk_subresource.go +++ b/vsphere/internal/virtualdevice/virtual_machine_disk_subresource.go @@ -206,15 +206,17 @@ func DiskSubresourceSchema() map[string]*schema.Schema { Description: "The type of controller the disk should be connected to. Must be 'scsi', 'sata', or 'ide'.", }, "rdm_lun_path": { - Type: schema.TypeString, - Optional: true, - Description: "The path to the Lun to be used for RDM disk.", + Type: schema.TypeString, + Optional: true, + RequiredWith: []string{"compatibility_mode"}, + Description: "The path to the LUN to be used for RDM disk.", }, "compatibility_mode": { Type: schema.TypeString, Optional: true, + RequiredWith: []string{"rdm_lun_path"}, ValidateFunc: validation.StringInSlice(diskSubresourceCompatibilityModeAllowedValues, false), - Description: "compatibility mode for rdm disk {physical or virtual}.", + Description: "Compatibility mode for RDM disk.", }, } structure.MergeSchema(s, subresourceSchema()) @@ -1562,14 +1564,11 @@ func (r *DiskSubresource) DiffGeneral() error { if r.Get("eagerly_scrub").(bool) && r.Get("thin_provisioned").(bool) { return fmt.Errorf("%s: eagerly_scrub and thin_provisioned cannot both be set to true", name) } - if r.Get("rdm_lun_path").(string) != "" && r.Get("compatibility_mode").(string) == "" { - return fmt.Errorf("%s: To add a RDM Disk, compatibility_mode is a required parameter", name) - } - if r.Get("rdm_lun_path").(string) != "" && r.Get("compatibility_mode").(string) == string(types.VirtualDiskCompatibilityModePhysicalMode) { - if r.Get("disk_mode").(string) != string(types.VirtualDiskModeIndependent_persistent) { - return fmt.Errorf("%s: To add RDM Disks in physical compatibility mode, only independent persistent disk mode is supported", name) - } + + if r.Get("compatibility_mode").(string) == string(types.VirtualDiskCompatibilityModePhysicalMode) && r.Get("disk_mode").(string) != string(types.VirtualDiskModeIndependent_persistent) { + return fmt.Errorf("%s: To add RDM Disks in physical compatibility mode, only independent persistent disk mode is supported", name) } + log.Printf("[DEBUG] %s: Diff validation complete", r) return nil } diff --git a/vsphere/resource_vsphere_virtual_machine_test.go b/vsphere/resource_vsphere_virtual_machine_test.go index 3433ce718..636a20000 100644 --- a/vsphere/resource_vsphere_virtual_machine_test.go +++ b/vsphere/resource_vsphere_virtual_machine_test.go @@ -546,6 +546,9 @@ func TestAccResourceVSphereVirtualMachine_RDMDisk(t *testing.T) { RunSweepers() testAccPreCheck(t) testAccResourceVSphereVirtualMachinePreCheck(t) + if os.Getenv("TF_VAR_VSPHERE_RDM_DISK_LUN_PATH") == "" { + t.Skip("set TF_VAR_VSPHERE_RDM_DISK_LUN_PATH to run vsphere_virtual_machine RDM tests") + } }, Providers: testAccProviders, CheckDestroy: testAccResourceVSphereVirtualMachineCheckExists(false), @@ -2573,9 +2576,6 @@ func testAccResourceVSphereVirtualMachinePreCheck(t *testing.T) { if os.Getenv("TF_VAR_VSPHERE_CONTENT_LIBRARY_FILES") == "" { t.Skip("set TF_VAR_VSPHERE_CONTENT_LIBRARY_FILES to run vsphere_virtual_machine acceptance tests") } - if os.Getenv("TF_VAR_VSPHERE_RDM_DISK_LUN_PATH") == "" { - t.Skip("set TF_VAR_VSPHERE_RDM_DISK_LUN_PATH to run vsphere_virtual_machine acceptance tests") - } } func testAccResourceVSphereVirtualMachineCheckExists(expected bool) resource.TestCheckFunc { diff --git a/website/docs/d/vmfs_disks.html.markdown b/website/docs/d/vmfs_disks.html.markdown index 4d0707f1a..eaa595c48 100644 --- a/website/docs/d/vmfs_disks.html.markdown +++ b/website/docs/d/vmfs_disks.html.markdown @@ -58,7 +58,7 @@ the output `disks` attribute below, which is lexicographically sorted. * `disks` - A lexicographically sorted list of devices discovered by the operation, matching the supplied `filter`, if provided. -* `disks_info` - List of disks discovered by the operation with more details about them. - * `name` - Display name of the disk - * `path` - The path of the volume of the disk. - * `capacity_in_gb` - Capacity of the disk in GB. +* `disk_details` - List of disks discovered by the operation with more details about them. The order matches that of `disks` + * `display_name` - Display name of the disk + * `device_path` - Path of the physical volume of the disk. + * `capacity_gb` - Capacity of the disk in GiB. diff --git a/website/docs/r/virtual_machine.html.markdown b/website/docs/r/virtual_machine.html.markdown index 1e51fa1ee..c15b4f626 100644 --- a/website/docs/r/virtual_machine.html.markdown +++ b/website/docs/r/virtual_machine.html.markdown @@ -891,7 +891,7 @@ resource "vsphere_virtual_machine" "vm" { label = "disk2" size = "10" unit_number = 2 - rdm_lun_path = "//Target LUN path to add a RDM Disk" + rdm_lun_path = "/path/to/lun" compatibility_mode = "physicalMode" }