github.com/vmware/govmomi@v0.37.2/event/example_test.go (about)

     1  /*
     2  Copyright (c) 2019 VMware, Inc. All Rights Reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package event_test
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  
    23  	"github.com/vmware/govmomi/event"
    24  	"github.com/vmware/govmomi/find"
    25  	"github.com/vmware/govmomi/simulator"
    26  	"github.com/vmware/govmomi/vim25"
    27  	"github.com/vmware/govmomi/vim25/mo"
    28  	"github.com/vmware/govmomi/vim25/types"
    29  )
    30  
    31  // ensure event.Manager implements the mo.Reference interface
    32  var _ mo.Reference = new(event.Manager)
    33  
    34  func ExampleManager_Events() {
    35  	simulator.Run(func(ctx context.Context, c *vim25.Client) error {
    36  		m := event.NewManager(c)
    37  
    38  		vm, err := find.NewFinder(c).VirtualMachine(ctx, "DC0_H0_VM0")
    39  		if err != nil {
    40  			return err
    41  		}
    42  
    43  		objs := []types.ManagedObjectReference{vm.Reference()}
    44  
    45  		return m.Events(ctx, objs, 10, false, false, func(ref types.ManagedObjectReference, events []types.BaseEvent) error {
    46  			event.Sort(events)
    47  			for _, event := range events {
    48  				fmt.Printf("%T\n", event)
    49  			}
    50  			return nil
    51  		})
    52  	})
    53  	// Output:
    54  	// *types.VmBeingCreatedEvent
    55  	// *types.VmInstanceUuidAssignedEvent
    56  	// *types.VmUuidAssignedEvent
    57  	// *types.VmCreatedEvent
    58  	// *types.VmStartingEvent
    59  	// *types.VmPoweredOnEvent
    60  }