create the folders if they don't exist
This commit is contained in:
parent
b357027f07
commit
e59fab952d
|
@ -3,7 +3,9 @@ package driver
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"path"
|
"path"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/vmware/govmomi/find"
|
||||||
"github.com/vmware/govmomi/object"
|
"github.com/vmware/govmomi/object"
|
||||||
"github.com/vmware/govmomi/vim25/mo"
|
"github.com/vmware/govmomi/vim25/mo"
|
||||||
"github.com/vmware/govmomi/vim25/types"
|
"github.com/vmware/govmomi/vim25/types"
|
||||||
|
@ -22,10 +24,32 @@ func (d *Driver) NewFolder(ref *types.ManagedObjectReference) *Folder {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Driver) FindFolder(name string) (*Folder, error) {
|
func (d *Driver) FindFolder(name string) (*Folder, error) {
|
||||||
|
if name != "" {
|
||||||
|
// create folders if they don't exist
|
||||||
|
parent := ""
|
||||||
|
parentFolder, err := d.finder.Folder(d.ctx, path.Join(d.datacenter.InventoryPath, "vm"))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
folders := strings.Split(name, "/")
|
||||||
|
for _, folder := range folders {
|
||||||
|
parent = path.Join(parent, folder)
|
||||||
|
f, err := d.finder.Folder(d.ctx, path.Join(d.datacenter.InventoryPath, "vm", parent))
|
||||||
|
if _, ok := err.(*find.NotFoundError); ok {
|
||||||
|
f, err = parentFolder.CreateFolder(d.ctx, folder)
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
parentFolder = f
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
f, err := d.finder.Folder(d.ctx, path.Join(d.datacenter.InventoryPath, "vm", name))
|
f, err := d.finder.Folder(d.ctx, path.Join(d.datacenter.InventoryPath, "vm", name))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return &Folder{
|
return &Folder{
|
||||||
folder: f,
|
folder: f,
|
||||||
driver: d,
|
driver: d,
|
||||||
|
|
Loading…
Reference in New Issue