duanmao9918 2019-06-09 08:41
浏览 156

Golang govmomi按资源池过滤VM

I currently have some go code to list VM names and Resource Pools with govmomi library. In the output I have some sort of Resource Pool identifiers, not actual names. So I have two questions: 1. Is there a clean and simple way to filter VMs by Resource Pool's name, as govc command:

govc find . -type m -resourcePool $(govc find -i . -name PoolName) 

2. Is there a way to search in all DCs, without specifying names, as I want to create goroutine to search in a number of vSpheres, they have different DC names but same Pools. My code:

package main

import (
    "flag"
    "fmt"
    "net/url"
    "os"
    "sync"
    "github.com/vmware/govmomi"
    "github.com/vmware/govmomi/find"
    "github.com/vmware/govmomi/property"
    "github.com/vmware/govmomi/vim25/mo"
    "github.com/vmware/govmomi/vim25/types"
    "golang.org/x/net/context"
)

var vsphereUrl = "https://name:pass@vsphere/sdk"
var insecureFlag = false


func exit(err error) {
    fmt.Fprintf(os.Stderr, "Error: %s
", err)
    os.Exit(1)
}


func main() {
    ctx, cancel := context.WithCancel(context.Background())
    defer cancel()
    flag.Parse()
    u, err := url.Parse(vsphereUrl)
    if err != nil {
        exit(err)
    }
    c, err := govmomi.NewClient(ctx, u, insecureFlag)
    if err != nil {
        exit(err)
    }
    f := find.NewFinder(c.Client, true)
    dcs := []string{"First-DC", "Second-DC"}
    var group sync.WaitGroup
    for _, dc := range dcs {
        group.Add(1)
        go func(ctx context.Context, dc string, f *find.Finder, c *govmomi.Client, group *sync.WaitGroup) {
            defer group.Done()
            dcObj, err := f.DatacenterOrDefault(ctx, dc)
            if err != nil {
                exit(err)
            }
            f.SetDatacenter(dcObj)
            vms, err := f.VirtualMachineList(ctx, "*")
            if err != nil {
                exit(err)
            }
            pc := property.DefaultCollector(c.Client)
            var refs []types.ManagedObjectReference
            for _, vm := range vms {
                refs = append(refs, vm.Reference())
            }
            var vmt []mo.VirtualMachine
            err = pc.Retrieve(ctx, refs, []string{"name", "resourcePool"}, &vmt)
            if err != nil {
                exit(err)
            }
            for _, vm := range vmt {
                fmt.Println(vm.Name, vm.ResourcePool)
            }
        }(ctx, dc, f, c, &group)
    }
    group.Wait()
    fmt.Println("Done.")
}

Current output:

SRVAP1027 ResourcePool:resgroup-3246
SRVAP1029 ResourcePool:resgroup-3246
SRVWE086 ResourcePool:resgroup-544
SRVDB087 ResourcePool:resgroup-3246
SRVAP1031 ResourcePool:resgroup-49
SRVAP1036 ResourcePool:resgroup-49
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度
    • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
    • ¥15 ETLCloud 处理json多层级问题
    • ¥15 matlab中使用gurobi时报错
    • ¥15 这个主板怎么能扩出一两个sata口
    • ¥15 不是,这到底错哪儿了😭
    • ¥15 2020长安杯与连接网探
    • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么