github.com/vmware/govmomi@v0.37.2/examples/tasks/main.go (about)

     1  /*
     2  Copyright (c) 2020 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 main
    18  
    19  import (
    20  	"context"
    21  	"flag"
    22  	"time"
    23  
    24  	"github.com/dougm/pretty"
    25  
    26  	"github.com/vmware/govmomi/task"
    27  
    28  	"github.com/vmware/govmomi/examples"
    29  	"github.com/vmware/govmomi/vim25"
    30  	"github.com/vmware/govmomi/vim25/methods"
    31  	"github.com/vmware/govmomi/vim25/types"
    32  )
    33  
    34  func main() {
    35  	// example use against vCenter with optional time filters:
    36  	// go run main.go -url $GOVMOMI_URL -insecure $GOVMOMI_INSECURE -b 8h -f
    37  	begin := flag.Duration("b", 10*time.Minute, "Begin time (filtered by started time)") // default BeginTime is 10min ago
    38  	end := flag.Duration("e", 0, "End time (filtered by started time)")
    39  	follow := flag.Bool("f", false, "Follow task stream")
    40  
    41  	examples.Run(func(ctx context.Context, c *vim25.Client) error {
    42  		m := task.NewManager(c)
    43  
    44  		ref := c.ServiceContent.RootFolder
    45  
    46  		now, err := methods.GetCurrentTime(ctx, c) // vCenter server time (UTC)
    47  		if err != nil {
    48  			return err
    49  		}
    50  
    51  		filter := types.TaskFilterSpec{
    52  			Entity: &types.TaskFilterSpecByEntity{
    53  				Entity:    ref,
    54  				Recursion: types.TaskFilterSpecRecursionOptionAll,
    55  			},
    56  			Time: &types.TaskFilterSpecByTime{
    57  				TimeType:  types.TaskFilterSpecTimeOptionStartedTime,
    58  				BeginTime: types.NewTime(now.Add(*begin * -1)),
    59  			},
    60  		}
    61  
    62  		if *end != 0 {
    63  			filter.Time.EndTime = types.NewTime(now.Add(*end * -1))
    64  		}
    65  
    66  		collector, err := m.CreateCollectorForTasks(ctx, filter)
    67  		if err != nil {
    68  			return err
    69  		}
    70  
    71  		defer collector.Destroy(ctx)
    72  
    73  		for {
    74  			tasks, err := collector.ReadNextTasks(ctx, 10)
    75  			if err != nil {
    76  				return err
    77  			}
    78  
    79  			if len(tasks) == 0 {
    80  				if *follow {
    81  					time.Sleep(time.Second)
    82  					continue
    83  				}
    84  				break
    85  			}
    86  
    87  			for i := range tasks {
    88  				pretty.Println(tasks[i])
    89  			}
    90  		}
    91  
    92  		return nil
    93  	})
    94  }