2017-08-23 15:40:57 -04:00
|
|
|
package driver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/vmware/govmomi/object"
|
|
|
|
"github.com/vmware/govmomi/vim25/types"
|
2017-08-23 20:06:50 -04:00
|
|
|
"github.com/vmware/govmomi/vim25/mo"
|
2017-08-23 18:23:48 -04:00
|
|
|
"fmt"
|
2017-08-23 15:40:57 -04:00
|
|
|
)
|
|
|
|
|
2017-08-23 20:06:50 -04:00
|
|
|
type ResourcePool struct {
|
|
|
|
pool *object.ResourcePool
|
|
|
|
driver *Driver
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Driver) NewResourcePool(ref *types.ManagedObjectReference) *ResourcePool {
|
|
|
|
return &ResourcePool{
|
|
|
|
pool: object.NewResourcePool(d.client.Client, *ref),
|
|
|
|
driver: d,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Driver) FindResourcePool(host string, name string) (*ResourcePool, error) {
|
|
|
|
p, err := d.finder.ResourcePool(d.ctx, fmt.Sprintf("/%v/host/%v/Resources/%v", d.datacenter.Name(), host, name))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &ResourcePool{
|
|
|
|
pool: p,
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|