github.com/vmware/govmomi@v0.51.0/object/search_index.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 object 6 7 import ( 8 "context" 9 10 "github.com/vmware/govmomi/vim25" 11 "github.com/vmware/govmomi/vim25/methods" 12 "github.com/vmware/govmomi/vim25/types" 13 ) 14 15 type SearchIndex struct { 16 Common 17 } 18 19 func NewSearchIndex(c *vim25.Client) *SearchIndex { 20 s := SearchIndex{ 21 Common: NewCommon(c, *c.ServiceContent.SearchIndex), 22 } 23 24 return &s 25 } 26 27 // FindByDatastorePath finds a virtual machine by its location on a datastore. 28 func (s SearchIndex) FindByDatastorePath(ctx context.Context, dc *Datacenter, path string) (Reference, error) { 29 req := types.FindByDatastorePath{ 30 This: s.Reference(), 31 Datacenter: dc.Reference(), 32 Path: path, 33 } 34 35 res, err := methods.FindByDatastorePath(ctx, s.c, &req) 36 if err != nil { 37 return nil, err 38 } 39 40 if res.Returnval == nil { 41 return nil, nil 42 } 43 return NewReference(s.c, *res.Returnval), nil 44 } 45 46 // FindByDnsName finds a virtual machine or host by DNS name. 47 func (s SearchIndex) FindByDnsName(ctx context.Context, dc *Datacenter, dnsName string, vmSearch bool) (Reference, error) { 48 req := types.FindByDnsName{ 49 This: s.Reference(), 50 DnsName: dnsName, 51 VmSearch: vmSearch, 52 } 53 if dc != nil { 54 ref := dc.Reference() 55 req.Datacenter = &ref 56 } 57 58 res, err := methods.FindByDnsName(ctx, s.c, &req) 59 if err != nil { 60 return nil, err 61 } 62 63 if res.Returnval == nil { 64 return nil, nil 65 } 66 return NewReference(s.c, *res.Returnval), nil 67 } 68 69 // FindByInventoryPath finds a managed entity based on its location in the inventory. 70 func (s SearchIndex) FindByInventoryPath(ctx context.Context, path string) (Reference, error) { 71 req := types.FindByInventoryPath{ 72 This: s.Reference(), 73 InventoryPath: path, 74 } 75 76 res, err := methods.FindByInventoryPath(ctx, s.c, &req) 77 if err != nil { 78 return nil, err 79 } 80 81 if res.Returnval == nil { 82 return nil, nil 83 } 84 85 r := NewReference(s.c, *res.Returnval) 86 87 type common interface { 88 SetInventoryPath(string) 89 } 90 91 if c, ok := r.(common); ok { 92 c.SetInventoryPath(path) 93 } 94 95 return r, nil 96 } 97 98 // FindByIp finds a virtual machine or host by IP address. 99 func (s SearchIndex) FindByIp(ctx context.Context, dc *Datacenter, ip string, vmSearch bool) (Reference, error) { 100 req := types.FindByIp{ 101 This: s.Reference(), 102 Ip: ip, 103 VmSearch: vmSearch, 104 } 105 if dc != nil { 106 ref := dc.Reference() 107 req.Datacenter = &ref 108 } 109 110 res, err := methods.FindByIp(ctx, s.c, &req) 111 if err != nil { 112 return nil, err 113 } 114 115 if res.Returnval == nil { 116 return nil, nil 117 } 118 return NewReference(s.c, *res.Returnval), nil 119 } 120 121 // FindByUuid finds a virtual machine or host by UUID. 122 func (s SearchIndex) FindByUuid(ctx context.Context, dc *Datacenter, uuid string, vmSearch bool, instanceUuid *bool) (Reference, error) { 123 req := types.FindByUuid{ 124 This: s.Reference(), 125 Uuid: uuid, 126 VmSearch: vmSearch, 127 InstanceUuid: instanceUuid, 128 } 129 if dc != nil { 130 ref := dc.Reference() 131 req.Datacenter = &ref 132 } 133 134 res, err := methods.FindByUuid(ctx, s.c, &req) 135 if err != nil { 136 return nil, err 137 } 138 139 if res.Returnval == nil { 140 return nil, nil 141 } 142 return NewReference(s.c, *res.Returnval), nil 143 } 144 145 // FindChild finds a particular child based on a managed entity name. 146 func (s SearchIndex) FindChild(ctx context.Context, entity Reference, name string) (Reference, error) { 147 req := types.FindChild{ 148 This: s.Reference(), 149 Entity: entity.Reference(), 150 Name: name, 151 } 152 153 res, err := methods.FindChild(ctx, s.c, &req) 154 if err != nil { 155 return nil, err 156 } 157 158 if res.Returnval == nil { 159 return nil, nil 160 } 161 return NewReference(s.c, *res.Returnval), nil 162 } 163 164 // FindAllByDnsName finds all virtual machines or hosts by DNS name. 165 func (s SearchIndex) FindAllByDnsName(ctx context.Context, dc *Datacenter, dnsName string, vmSearch bool) ([]Reference, error) { 166 req := types.FindAllByDnsName{ 167 This: s.Reference(), 168 DnsName: dnsName, 169 VmSearch: vmSearch, 170 } 171 if dc != nil { 172 ref := dc.Reference() 173 req.Datacenter = &ref 174 } 175 176 res, err := methods.FindAllByDnsName(ctx, s.c, &req) 177 if err != nil { 178 return nil, err 179 } 180 181 if len(res.Returnval) == 0 { 182 return nil, nil 183 } 184 185 var references []Reference 186 for _, returnval := range res.Returnval { 187 references = append(references, NewReference(s.c, returnval)) 188 } 189 return references, nil 190 } 191 192 // FindAllByIp finds all virtual machines or hosts by IP address. 193 func (s SearchIndex) FindAllByIp(ctx context.Context, dc *Datacenter, ip string, vmSearch bool) ([]Reference, error) { 194 req := types.FindAllByIp{ 195 This: s.Reference(), 196 Ip: ip, 197 VmSearch: vmSearch, 198 } 199 if dc != nil { 200 ref := dc.Reference() 201 req.Datacenter = &ref 202 } 203 204 res, err := methods.FindAllByIp(ctx, s.c, &req) 205 if err != nil { 206 return nil, err 207 } 208 209 if len(res.Returnval) == 0 { 210 return nil, nil 211 } 212 213 var references []Reference 214 for _, returnval := range res.Returnval { 215 references = append(references, NewReference(s.c, returnval)) 216 } 217 return references, nil 218 } 219 220 // FindAllByUuid finds all virtual machines or hosts by UUID. 221 func (s SearchIndex) FindAllByUuid(ctx context.Context, dc *Datacenter, uuid string, vmSearch bool, instanceUuid *bool) ([]Reference, error) { 222 req := types.FindAllByUuid{ 223 This: s.Reference(), 224 Uuid: uuid, 225 VmSearch: vmSearch, 226 InstanceUuid: instanceUuid, 227 } 228 if dc != nil { 229 ref := dc.Reference() 230 req.Datacenter = &ref 231 } 232 233 res, err := methods.FindAllByUuid(ctx, s.c, &req) 234 if err != nil { 235 return nil, err 236 } 237 238 if len(res.Returnval) == 0 { 239 return nil, nil 240 } 241 242 var references []Reference 243 for _, returnval := range res.Returnval { 244 references = append(references, NewReference(s.c, returnval)) 245 } 246 return references, nil 247 }