Skip to content

Commit

Permalink
Rebase merge conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
sumitAgrawal007 authored and appilon committed Jan 10, 2022
1 parent ea9f93c commit 70ec2e6
Show file tree
Hide file tree
Showing 8 changed files with 701 additions and 47 deletions.
36 changes: 35 additions & 1 deletion vsphere/data_source_vsphere_vmfs_disks.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package vsphere
import (
"context"
"fmt"
"github.com/hashicorp/terraform-provider-vsphere/vsphere/internal/helper/structure"
"regexp"
"sort"
"time"
Expand Down Expand Up @@ -40,6 +41,28 @@ func dataSourceVSphereVmfsDisks() *schema.Resource {
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"disks_info": {
Type: schema.TypeList,
Description: "The details of the disks discovered by the search.",
Computed: true,
Elem: &schema.Resource{Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Computed: true,
Description: "Display name of the disk",
},
"path": {
Type: schema.TypeString,
Computed: true,
Description: "Path of the physical volume of the disk.",
},
"capacity_in_gb": {
Type: schema.TypeInt,
Computed: true,
Description: "Capacity in GB.",
},
}},
},
},
}
}
Expand Down Expand Up @@ -70,19 +93,30 @@ func dataSourceVSphereVmfsDisksRead(d *schema.ResourceData, meta interface{}) er
d.SetId(time.Now().UTC().String())

var disks []string
var disksInfo []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
block := hsd.Capacity.Block
blockSize := int64(hsd.Capacity.BlockSize)
disk["capacity_in_gb"] = structure.ByteToGiB(block * blockSize)
disksInfo = append(disksInfo, disk)
disks = append(disks, hsd.CanonicalName)
}
}
}

sort.Strings(disks)

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 {
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.disks_info) >= 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

0 comments on commit 70ec2e6

Please sign in to comment.