Skip to content

Commit

Permalink
Small edits to the PR
Browse files Browse the repository at this point in the history
  • Loading branch information
appilon committed Jan 10, 2022
1 parent 70ec2e6 commit 0e07c37
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 34 deletions.
34 changes: 20 additions & 14 deletions vsphere/data_source_vsphere_vmfs_disks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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.",
},
}},
},
Expand Down Expand Up @@ -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)
}

Expand Down
2 changes: 1 addition & 1 deletion vsphere/data_source_vsphere_vmfs_disks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand Down
21 changes: 10 additions & 11 deletions vsphere/internal/virtualdevice/virtual_machine_disk_subresource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions vsphere/resource_vsphere_virtual_machine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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 {
Expand Down
8 changes: 4 additions & 4 deletions website/docs/d/vmfs_disks.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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.
2 changes: 1 addition & 1 deletion website/docs/r/virtual_machine.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down

0 comments on commit 0e07c37

Please sign in to comment.