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

75 lines
1.5 KiB
Go
Raw Normal View History

package driver
import (
2018-10-31 17:42:24 -04:00
"fmt"
"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 *Driver
}
func (d *Driver) NewResourcePool(ref *types.ManagedObjectReference) *ResourcePool {
return &ResourcePool{
pool: object.NewResourcePool(d.client.Client, *ref),
driver: d,
}
}
2018-03-15 17:53:25 -04:00
func (d *Driver) FindResourcePool(cluster string, host string, name string) (*ResourcePool, error) {
var res string
if cluster != "" {
res = cluster
} else {
res = host
}
p, err := d.finder.ResourcePool(d.ctx, fmt.Sprintf("%v/Resources/%v", res, name))
2017-08-23 20:06:50 -04:00
if err != nil {
return nil, err
}
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
}
}
}