Beispiel #1
0
// getVCHs will check vm with same name under this resource pool, to see if that's VCH vm, and it will also check children vApp, to see if that's a VCH.
// eventually return all fond VCH VMs
func (d *Dispatcher) getChildVCHs(pool *object.ResourcePool, searchVapp bool) ([]*vm.VirtualMachine, error) {
	defer trace.End(trace.Begin(pool.InventoryPath))

	// check if pool itself contains VCH vm.
	var vchs []*vm.VirtualMachine
	poolName := pool.Name()
	computeResource := compute.NewResourcePool(d.ctx, d.session, pool.Reference())
	vmm, err := computeResource.GetChildVM(d.ctx, d.session, poolName)
	if err != nil {
		return nil, errors.Errorf("Failed to query children VM in resource pool %q: %s", pool.InventoryPath, err)
	}
	if vmm != nil {
		vmm.InventoryPath = path.Join(pool.InventoryPath, poolName)
		if ok, _ := d.isVCH(vmm); ok {
			log.Debugf("%q is VCH", vmm.InventoryPath)
			vchs = append(vchs, vmm)
		}
	}

	if !searchVapp {
		return vchs, nil
	}

	vappPath := path.Join(pool.InventoryPath, "*")
	vapps, err := d.session.Finder.VirtualAppList(d.ctx, vappPath)
	if err != nil {
		if _, ok := err.(*find.NotFoundError); ok {
			return vchs, nil
		}
		log.Debugf("Failed to query vapp %q: %s", vappPath, err)
	}
	for _, vapp := range vapps {
		childVCHs, err := d.getChildVCHs(vapp.ResourcePool, false)
		if err != nil {
			return nil, err
		}
		vchs = append(vchs, childVCHs...)
	}
	return vchs, nil
}