Example #1
0
func (f *Finder) NetworkList(ctx context.Context, path string) ([]object.NetworkReference, error) {
	es, err := f.find(ctx, f.networkFolder, false, path)
	if err != nil {
		return nil, err
	}

	var ns []object.NetworkReference
	for _, e := range es {
		ref := e.Object.Reference()
		switch ref.Type {
		case "Network":
			r := object.NewNetwork(f.client, ref)
			r.InventoryPath = e.Path
			ns = append(ns, r)
		case "DistributedVirtualPortgroup":
			r := object.NewDistributedVirtualPortgroup(f.client, ref)
			r.InventoryPath = e.Path
			ns = append(ns, r)
		case "DistributedVirtualSwitch", "VmwareDistributedVirtualSwitch":
			r := object.NewDistributedVirtualSwitch(f.client, ref)
			r.InventoryPath = e.Path
			ns = append(ns, r)
		}
	}

	if len(ns) == 0 {
		return nil, &NotFoundError{"network", path}
	}

	return ns, nil
}
Example #2
0
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
}