github.com/vmware/govmomi@v0.51.0/task/history_collector.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 task
     6  
     7  import (
     8  	"context"
     9  
    10  	"github.com/vmware/govmomi/history"
    11  	"github.com/vmware/govmomi/vim25"
    12  	"github.com/vmware/govmomi/vim25/methods"
    13  	"github.com/vmware/govmomi/vim25/mo"
    14  	"github.com/vmware/govmomi/vim25/types"
    15  )
    16  
    17  // HistoryCollector provides a mechanism for retrieving historical data and
    18  // updates when the server appends new tasks.
    19  type HistoryCollector struct {
    20  	*history.Collector
    21  }
    22  
    23  func newHistoryCollector(c *vim25.Client, ref types.ManagedObjectReference) *HistoryCollector {
    24  	return &HistoryCollector{
    25  		Collector: history.NewCollector(c, ref),
    26  	}
    27  }
    28  
    29  // LatestPage returns items in the 'viewable latest page' of the task history collector.
    30  // As new tasks that match the collector's TaskFilterSpec are created,
    31  // they are added to this page, and the oldest tasks are removed from the collector to keep
    32  // the size of the page to that allowed by SetCollectorPageSize.
    33  // The "oldest task" is the one with the oldest creation time. The tasks in the returned page are unordered.
    34  func (h HistoryCollector) LatestPage(ctx context.Context) ([]types.TaskInfo, error) {
    35  	var o mo.TaskHistoryCollector
    36  
    37  	err := h.Properties(ctx, h.Reference(), []string{"latestPage"}, &o)
    38  	if err != nil {
    39  		return nil, err
    40  	}
    41  
    42  	return o.LatestPage, nil
    43  }
    44  
    45  // ReadNextTasks reads the scrollable view from the current position. The
    46  // scrollable position is moved to the next newer page after the read. No item
    47  // is returned when the end of the collector is reached.
    48  func (h HistoryCollector) ReadNextTasks(ctx context.Context, maxCount int32) ([]types.TaskInfo, error) {
    49  	req := types.ReadNextTasks{
    50  		This:     h.Reference(),
    51  		MaxCount: maxCount,
    52  	}
    53  
    54  	res, err := methods.ReadNextTasks(ctx, h.Client(), &req)
    55  	if err != nil {
    56  		return nil, err
    57  	}
    58  
    59  	return res.Returnval, nil
    60  }
    61  
    62  // ReadPreviousTasks reads the scrollable view from the current position. The
    63  // scrollable position is then moved to the next older page after the read. No
    64  // item is returned when the head of the collector is reached.
    65  func (h HistoryCollector) ReadPreviousTasks(ctx context.Context, maxCount int32) ([]types.TaskInfo, error) {
    66  	req := types.ReadPreviousTasks{
    67  		This:     h.Reference(),
    68  		MaxCount: maxCount,
    69  	}
    70  
    71  	res, err := methods.ReadPreviousTasks(ctx, h.Client(), &req)
    72  	if err != nil {
    73  		return nil, err
    74  	}
    75  
    76  	return res.Returnval, nil
    77  }