github.com/vmware/govmomi@v0.51.0/vim25/examples_test.go (about)

     1  // © Broadcom. All Rights Reserved.
     2  // The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.
     3  // SPDX-License-Identifier: Apache-2.0
     4  
     5  package vim25_test
     6  
     7  import (
     8  	"context"
     9  	"fmt"
    10  	"net/http"
    11  	"time"
    12  
    13  	"github.com/vmware/govmomi/find"
    14  	"github.com/vmware/govmomi/simulator"
    15  	"github.com/vmware/govmomi/vim25"
    16  )
    17  
    18  func ExampleTemporaryNetworkError() {
    19  	simulator.Run(func(ctx context.Context, c *vim25.Client) error {
    20  		// Configure retry handler
    21  		delay := time.Millisecond * 100
    22  		retry := func(err error) (bool, time.Duration) {
    23  			return vim25.IsTemporaryNetworkError(err), delay
    24  		}
    25  		c.RoundTripper = vim25.Retry(c.Client, retry, 3)
    26  
    27  		vm, err := find.NewFinder(c).VirtualMachine(ctx, "DC0_H0_VM0")
    28  		if err != nil {
    29  			return err
    30  		}
    31  
    32  		// Tell vcsim to respond with 502 on the 1st request
    33  		simulator.StatusSDK = http.StatusBadGateway
    34  
    35  		state, err := vm.PowerState(ctx)
    36  		if err != nil {
    37  			return err
    38  		}
    39  
    40  		fmt.Println(state)
    41  
    42  		return nil
    43  	})
    44  	// Output: poweredOn
    45  }