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