Merge branch 'peay-vsphere-disk-capacity'

This commit is contained in:
stack72 2017-03-27 18:11:31 +03:00
commit 428d3926fa
No known key found for this signature in database
GPG Key ID: 8619A619B085CB16
2 changed files with 68 additions and 19 deletions

View File

@ -4,12 +4,14 @@ import (
"fmt"
"log"
"errors"
"github.com/hashicorp/terraform/helper/schema"
"github.com/vmware/govmomi"
"github.com/vmware/govmomi/find"
"github.com/vmware/govmomi/object"
"github.com/vmware/govmomi/vim25/types"
"golang.org/x/net/context"
"path"
)
type virtualDisk struct {
@ -180,20 +182,66 @@ func resourceVSphereVirtualDiskRead(d *schema.ResourceData, meta interface{}) er
return err
}
fileInfo, err := ds.Stat(context.TODO(), vDisk.vmdkPath)
ctx := context.TODO()
b, err := ds.Browser(ctx)
if err != nil {
log.Printf("[DEBUG] resourceVSphereVirtualDiskRead - stat failed on: %v", vDisk.vmdkPath)
d.SetId("")
return err
}
_, ok := err.(object.DatastoreNoSuchFileError)
if !ok {
return err
// `Datastore.Stat` does not allow to query `VmDiskFileQuery`. Instead, we
// search the datastore manually.
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
}
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)
size := fileInfo.(*types.FileInfo).FileSize / 1024 / 1024 / 1024
size := fileInfo.(*types.VmDiskFileInfo).CapacityKb / 1024 / 1024
d.SetId(vDisk.vmdkPath)

View File

@ -6,6 +6,7 @@ import (
"os"
"testing"
"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
"github.com/vmware/govmomi"
@ -19,6 +20,8 @@ func TestAccVSphereVirtualDisk_basic(t *testing.T) {
var initTypeOpt string
var adapterTypeOpt string
rString := acctest.RandString(5)
if v := os.Getenv("VSPHERE_DATACENTER"); v != "" {
datacenterOpt = v
}
@ -27,6 +30,8 @@ func TestAccVSphereVirtualDisk_basic(t *testing.T) {
}
if v := os.Getenv("VSPHERE_INIT_TYPE"); v != "" {
initTypeOpt += fmt.Sprintf(" type = \"%s\"\n", v)
} else {
initTypeOpt += fmt.Sprintf(" type = \"%s\"\n", "thin")
}
if v := os.Getenv("VSPHERE_ADAPTER_TYPE"); v != "" {
adapterTypeOpt += fmt.Sprintf(" adapter_type = \"%s\"\n", v)
@ -37,14 +42,8 @@ func TestAccVSphereVirtualDisk_basic(t *testing.T) {
Providers: testAccProviders,
CheckDestroy: testAccCheckVSphereVirtualDiskDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: fmt.Sprintf(
testAccCheckVSphereVirtuaDiskConfig_basic,
initTypeOpt,
adapterTypeOpt,
datacenterOpt,
datastoreOpt,
),
{
Config: testAccCheckVSphereVirtuaDiskConfig_basic(rString, initTypeOpt, adapterTypeOpt, datacenterOpt, datastoreOpt),
Check: resource.ComposeTestCheckFunc(
testAccVSphereVirtualDiskExists("vsphere_virtual_disk.foo"),
),
@ -117,13 +116,15 @@ func testAccCheckVSphereVirtualDiskDestroy(s *terraform.State) error {
return nil
}
const testAccCheckVSphereVirtuaDiskConfig_basic = `
func testAccCheckVSphereVirtuaDiskConfig_basic(rName, initTypeOpt, adapterTypeOpt, datacenterOpt, datastoreOpt string) string {
return fmt.Sprintf(`
resource "vsphere_virtual_disk" "foo" {
size = 1
vmdk_path = "tfTestDisk.vmdk"
vmdk_path = "tfTestDisk-%s.vmdk"
%s
%s
datacenter = "%s"
datastore = "%s"
}
`
`, rName, initTypeOpt, adapterTypeOpt, datacenterOpt, datastoreOpt)
}