test for subfolders

This commit is contained in:
Michael Kuzmin 2017-08-24 00:54:23 +03:00
parent 2c184abdef
commit c335dc1015
2 changed files with 38 additions and 4 deletions

View File

@ -108,13 +108,13 @@ func TestBuilderAcc_folder(t *testing.T) {
builderT.Test(t, builderT.TestCase{
Builder: &Builder{},
Template: folderConfig(),
Check: checkFolder(t, "folder1"),
Check: checkFolder(t, "folder1/folder2"),
})
}
func folderConfig() string {
config := defaultConfig()
config["folder"] = "folder1"
config["folder"] = "folder1/folder2"
config["linked_clone"] = true // speed up
return renderConfig(config)
}

View File

@ -3,12 +3,46 @@ package driver
import (
"github.com/vmware/govmomi/object"
"github.com/vmware/govmomi/vim25/types"
"github.com/vmware/govmomi/vim25/mo"
"fmt"
)
func (d *Driver) NewFolder(ref *types.ManagedObjectReference) *object.Folder {
return object.NewFolder(d.client.Client, *ref)
}
func (d *Driver) GetFolderPath(folder *object.Folder) (string, error) {
return folder.ObjectName(d.ctx)
func (d *Driver) FolderInfo(folder *object.Folder, params ...string) (*mo.Folder, error) {
var p []string
if len(params) == 0 {
p = []string{"*"}
} else {
p = params
}
var folderInfo mo.Folder
err := folder.Properties(d.ctx, folder.Reference(), p, &folderInfo)
if err != nil {
return nil, err
}
return &folderInfo, nil
}
func (d *Driver) GetFolderPath(folder *object.Folder) (string, error) {
f, err := d.FolderInfo(folder, "name", "parent")
if err != nil {
return "", err
}
if f.Parent.Type == "Datacenter" {
return "", nil
} else {
parent := d.NewFolder(f.Parent)
parentPath, err := d.GetFolderPath(parent)
if err != nil {
return "", err
}
if parentPath == "" {
return f.Name, nil
} else {
return fmt.Sprintf("%v/%v", parentPath, f.Name), nil
}
}
}