github.com/vmware/govmomi@v0.37.2/task/history_collector.go (about)

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