github.com/vmware/govmomi@v0.51.0/view/container_view.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 view
     6  
     7  import (
     8  	"context"
     9  
    10  	"github.com/vmware/govmomi/property"
    11  	"github.com/vmware/govmomi/vim25"
    12  	"github.com/vmware/govmomi/vim25/mo"
    13  	"github.com/vmware/govmomi/vim25/types"
    14  )
    15  
    16  type ContainerView struct {
    17  	ManagedObjectView
    18  }
    19  
    20  func NewContainerView(c *vim25.Client, ref types.ManagedObjectReference) *ContainerView {
    21  	return &ContainerView{
    22  		ManagedObjectView: *NewManagedObjectView(c, ref),
    23  	}
    24  }
    25  
    26  // Retrieve populates dst as property.Collector.Retrieve does, for all entities in the view of types specified by kind.
    27  func (v ContainerView) Retrieve(ctx context.Context, kind []string, ps []string, dst any, pspec ...types.PropertySpec) error {
    28  	pc := property.DefaultCollector(v.Client())
    29  
    30  	ospec := types.ObjectSpec{
    31  		Obj:  v.Reference(),
    32  		Skip: types.NewBool(true),
    33  		SelectSet: []types.BaseSelectionSpec{
    34  			&types.TraversalSpec{
    35  				Type: v.Reference().Type,
    36  				Path: "view",
    37  			},
    38  		},
    39  	}
    40  
    41  	if len(kind) == 0 {
    42  		kind = []string{"ManagedEntity"}
    43  	}
    44  
    45  	for _, t := range kind {
    46  		spec := types.PropertySpec{
    47  			Type: t,
    48  		}
    49  
    50  		if len(ps) == 0 {
    51  			spec.All = types.NewBool(true)
    52  		} else {
    53  			spec.PathSet = ps
    54  		}
    55  
    56  		pspec = append(pspec, spec)
    57  	}
    58  
    59  	req := types.RetrieveProperties{
    60  		SpecSet: []types.PropertyFilterSpec{
    61  			{
    62  				ObjectSet: []types.ObjectSpec{ospec},
    63  				PropSet:   pspec,
    64  			},
    65  		},
    66  	}
    67  
    68  	res, err := pc.RetrieveProperties(ctx, req)
    69  	if err != nil {
    70  		return err
    71  	}
    72  
    73  	if d, ok := dst.(*[]types.ObjectContent); ok {
    74  		*d = res.Returnval
    75  		return nil
    76  	}
    77  
    78  	return mo.LoadObjectContent(res.Returnval, dst)
    79  }
    80  
    81  // RetrieveWithFilter populates dst as Retrieve does, but only for entities matching the given filter.
    82  func (v ContainerView) RetrieveWithFilter(ctx context.Context, kind []string, ps []string, dst any, filter property.Match) error {
    83  	if len(filter) == 0 {
    84  		return v.Retrieve(ctx, kind, ps, dst)
    85  	}
    86  
    87  	var content []types.ObjectContent
    88  
    89  	err := v.Retrieve(ctx, kind, filter.Keys(), &content)
    90  	if err != nil {
    91  		return err
    92  	}
    93  
    94  	objs := filter.ObjectContent(content)
    95  
    96  	pc := property.DefaultCollector(v.Client())
    97  
    98  	return pc.Retrieve(ctx, objs, ps, dst)
    99  }
   100  
   101  // Find returns object references for entities of type kind, matching the given filter.
   102  func (v ContainerView) Find(ctx context.Context, kind []string, filter property.Match) ([]types.ManagedObjectReference, error) {
   103  	if len(filter) == 0 {
   104  		// Ensure we have at least 1 filter to avoid retrieving all properties.
   105  		filter = property.Match{"name": "*"}
   106  	}
   107  
   108  	var content []types.ObjectContent
   109  
   110  	err := v.Retrieve(ctx, kind, filter.Keys(), &content)
   111  	if err != nil {
   112  		return nil, err
   113  	}
   114  
   115  	return filter.ObjectContent(content), nil
   116  }
   117  
   118  // FindAny returns object references for entities of type kind, matching any property the given filter.
   119  func (v ContainerView) FindAny(ctx context.Context, kind []string, filter property.Match) ([]types.ManagedObjectReference, error) {
   120  	if len(filter) == 0 {
   121  		// Ensure we have at least 1 filter to avoid retrieving all properties.
   122  		filter = property.Match{"name": "*"}
   123  	}
   124  
   125  	var content []types.ObjectContent
   126  
   127  	err := v.Retrieve(ctx, kind, filter.Keys(), &content)
   128  	if err != nil {
   129  		return nil, err
   130  	}
   131  
   132  	return filter.AnyObjectContent(content), nil
   133  }