2017-08-23 15:40:57 -04:00
|
|
|
package driver
|
|
|
|
|
|
|
|
import (
|
2018-10-31 17:42:24 -04:00
|
|
|
"fmt"
|
2020-08-31 10:26:48 -04:00
|
|
|
"log"
|
2020-02-14 11:42:29 -05:00
|
|
|
|
2020-08-31 10:26:48 -04:00
|
|
|
"github.com/vmware/govmomi/find"
|
2017-08-23 15:40:57 -04:00
|
|
|
"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 15:40:57 -04:00
|
|
|
)
|
|
|
|
|
2017-08-23 20:06:50 -04:00
|
|
|
type ResourcePool struct {
|
|
|
|
pool *object.ResourcePool
|
2020-08-31 04:34:41 -04:00
|
|
|
driver *VCenterDriver
|
2017-08-23 20:06:50 -04:00
|
|
|
}
|
|
|
|
|
2020-08-31 04:34:41 -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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-31 04:34:41 -04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-08-31 10:26:48 -04:00
|
|
|
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 {
|
2020-08-31 10:26:48 -04:00
|
|
|
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
|
|
|
}
|
2020-08-31 10:26:48 -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 15:40:57 -04:00
|
|
|
}
|
|
|
|
|
2017-08-23 20:06:50 -04:00
|
|
|
func (p *ResourcePool) Info(params ...string) (*mo.ResourcePool, error) {
|
|
|
|
var params2 []string
|
2017-08-23 15:40:57 -04:00
|
|
|
if len(params) == 0 {
|
2017-08-23 20:06:50 -04:00
|
|
|
params2 = []string{"*"}
|
2017-08-23 15:40:57 -04:00
|
|
|
} else {
|
2017-08-23 20:06:50 -04:00
|
|
|
params2 = params
|
2017-08-23 15:40:57 -04:00
|
|
|
}
|
2017-08-23 20:06:50 -04:00
|
|
|
var info mo.ResourcePool
|
|
|
|
err := p.pool.Properties(p.driver.ctx, p.pool.Reference(), params2, &info)
|
2017-08-23 15:40:57 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-08-23 20:06:50 -04:00
|
|
|
return &info, nil
|
2017-08-23 15:40:57 -04:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|