mirror of
https://github.com/opentofu/opentofu.git
synced 2025-02-25 18:45:20 -06:00
vSphere disks: read disk capacity instead of file size
This commit is contained in:
parent
9210222f3c
commit
07733e13ba
@ -4,12 +4,14 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
|
||||||
|
"errors"
|
||||||
"github.com/hashicorp/terraform/helper/schema"
|
"github.com/hashicorp/terraform/helper/schema"
|
||||||
"github.com/vmware/govmomi"
|
"github.com/vmware/govmomi"
|
||||||
"github.com/vmware/govmomi/find"
|
"github.com/vmware/govmomi/find"
|
||||||
"github.com/vmware/govmomi/object"
|
"github.com/vmware/govmomi/object"
|
||||||
"github.com/vmware/govmomi/vim25/types"
|
"github.com/vmware/govmomi/vim25/types"
|
||||||
"golang.org/x/net/context"
|
"golang.org/x/net/context"
|
||||||
|
"path"
|
||||||
)
|
)
|
||||||
|
|
||||||
type virtualDisk struct {
|
type virtualDisk struct {
|
||||||
@ -180,20 +182,66 @@ func resourceVSphereVirtualDiskRead(d *schema.ResourceData, meta interface{}) er
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
fileInfo, err := ds.Stat(context.TODO(), vDisk.vmdkPath)
|
ctx := context.TODO()
|
||||||
|
b, err := ds.Browser(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("[DEBUG] resourceVSphereVirtualDiskRead - stat failed on: %v", vDisk.vmdkPath)
|
return err
|
||||||
d.SetId("")
|
}
|
||||||
|
|
||||||
_, ok := err.(object.DatastoreNoSuchFileError)
|
// `Datastore.Stat` does not allow to query `VmDiskFileQuery`. Instead, we
|
||||||
if !ok {
|
// search the datastore manually.
|
||||||
return err
|
spec := types.HostDatastoreBrowserSearchSpec{
|
||||||
|
Query: []types.BaseFileQuery{&types.VmDiskFileQuery{Details: &types.VmDiskFileQueryFlags{
|
||||||
|
CapacityKb: true,
|
||||||
|
DiskType: true,
|
||||||
|
}}},
|
||||||
|
Details: &types.FileQueryFlags{
|
||||||
|
FileSize: true,
|
||||||
|
FileType: true,
|
||||||
|
Modification: true,
|
||||||
|
FileOwner: types.NewBool(true),
|
||||||
|
},
|
||||||
|
MatchPattern: []string{path.Base(vDisk.vmdkPath)},
|
||||||
|
}
|
||||||
|
|
||||||
|
dsPath := ds.Path(path.Dir(vDisk.vmdkPath))
|
||||||
|
task, err := b.SearchDatastore(context.TODO(), dsPath, &spec)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("[DEBUG] resourceVSphereVirtualDiskRead - could not search datastore for: %v", vDisk.vmdkPath)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
info, err := task.WaitForResult(context.TODO(), nil)
|
||||||
|
if err != nil {
|
||||||
|
if info == nil || info.Error != nil {
|
||||||
|
_, ok := info.Error.Fault.(*types.FileNotFound)
|
||||||
|
if ok {
|
||||||
|
log.Printf("[DEBUG] resourceVSphereVirtualDiskRead - could not find: %v", vDisk.vmdkPath)
|
||||||
|
d.SetId("")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log.Printf("[DEBUG] resourceVSphereVirtualDiskRead - could not search datastore for: %v", vDisk.vmdkPath)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
res := info.Result.(types.HostDatastoreBrowserSearchResults)
|
||||||
|
log.Printf("[DEBUG] num results: %d", len(res.File))
|
||||||
|
if len(res.File) == 0 {
|
||||||
|
d.SetId("")
|
||||||
|
log.Printf("[DEBUG] resourceVSphereVirtualDiskRead - could not find: %v", vDisk.vmdkPath)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
fileInfo = fileInfo.GetFileInfo()
|
|
||||||
|
if len(res.File) != 1 {
|
||||||
|
return errors.New("Datastore search did not return exactly one result")
|
||||||
|
}
|
||||||
|
|
||||||
|
fileInfo := res.File[0]
|
||||||
log.Printf("[DEBUG] resourceVSphereVirtualDiskRead - fileinfo: %#v", fileInfo)
|
log.Printf("[DEBUG] resourceVSphereVirtualDiskRead - fileinfo: %#v", fileInfo)
|
||||||
size := fileInfo.(*types.FileInfo).FileSize / 1024 / 1024 / 1024
|
size := fileInfo.(*types.VmDiskFileInfo).CapacityKb / 1024 / 1024
|
||||||
|
|
||||||
d.SetId(vDisk.vmdkPath)
|
d.SetId(vDisk.vmdkPath)
|
||||||
|
|
||||||
|
@ -27,6 +27,8 @@ func TestAccVSphereVirtualDisk_basic(t *testing.T) {
|
|||||||
}
|
}
|
||||||
if v := os.Getenv("VSPHERE_INIT_TYPE"); v != "" {
|
if v := os.Getenv("VSPHERE_INIT_TYPE"); v != "" {
|
||||||
initTypeOpt += fmt.Sprintf(" type = \"%s\"\n", v)
|
initTypeOpt += fmt.Sprintf(" type = \"%s\"\n", v)
|
||||||
|
} else {
|
||||||
|
initTypeOpt += fmt.Sprintf(" type = \"%s\"\n", "thin")
|
||||||
}
|
}
|
||||||
if v := os.Getenv("VSPHERE_ADAPTER_TYPE"); v != "" {
|
if v := os.Getenv("VSPHERE_ADAPTER_TYPE"); v != "" {
|
||||||
adapterTypeOpt += fmt.Sprintf(" adapter_type = \"%s\"\n", v)
|
adapterTypeOpt += fmt.Sprintf(" adapter_type = \"%s\"\n", v)
|
||||||
|
Loading…
Reference in New Issue
Block a user