本文整理汇总了Golang中github.com/vmware/govmomi/property.DefaultCollector函数的典型用法代码示例。如果您正苦于以下问题:Golang DefaultCollector函数的具体用法?Golang DefaultCollector怎么用?Golang DefaultCollector使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了DefaultCollector函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: run
func (p *eventProcessor) run(ctx context.Context, tail bool) error {
if len(p.tailers) == 0 {
return nil
}
var err error
var collectors []types.ManagedObjectReference
for _, t := range p.tailers {
collectors = append(collectors, t.collector)
}
if len(p.tailers) > 1 {
// create and populate a ListView
viewMgr := view.NewManager(p.mgr.Client())
var listView *view.ListView
listView, err = viewMgr.CreateListView(ctx, collectors)
if err != nil {
return err
}
count := 0
// Retrieve the property from the objects in the ListView
err = property.WaitForView(ctx, property.DefaultCollector(p.mgr.Client()), listView.Reference(), collectors[0], []string{latestPageProp}, func(c types.ManagedObjectReference, pc []types.PropertyChange) bool {
if err = p.process(c, pc); err != nil {
return false
}
count++
if count == len(collectors) && !tail {
return true
}
return false
})
return err
}
// only one object to follow
err = property.Wait(ctx, property.DefaultCollector(p.mgr.Client()), collectors[0], []string{latestPageProp}, func(pc []types.PropertyChange) bool {
if err = p.process(collectors[0], pc); err != nil {
return false
}
if !tail {
return true
}
return false
})
return err
}
示例2: DetachDisk
func (cmd *vmdk) DetachDisk(vm *object.VirtualMachine) (string, error) {
ctx := context.TODO()
var mvm mo.VirtualMachine
pc := property.DefaultCollector(cmd.Client)
err := pc.RetrieveOne(ctx, vm.Reference(), []string{"config.hardware"}, &mvm)
if err != nil {
return "", err
}
spec := new(configSpec)
dsFile := spec.RemoveDisk(&mvm)
task, err := vm.Reconfigure(ctx, spec.ToSpec())
if err != nil {
return "", err
}
err = task.Wait(ctx)
if err != nil {
return "", err
}
return dsFile, nil
}
示例3: Run
func (cmd *info) Run(ctx context.Context, f *flag.FlagSet) error {
client, err := cmd.Client()
if err != nil {
return err
}
ns, err := cmd.HostNetworkSystem()
if err != nil {
return err
}
var mns mo.HostNetworkSystem
pc := property.DefaultCollector(client)
err = pc.RetrieveOne(context.TODO(), ns.Reference(), []string{"networkInfo.vswitch"}, &mns)
if err != nil {
return err
}
tw := tabwriter.NewWriter(os.Stdout, 2, 0, 2, ' ', 0)
for i, s := range mns.NetworkInfo.Vswitch {
if i > 0 {
fmt.Fprintln(tw)
}
fmt.Fprintf(tw, "Name:\t%s\n", s.Name)
fmt.Fprintf(tw, "Portgroup:\t%s\n", cmd.keys("key-vim.host.PortGroup-", s.Portgroup))
fmt.Fprintf(tw, "Pnic:\t%s\n", cmd.keys("key-vim.host.PhysicalNic-", s.Pnic))
fmt.Fprintf(tw, "MTU:\t%d\n", s.Mtu)
fmt.Fprintf(tw, "Ports:\t%d\n", s.NumPorts)
fmt.Fprintf(tw, "Ports Available:\t%d\n", s.NumPortsAvailable)
}
return tw.Flush()
}
示例4: LocalDatastores
func (h *Helper) LocalDatastores(ctx context.Context, cr *object.ComputeResource) ([]*object.Datastore, error) {
// List datastores for compute resource
dss, err := cr.Datastores(ctx)
if err != nil {
return nil, err
}
// Filter local datastores
var ldss []*object.Datastore
for _, ds := range dss {
var mds mo.Datastore
err = property.DefaultCollector(h.c).RetrieveOne(ctx, ds.Reference(), nil, &mds)
if err != nil {
return nil, err
}
switch i := mds.Info.(type) {
case *types.VmfsDatastoreInfo:
if i.Vmfs.Local != nil && *i.Vmfs.Local == true {
break
}
default:
continue
}
ds.InventoryPath = mds.Name
ldss = append(ldss, ds)
}
return ldss, nil
}
示例5: Hosts
func (c ComputeResource) Hosts(ctx context.Context) ([]*HostSystem, error) {
var cr mo.ComputeResource
err := c.Properties(ctx, c.Reference(), []string{"host"}, &cr)
if err != nil {
return nil, err
}
if len(cr.Host) == 0 {
return nil, nil
}
var hs []mo.HostSystem
pc := property.DefaultCollector(c.Client())
err = pc.Retrieve(ctx, cr.Host, []string{"name"}, &hs)
if err != nil {
return nil, err
}
var hosts []*HostSystem
for _, h := range hs {
host := NewHostSystem(c.Client(), h.Reference())
host.InventoryPath = path.Join(c.InventoryPath, h.Name)
hosts = append(hosts, host)
}
return hosts, nil
}
示例6: waitForNetworkingActive
func waitForNetworkingActive(client *govmomi.Client, datacenter, name string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
dc, err := getDatacenter(client, datacenter)
if err != nil {
log.Printf("[ERROR] %#v", err)
return nil, "", err
}
finder := find.NewFinder(client.Client, true)
finder = finder.SetDatacenter(dc)
vm, err := finder.VirtualMachine(context.TODO(), name)
if err != nil {
log.Printf("[ERROR] %#v", err)
return nil, "", err
}
var mvm mo.VirtualMachine
collector := property.DefaultCollector(client.Client)
if err := collector.RetrieveOne(context.TODO(), vm.Reference(), []string{"summary"}, &mvm); err != nil {
log.Printf("[ERROR] %#v", err)
return nil, "", err
}
if mvm.Summary.Guest.IpAddress != "" {
log.Printf("[DEBUG] IP address with DHCP: %v", mvm.Summary.Guest.IpAddress)
return mvm.Summary, "active", err
} else {
log.Printf("[DEBUG] Waiting for IP address")
return nil, "pending", err
}
}
}
示例7: WaitForIP
func (v VirtualMachine) WaitForIP(ctx context.Context) (string, error) {
var ip string
p := property.DefaultCollector(v.c)
err := property.Wait(ctx, p, v.Reference(), []string{"guest.ipAddress"}, func(pc []types.PropertyChange) bool {
for _, c := range pc {
if c.Name != "guest.ipAddress" {
continue
}
if c.Op != types.PropertyChangeOpAssign {
continue
}
if c.Val == nil {
continue
}
ip = c.Val.(string)
return true
}
return false
})
if err != nil {
return "", err
}
return ip, nil
}
示例8: IpAddress
// IpAddress attempts to find the guest IP address using esxcli.
// ESX hosts must be configured with the /Net/GuestIPHack enabled.
// For example:
// $ govc host.esxcli -- system settings advanced set -o /Net/GuestIPHack -i 1
func (g *GuestInfo) IpAddress(vm *object.VirtualMachine) (string, error) {
const any = "0.0.0.0"
var mvm mo.VirtualMachine
pc := property.DefaultCollector(g.c)
err := pc.RetrieveOne(context.TODO(), vm.Reference(), []string{"runtime.host", "config.uuid"}, &mvm)
if err != nil {
return "", err
}
h, err := g.hostInfo(mvm.Runtime.Host)
if err != nil {
return "", err
}
// Normalize uuid, esxcli and mo.VirtualMachine have different formats
uuid := strings.Replace(mvm.Config.Uuid, "-", "", -1)
if wid, ok := h.wids[uuid]; ok {
res, err := h.Run([]string{"network", "vm", "port", "list", "--world-id", wid})
if err != nil {
return "", err
}
for _, val := range res.Values {
if ip, ok := val["IPAddress"]; ok {
if ip[0] != any {
return ip[0], nil
}
}
}
}
return any, nil
}
示例9: WaitForMAC
// WaitForMac will wait until VM get mac for all attached nics.
// Returns map "Virtual Network Name": "nic MAC address"
func (vm VirtualMachine) WaitForMAC(ctx context.Context) (map[string]string, error) {
devices, err := vm.Device(ctx)
if err != nil {
log.Errorf("Unable to get device listing for VM")
return nil, err
}
nics := devices.SelectByType(&types.VirtualEthernetCard{})
macs := make(map[string]string)
// device name:network name
nicMappings := make(map[string]string)
for _, nic := range nics {
if n, ok := nic.(types.BaseVirtualEthernetCard); ok {
netName, err := vm.getNetworkName(ctx, n)
if err != nil {
log.Errorf("failed to get network name: %s", err)
return nil, err
}
macs[netName] = ""
nicMappings[devices.Name(nic)] = netName
} else {
log.Errorf("Failed to get network name of vNIC: %v", nic)
return nil, err
}
}
p := property.DefaultCollector(vm.Session.Vim25())
// Wait for all NICs to have a MacAddress, which may not be generated yet.
err = property.Wait(ctx, p, vm.Reference(), []string{"config.hardware.device"}, func(pc []types.PropertyChange) bool {
for _, c := range pc {
if c.Op != types.PropertyChangeOpAssign {
continue
}
changedDevices := c.Val.(types.ArrayOfVirtualDevice).VirtualDevice
for _, device := range changedDevices {
if nic, ok := device.(types.BaseVirtualEthernetCard); ok {
mac := nic.GetVirtualEthernetCard().MacAddress
if mac == "" {
continue
}
netName := nicMappings[devices.Name(device)]
macs[netName] = mac
}
}
}
for key, value := range macs {
if value == "" {
log.Debugf("Didn't get mac address for nic on %s, continue", key)
return false
}
}
return true
})
return macs, err
}
示例10: TestWaitForUpdates
func TestWaitForUpdates(t *testing.T) {
folder := esx.RootFolder
s := New(NewServiceInstance(esx.ServiceContent, folder))
ts := s.NewServer()
defer ts.Close()
ctx := context.Background()
c, err := govmomi.NewClient(ctx, ts.URL, true)
if err != nil {
t.Fatal(err)
}
cb := func(once bool) func([]types.PropertyChange) bool {
return func(pc []types.PropertyChange) bool {
if len(pc) != 1 {
t.Fail()
}
c := pc[0]
if c.Op != types.PropertyChangeOpAssign {
t.Fail()
}
if c.Name != "name" {
t.Fail()
}
if c.Val.(string) != folder.Name {
t.Fail()
}
return once
}
}
pc := property.DefaultCollector(c.Client)
props := []string{"name"}
err = property.Wait(ctx, pc, folder.Reference(), props, cb(true))
if err != nil {
t.Error(err)
}
// incremental updates not yet suppported
err = property.Wait(ctx, pc, folder.Reference(), props, cb(false))
if err == nil {
t.Error("expected error")
}
// test object not found
Map.Remove(folder.Reference())
err = property.Wait(ctx, pc, folder.Reference(), props, cb(true))
if err == nil {
t.Error("expected error")
}
}
示例11: Run
func (cmd *info) Run(ctx context.Context, f *flag.FlagSet) error {
c, err := cmd.Client()
if err != nil {
return err
}
finder, err := cmd.Finder()
if err != nil {
return err
}
args := f.Args()
if len(args) == 0 {
args = []string{"*"}
}
var props []string
res := infoResult{
finder: finder,
ctx: ctx,
}
if !cmd.OutputFlag.JSON {
props = []string{
"name",
"vmFolder",
"hostFolder",
"datastoreFolder",
"networkFolder",
"datastore",
"network",
}
}
for _, arg := range args {
objects, err := finder.DatacenterList(ctx, arg)
if err != nil {
return err
}
res.objects = append(res.objects, objects...)
}
if len(res.objects) != 0 {
refs := make([]types.ManagedObjectReference, 0, len(res.objects))
for _, o := range res.objects {
refs = append(refs, o.Reference())
}
pc := property.DefaultCollector(c)
err = pc.Retrieve(ctx, refs, props, &res.Datacenters)
if err != nil {
return err
}
}
return cmd.WriteResult(&res)
}
示例12: getVirtualMachineManagedObjectReference
func getVirtualMachineManagedObjectReference(ctx context.Context, c *govmomi.Client, vm *object.VirtualMachine, field string, dst interface{}) error {
collector := property.DefaultCollector(c.Client)
// Retrieve required field from VM object
err := collector.RetrieveOne(ctx, vm.Reference(), []string{field}, dst)
if err != nil {
return err
}
return nil
}
示例13: List
func (m Manager) List(ctx context.Context) ([]types.LicenseManagerLicenseInfo, error) {
var mlm mo.LicenseManager
err := property.DefaultCollector(m.c).RetrieveOne(ctx, m.Reference(), []string{"licenses"}, &mlm)
if err != nil {
return nil, err
}
return mlm.Licenses, nil
}
示例14: Run
func (cmd *info) Run(f *flag.FlagSet) error {
if f.NArg() == 0 {
return flag.ErrHelp
}
c, err := cmd.Client()
if err != nil {
return err
}
ctx := context.TODO()
finder, err := cmd.Finder()
if err != nil {
return err
}
var res infoResult
var props []string
if cmd.OutputFlag.JSON {
props = nil
} else {
props = []string{
"name",
"config.cpuAllocation",
"config.memoryAllocation",
"runtime.cpu",
"runtime.memory",
}
}
for _, arg := range f.Args() {
objects, err := finder.ResourcePoolList(ctx, arg)
if err != nil {
return err
}
res.objects = append(res.objects, objects...)
}
if len(res.objects) != 0 {
refs := make([]types.ManagedObjectReference, 0, len(res.objects))
for _, o := range res.objects {
refs = append(refs, o.Reference())
}
pc := property.DefaultCollector(c)
err = pc.Retrieve(ctx, refs, props, &res.ResourcePools)
if err != nil {
return err
}
}
return cmd.WriteResult(&res)
}
示例15: Run
func (cmd *info) Run(f *flag.FlagSet) error {
c, err := cmd.Client()
if err != nil {
return err
}
ctx := context.TODO()
var res infoResult
var props []string
if cmd.OutputFlag.JSON {
props = nil // Load everything
} else {
props = []string{"summary"} // Load summary
}
// We could do without the -host flag, leaving it for compat
host, err := cmd.HostSystemIfSpecified()
if err != nil {
return err
}
// Default only if there is a single host
if host == nil && f.NArg() == 0 {
host, err = cmd.HostSystem()
if err != nil {
return err
}
}
if host != nil {
res.objects = append(res.objects, host)
} else {
res.objects, err = cmd.HostSystems(f.Args())
if err != nil {
return err
}
}
if len(res.objects) != 0 {
refs := make([]types.ManagedObjectReference, 0, len(res.objects))
for _, o := range res.objects {
refs = append(refs, o.Reference())
}
pc := property.DefaultCollector(c)
err = pc.Retrieve(ctx, refs, props, &res.HostSystems)
if err != nil {
return err
}
}
return cmd.WriteResult(&res)
}