packer-cn/builder/vsphere/driver/resource_pool.go

92 lines
2.0 KiB
Go
Raw Normal View History

package driver
import (
2018-10-31 17:42:24 -04:00
"fmt"
"log"
"github.com/vmware/govmomi/find"
"github.com/vmware/govmomi/object"
2017-08-23 20:06:50 -04:00
"github.com/vmware/govmomi/vim25/mo"
2018-10-31 17:42:24 -04:00
"github.com/vmware/govmomi/vim25/types"
)
2017-08-23 20:06:50 -04:00
type ResourcePool struct {
pool *object.ResourcePool
driver *VCenterDriver
2017-08-23 20:06:50 -04:00
}
func (d *VCenterDriver) NewResourcePool(ref *types.ManagedObjectReference) *ResourcePool {
2017-08-23 20:06:50 -04:00
return &ResourcePool{
pool: object.NewResourcePool(d.client.Client, *ref),
driver: d,
}
}
func (d *VCenterDriver) FindResourcePool(cluster string, host string, name string) (*ResourcePool, error) {
2018-03-15 17:53:25 -04:00
var res string
if cluster != "" {
res = cluster
} else {
res = host
}
resourcePath := fmt.Sprintf("%v/Resources/%v", res, name)
p, err := d.finder.ResourcePool(d.ctx, resourcePath)
2017-08-23 20:06:50 -04:00
if err != nil {
log.Printf("[WARN] %s not found. Looking for default resource pool.", resourcePath)
dp, dperr := d.finder.DefaultResourcePool(d.ctx)
if _, ok := dperr.(*find.NotFoundError); ok {
// VirtualApp extends ResourcePool, so it should support VirtualApp types.
vapp, verr := d.finder.VirtualApp(d.ctx, name)
if verr != nil {
return nil, err
}
dp = vapp.ResourcePool
} else if dperr != nil {
return nil, err
}
p = dp
2017-08-23 20:06:50 -04:00
}
2017-08-23 20:06:50 -04:00
return &ResourcePool{
2018-10-31 17:42:24 -04:00
pool: p,
2017-08-23 20:06:50 -04:00
driver: d,
}, nil
}
2017-08-23 20:06:50 -04:00
func (p *ResourcePool) Info(params ...string) (*mo.ResourcePool, error) {
var params2 []string
if len(params) == 0 {
2017-08-23 20:06:50 -04:00
params2 = []string{"*"}
} else {
2017-08-23 20:06:50 -04:00
params2 = params
}
2017-08-23 20:06:50 -04:00
var info mo.ResourcePool
err := p.pool.Properties(p.driver.ctx, p.pool.Reference(), params2, &info)
if err != nil {
return nil, err
}
2017-08-23 20:06:50 -04:00
return &info, nil
}
2017-08-23 18:23:48 -04:00
2017-08-23 20:06:50 -04:00
func (p *ResourcePool) Path() (string, error) {
poolInfo, err := p.Info("name", "parent")
2017-08-23 18:23:48 -04:00
if err != nil {
return "", err
}
2017-08-23 20:06:50 -04:00
if poolInfo.Parent.Type == "ComputeResource" {
2017-08-23 18:23:48 -04:00
return "", nil
} else {
2017-08-23 20:06:50 -04:00
parent := p.driver.NewResourcePool(poolInfo.Parent)
parentPath, err := parent.Path()
2017-08-23 18:23:48 -04:00
if err != nil {
return "", err
}
if parentPath == "" {
2017-08-23 20:06:50 -04:00
return poolInfo.Name, nil
2017-08-23 18:23:48 -04:00
} else {
2017-08-23 20:06:50 -04:00
return fmt.Sprintf("%v/%v", parentPath, poolInfo.Name), nil
2017-08-23 18:23:48 -04:00
}
}
}