github.com/vmware/govmomi@v0.43.0/vim25/examples_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 vim25_test
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"net/http"
    23  	"time"
    24  
    25  	"github.com/vmware/govmomi/find"
    26  	"github.com/vmware/govmomi/simulator"
    27  	"github.com/vmware/govmomi/vim25"
    28  )
    29  
    30  func ExampleTemporaryNetworkError() {
    31  	simulator.Run(func(ctx context.Context, c *vim25.Client) error {
    32  		// Configure retry handler
    33  		delay := time.Millisecond * 100
    34  		retry := func(err error) (bool, time.Duration) {
    35  			return vim25.IsTemporaryNetworkError(err), delay
    36  		}
    37  		c.RoundTripper = vim25.Retry(c.Client, retry, 3)
    38  
    39  		vm, err := find.NewFinder(c).VirtualMachine(ctx, "DC0_H0_VM0")
    40  		if err != nil {
    41  			return err
    42  		}
    43  
    44  		// Tell vcsim to respond with 502 on the 1st request
    45  		simulator.StatusSDK = http.StatusBadGateway
    46  
    47  		state, err := vm.PowerState(ctx)
    48  		if err != nil {
    49  			return err
    50  		}
    51  
    52  		fmt.Println(state)
    53  
    54  		return nil
    55  	})
    56  	// Output: poweredOn
    57  }