コード例 #1
0
func deleteFolder(client *govmomi.Client, f *folder) error {
	dc, err := getDatacenter(client, f.datacenter)
	if err != nil {
		return err
	}
	var folder *object.Folder
	currentPath := f.path

	finder := find.NewFinder(client.Client, true)
	finder = finder.SetDatacenter(dc)
	si := object.NewSearchIndex(client.Client)

	folderRef, err := si.FindByInventoryPath(
		context.TODO(), fmt.Sprintf("%v/vm/%v", f.datacenter, f.path))

	if err != nil {
		return fmt.Errorf("[ERROR] Could not locate folder %s: %v", f.path, err)
	} else {
		folder = folderRef.(*object.Folder)
	}

	log.Printf("[INFO] Deleting empty sub-folders of existing path: %s", f.existingPath)
	for currentPath != f.existingPath {
		log.Printf("[INFO] Deleting folder: %s", currentPath)
		children, err := folder.Children(context.TODO())
		if err != nil {
			return err
		}

		if len(children) > 0 {
			return fmt.Errorf("Folder %s is non-empty and will not be deleted", currentPath)
		} else {
			log.Printf("[DEBUG] current folder: %#v", folder)
			currentPath = path.Dir(currentPath)
			if currentPath == "." {
				currentPath = ""
			}
			log.Printf("[INFO] parent path of %s is calculated as %s", f.path, currentPath)
			task, err := folder.Destroy(context.TODO())
			if err != nil {
				return err
			}
			err = task.Wait(context.TODO())
			if err != nil {
				return err
			}
			folderRef, err = si.FindByInventoryPath(
				context.TODO(), fmt.Sprintf("%v/vm/%v", f.datacenter, currentPath))

			if err != nil {
				return err
			} else if folderRef != nil {
				folder = folderRef.(*object.Folder)
			}
		}
	}
	return nil
}
コード例 #2
0
ファイル: finder.go プロジェクト: gonkulator/govmomi
func (f *Finder) findSwitchesInFolder(ctx context.Context, folder *object.Folder) []object.DistributedVirtualSwitch {
	var switches []object.DistributedVirtualSwitch
	if children, err := folder.Children(ctx); err == nil {
		for _, j := range children {
			if j.Reference().Type == "VmwareDistributedVirtualSwitch" {
				switches = append(switches, *object.NewDistributedVirtualSwitch(f.client, j.Reference()))
			}
			if j.Reference().Type == "Folder" {
				switches2 := f.findSwitchesInFolder(ctx, object.NewFolder(f.client, j.Reference()))
				for _, k := range switches2 {
					switches = append(switches, k)
				}
			}
		}
	} else {
		fmt.Printf("Error getting switches! %v\n", err)
	}
	return switches
}